Announcement Announcement Module
Collapse
No announcement yet.
Dependency injection don't works Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dependency injection don't works

    I'm new in Spring and i have the next problem:

    I'm using Spring MVC in Netbeans and when i try to inyect a bean,
    it does fine but when i use that bean inyected the value of that is "null"

    My question is the container of SpringMVC works differently ???

    Or what i need to the value of the inyected objects don't be "null" ??

    This is my applicationContext.xml configuration

    <?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: p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/test"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
    </bean>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean" scope="globalSession">
    <property name="dataSource" ref="myDataSource"/>
    <property name="mappingResources">
    <list>
    <value>peristencia/tablas/Productos.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQ LDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="current_session_context_class">thread</prop>
    </props>
    </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory" />
    </bean>

    <bean id="hibernateProductoDao" class="persistencia.dao.HibernateProductoDao">
    <property name="sessionFactory"><ref bean="mySessionFactory"/></property>
    </bean>

    <bean id="implementacionBussinesInterface" class="bussines.capa.ImplementacionBussinesInterfa ce">
    <property name="productoDao"><ref bean="hibernateProductoDao"/></property>
    </bean>

    <bean id="contoladorView" class="contoller.capa.ContoladorView">
    <property name="implementacionBussinesInterface" ref="implementacionBussinesInterface"/>
    <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>

    </beans>

    Someone has an idea ???

  • #2
    Your bean definitions look ok. Can you post a full stack trace?

    Comment


    • #3
      No stack trace

      My question is when Spring MVC load the applicationContext ?? or i need to do that in a Servlet Controller ??? i'm new on Spring MVC

      Comment


      • #4
        The Spring DispatcherServlet also needs to be setup correctly in web.xml, otherwise your application context will never be picked up.

        Comment


        • #5
          Can you explain me about DispatcherServlet setup ??

          This is my web.xml configuration :


          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
          <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/applicationContext.xml</param-value>
          </context-param>
          <listener>
          <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
          </listener>
          <servlet>
          <servlet-name>dispatcher</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
          <load-on-startup>2</load-on-startup>
          </servlet>
          <servlet>
          <servlet-name>dwr-invoker</servlet-name>
          <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
          <init-param>
          <param-name>debug</param-name>
          <param-value>true</param-value>
          </init-param>
          </servlet>
          <servlet-mapping>
          <servlet-name>dispatcher</servlet-name>
          <url-pattern>*.htm</url-pattern>
          </servlet-mapping>
          <servlet-mapping>
          <servlet-name>dwr-invoker</servlet-name>
          <url-pattern>/dwr/*</url-pattern>
          </servlet-mapping>
          <session-config>
          <session-timeout>
          30
          </session-timeout>
          </session-config>
          <welcome-file-list>
          <welcome-file>redirect.jsp</welcome-file>
          </welcome-file-list>
          </web-app>


          Do you look something wrong ???

          Comment


          • #6
            Your web.xml looks ok to me. Requests to *.htm should get picked up by the Spring DispatcherServlet and passed off to the Spring container. However you haven't defined any controllers, or you haven't told Spring where to look for your controllers. You can either use annotations or create more bean definitions. I prefer the annotation approach.

            Can you please post a stack trace? Otherwise I'm only guessing as to what the problem might be.

            If you're new to Spring, I recommend you read the documentation http://static.springsource.org/sprin...eference/html/ and get a better understanding of how it all works.

            Comment


            • #7
              Full stack trace

              I can't look something wrong

              HTML Code:
              8/12/2010 01:15:33 PM org.apache.catalina.core.StandardService start
              INFO: Arrancando servicio Catalina
              8/12/2010 01:15:33 PM org.apache.catalina.core.StandardEngine start
              INFO: Starting Servlet Engine: Apache Tomcat/6.0.26
              8/12/2010 01:15:33 PM org.apache.catalina.startup.HostConfig deployDescriptor
              INFO: Desplieque del descriptor de configuración manager.xml
              8/12/2010 01:15:33 PM org.apache.catalina.startup.HostConfig deployDescriptor
              INFO: Desplieque del descriptor de configuración ROOT.xml
              8/12/2010 01:15:33 PM org.apache.catalina.startup.HostConfig deployDescriptor
              INFO: Desplieque del descriptor de configuración SpringEjemplo.xml
              8/12/2010 01:15:34 PM org.springframework.web.context.ContextLoader initWebApplicationContext
              INFO: Root WebApplicationContext: initialization started
              8/12/2010 01:15:34 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
              INFO: Refreshing Root WebApplicationContext: startup date [Wed Dec 08 13:15:34 CST 2010]; root of context hierarchy
              8/12/2010 01:15:34 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
              INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
              8/12/2010 01:15:42 PM org.hibernate.cfg.SettingsFactory buildSettings
              INFO: Query language substitutions: {}
              8/12/2010 01:15:42 PM org.hibernate.cfg.SettingsFactory buildSettings
              INFO: JPA-QL strict compliance: disabled
              8/12/2010 01:15:42 PM org.hibernate.cfg.SettingsFactory buildSettings
              INFO: Second-level cache: enabled
              8/12/2010 01:15:42 PM org.hibernate.cfg.SettingsFactory buildSettings
              INFO: Query cache: disabled
              8/12/2010 01:15:42 PM org.hibernate.cfg.SettingsFactory createCacheProvider
              INFO: Cache provider: org.hibernate.cache.NoCacheProvider
              8/12/2010 01:15:42 PM org.hibernate.cfg.SettingsFactory buildSettings
              INFO: Optimize cache for minimal puts: disabled
              8/12/2010 01:15:42 PM org.hibernate.cfg.SettingsFactory buildSettings
              INFO: Structured second-level cache entries: disabled
              8/12/2010 01:15:42 PM org.hibernate.cfg.SettingsFactory buildSettings
              INFO: Echoing all SQL to stdout
              8/12/2010 01:15:42 PM org.hibernate.cfg.SettingsFactory buildSettings
              INFO: Statistics: disabled
              8/12/2010 01:15:42 PM org.hibernate.cfg.SettingsFactory buildSettings
              INFO: Deleted entity synthetic identifier rollback: disabled
              8/12/2010 01:15:42 PM org.hibernate.cfg.SettingsFactory buildSettings
              INFO: Default entity-mode: pojo
              8/12/2010 01:15:42 PM org.hibernate.cfg.SettingsFactory buildSettings
              INFO: Named query checking : enabled
              8/12/2010 01:15:42 PM org.hibernate.impl.SessionFactoryImpl <init>
              INFO: building session factory
              8/12/2010 01:15:43 PM org.hibernate.impl.SessionFactoryObjectFactory addInstance
              INFO: Not binding factory to JNDI, no JNDI name configured
              8/12/2010 01:15:43 PM org.springframework.orm.hibernate3.HibernateTransactionManager afterPropertiesSet
              INFO: Using DataSource [org.apache.commons.dbcp.BasicDataSource@86f716] of Hibernate SessionFactory for HibernateTransactionManager
              8/12/2010 01:15:43 PM org.springframework.web.context.ContextLoader initWebApplicationContext
              INFO: Root WebApplicationContext: initialization completed in 2879 ms
              8/12/2010 01:15:43 PM org.springframework.web.servlet.FrameworkServlet initServletBean
              INFO: FrameworkServlet 'dispatcher': initialization started
              8/12/2010 01:15:43 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
              INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Wed Dec 08 13:15:43 CST 2010]; parent: Root WebApplicationContext
              8/12/2010 01:15:43 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
              INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
              8/12/2010 01:15:43 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
              INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@8a4156: defining beans [org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0,urlMapping,viewResolver,indexController]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@18efa2f
              8/12/2010 01:15:43 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
              INFO: Mapped URL path [/index.htm] onto handler [org.springframework.web.servlet.mvc.ParameterizableViewController@1f0c45f]
              8/12/2010 01:15:43 PM org.springframework.web.servlet.FrameworkServlet initServletBean
              INFO: FrameworkServlet 'dispatcher': initialization completed in 287 ms
              8/12/2010 01:15:43 PM org.apache.catalina.core.StandardContext start
              INFO: Ya ha sido arrancado el Contenedor org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/SpringEjemplo]
              8/12/2010 01:15:52 PM org.directwebremoting.impl.StartupUtil logStartup
              INFO: Starting: DwrServlet v3.0.0.116.rc1 on Apache Tomcat/6.0.26 / JDK 1.6.0_02 from Sun Microsystems Inc. at /SpringEjemplo

              Comment


              • #8
                I don't see anything failing. No exceptions are being thrown on startup, so that tells us the DispatcherServlet is starting. I can even see that the Spring container is preinstantiating singletons. So that's good.

                There only appears to be a single controller defined, the index controller. When you browse to index.htm at the root of your app, what happens? Does it throw an exception at this point?

                Comment


                • #9
                  Dwr

                  No matter with that point, you can see i'm usin DWR. So when i expose a method of my Bussines Class in DWR and try to acces it, the objects inyected in that class, like DAO'S, are null. Can be that my problem, the acces by DWR ???

                  Comment


                  • #10
                    I don't have any experience integrating Spring with DWR. I think there's some threads on this forum you can search for that cover that. That could be part of your problem.

                    Comment


                    • #11
                      Ok

                      Ok txs a lot

                      Comment


                      • #12
                        You want to use DWR with Spring, you should use Spring-DWR integration. That means you shouldn't define dwr servlet and config but should use dwr through Spring's own DispatcherServlet and define dwr business classes with @RemoteProxy annotations. It's all explained here.

                        The cause of your problem is that your business class that dwr is using is not managed by Spring so dependency injection simply doesn't happen. DWR by itself doesn't know what DI is so when it instantiates its business classes defined in its own configuration files, it uses the java keyword new which means it's creating an instance of that class that is not managed by Spring and so all DI objects you expect to be there CANNOT be there, that is the source of your nullpointerexception.

                        Comment


                        • #13
                          It Works !!!!

                          You has right !!!! My problem was the DWR configuration.

                          Now the objects inyection happends

                          Thank's a lot

                          Comment

                          Working...
                          X