gpt4 book ai didi

java - Spring jdbcTemplate 为空

转载 作者:行者123 更新时间:2023-11-29 08:08:18 25 4
gpt4 key购买 nike

我正在尝试制作一个可以工作的 SpringMVC + JDBC“样板”..

当我访问 home.vis 时,我得到控制台输出:null (getJdbcTemplate() => null)

为什么?

来源:

网络.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringHello</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.vis</url-pattern>
</servlet-mapping>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/satmDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>

调度器-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<description>Spring Hello World</description>
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/satmDataSource" />
<bean name="/home.vis" class="it.almaviva.springhello.web.HomeController" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="testDao" class="it.almaviva.springhello.dao.TestDao">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>

TestDao.java

package it.almaviva.springhello.dao;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class TestDao extends JdbcDaoSupport {
public int lookup() {
System.out.println( getJdbcTemplate() );
return 0;
}
}

家庭 Controller .java

package it.almaviva.springhello.web;

import it.almaviva.springhello.dao.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HomeController implements Controller {

@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
ModelAndView mv = new ModelAndView("home");

TestDao jdbc = new TestDao();
mv.addObject("msg", jdbc.lookup());

return mv;
}

}

谢谢!

最佳答案

不要在 Controller 中实例化 TestDao,注入(inject)它。这就是依赖注入(inject)的全部要点。

public class HomeController implements Controller {

private TestDao testDao;

public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}

<bean name="/home.vis" class="it.almaviva.springhello.web.HomeController">
<property name="testDao" ref="testDao"/>
</bean>

顺便说一下,您的代码是以 Spring 2.0 应用程序的风格编写的,这种风格现在已经过时了。新的 Spring 应用程序应该使用 @Controller 注释样式(16.3 Implementing Controllers)编写。此外, Autowiring (4.9 Annotation-based container configuration) 和组件扫描 (4.10 Classpath scanning and managed components) 减少了您必须编写的样板数量。

关于java - Spring jdbcTemplate 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9636663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com