Spring jdbcTemplate es nulo
Frecuentes
Visto 4,136 veces
1
Estoy tratando de hacer un "repetitivo" SpringMVC + JDBC que funcione ...
cuando accedo a home.vis obtengo la salida de la consola: null (getJdbcTemplate() => null)
¿POR QUÉ?
la fuente:
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>
despachador-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;
}
}
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;
}
}
gracias!
1 Respuestas
5
no instanciar TestDao
en su controlador, inyéctelo. Ese es el objetivo de la inyección de dependencia.
public class HomeController implements Controller {
private TestDao testDao;
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}
y
<bean name="/home.vis" class="it.almaviva.springhello.web.HomeController">
<property name="testDao" ref="testDao"/>
</bean>
Por cierto, su código está escrito en el estilo de las aplicaciones Spring 2.0, un estilo que ahora está obsoleto. Las nuevas aplicaciones de Spring deben escribirse usando el @Controller
estilo anotado (16.3 Implementación de controladores). Además, cableado automático (4.9 Configuración de contenedor basada en anotaciones) y escaneo de componentes (4.10 Escaneo de Classpath y componentes administrados) reducir la cantidad de placa de caldera que tiene que escribir.
respondido 09 mar '12, 15:03
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas java spring jdbctemplate or haz tu propia pregunta.