Spring jdbcTemplate es nulo

I'm trying to make a SpringMVC + JDBC "boilerplate" that works..

when I access home.vis I get the console output: 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!

preguntado el 09 de marzo de 12 a las 15:03

1 Respuestas

Don't instantiate TestDao in your controller, inject it. That's the whole point of dependency injection.

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>

Incidentally, your code is written in the style of Spring 2.0 applications, a style that's now obsolete. New Spring apps should be written using the @Controller annotated style (16.3 Implementación de controladores). Also, autowiring (4.9 Configuración de contenedor basada en anotaciones) and component scanning (4.10 Escaneo de Classpath y componentes administrados) reduce the amount of boiler plate that you have to write.

respondido 09 mar '12, 15:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.