frx*_*x08 1 java spring jdbctemplate
我正在尝试制作一个有效的SpringMVC + JDBC"样板".
当我访问home.vis时,我得到控制台输出:null(getJdbcTemplate()=> null)
为什么?
来源:
web.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>
Run Code Online (Sandbox Code Playgroud)
调度员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>
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
HomeController.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;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
不要TestDao在控制器中实例化,注入它.这就是依赖注入的全部要点.
public class HomeController implements Controller {
private TestDao testDao;
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}
Run Code Online (Sandbox Code Playgroud)
和
<bean name="/home.vis" class="it.almaviva.springhello.web.HomeController">
<property name="testDao" ref="testDao"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
顺便提一下,您的代码是以Spring 2.0应用程序的风格编写的,这种风格现在已经过时了.应使用带@Controller注释的样式(16.3实现控制器)编写新的Spring应用程序.此外,自动装配(4.9基于注释的容器配置)和组件扫描(4.10类路径扫描和托管组件)可减少您必须编写的锅炉板数量.
| 归档时间: |
|
| 查看次数: |
3933 次 |
| 最近记录: |