Results 1 to 10 of 10

Thread: Autowired Object return null

  1. #1

    Default Autowired Object return null

    Hello to all, I have configure Spring to autowired my dao object into servlet but it return null. Anyone know why?

    web.xml
    Code:
    <?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>BISApp</display-name>
      <listener>
        <listener-class>com.breeze.bis.app.listener.VBossContextListener</listener-class>
      </listener>
      
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>applicationContext.xml</param-value>
      </context-param>
      
      <listener>
      <display-name>contextLoaderListener</display-name>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <filter>
        <filter-name>compress</filter-name>
        <filter-class>com.breeze.web.commons.filters.CompressionFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>compress</filter-name>
        <url-pattern>*.view</url-pattern>
        <url-pattern>*.get</url-pattern>
        <url-pattern>*.jsp</url-pattern>
      </filter-mapping>
      <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>
          com.breeze.bis.app.filters.BISHibernateSessionFilter
        </filter-class>
      </filter>
      <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>*.view</url-pattern>
        <url-pattern>*.get</url-pattern>
      </filter-mapping>
      <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      <filter>
        <display-name>AuthFilter</display-name>
        <filter-name>AuthFilter</filter-name>
        <filter-class>com.breeze.bis.app.filters.BISAuthFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>AuthFilter</filter-name>
        <url-pattern>*.get</url-pattern>
        <url-pattern>*.view</url-pattern>
        <url-pattern>*.jsp</url-pattern>
      </filter-mapping>
      <filter>
        <display-name>CacheFilter</display-name>
        <filter-name>CacheFilter</filter-name>
        <filter-class>com.breeze.web.commons.filters.CacheFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>CacheFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      <servlet>
        <display-name>AppController</display-name>
        <servlet-name>AppController</servlet-name>
        <servlet-class>com.breeze.bis.app.servlets.BISAppController</servlet-class>
        <load-on-startup></load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>AppController</servlet-name>
        <url-pattern>/app.view</url-pattern>
        <url-pattern>/app.get</url-pattern>
      </servlet-mapping>
      <servlet>
        <description></description>
        <display-name>AuthServlet</display-name>
        <servlet-name>AuthServlet</servlet-name>
        <servlet-class>com.breeze.bis.app.servlets.BISAuthServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>AuthServlet</servlet-name>
        <url-pattern>/auth.view</url-pattern>
        <url-pattern>/admin/auth.view</url-pattern>
      </servlet-mapping>
      <session-config>
        <session-timeout>60</session-timeout>
      </session-config>
      <servlet>
        <description></description>
        <display-name>GetCustomerAddImage</display-name>
        <servlet-name>GetCustomerAddImage</servlet-name>
        <servlet-class>com.breeze.bis.app.servlets.GetCustomerAddImage</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>GetCustomerAddImage</servlet-name>
        <url-pattern>/GetCustomerAddImage</url-pattern>
      </servlet-mapping>
    </web-app>
    applicationContext.xml
    Code:
    <?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"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    	<context:annotation-config />
    	<context:component-scan base-package="com.breeze.bis.core.service.jdbcTemplate" />
    	
    	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    
    	
    	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/vbossdb" />
    		<property name="driverClass" value="com.mysql.jdbc.Driver" />
    		<property name="user" value="root" />
    		<property name="password" value="root" />
    	</bean>
    	
    	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    		<constructor-arg type="javax.sql.DataSource" ref="dataSource"></constructor-arg>
    	</bean>
    
    	<bean id="genericDaoImpl" class="com.breeze.bis.core.service.jdbcTemplate.GenericDAOImpl">
    		<property name="jdbcDao" ref="jdbcTemplate"></property>	
    	</bean>
    	
    	 <!--
                    This will automatically locate any and all property files you have
                    within your classpath, provided they fall under the META-INF
                    directory. The located property files are parsed and their values can
                    then be used within application context files in the form of
                    ${propertyKey}.
            -->
            <!--  
            	<context:property-placeholder location="classpath*:*.properties" />
            -->
            
            <!--
                    This declaration will cause Spring to locate every @Component,
                    @Repository and @Service in your application. In practical terms this
                    allows you to write a POJO and then simply annotate the new POJO as an
                    @Service and Spring will automatically detect, instantiate and
                    dependency inject your service at startup time. Importantly, you can
                    then also have your new service injected into any other class that
                    requires it simply by declaring a field for your service inside the
                    relying class and Spring will inject it. Note that two exclude filters
                    are declared. The first ensures that Spring doesn't spend time
                    introspecting Roo-specific ITD aspects. The second ensures Roo doesn't
                    instantiate your @Controller classes, as these should be instantiated
                    by a web tier application context. Refer to web.xml for more details
                    about the web tier application context setup services. Furthermore,
                    this turns on @Autowired, @PostConstruct etc support. These
                    annotations allow you to use common Spring and Java Enterprise Edition
                    annotations in your classes without needing to do any special
                    configuration. The most commonly used annotation is @Autowired, which
                    instructs Spring to dependency inject an object into your class.
            -->
            <!--  
            <context:component-scan base-package="sk.funix.userstory">
                    <context:exclude-filter expression="org.springframework.stereotype.Controller"
                            type="annotation" />
            </context:component-scan>
            
            <import resource="applicationContext-infrastructure.xml" />
            <import resource="applicationContext-aop.xml" />
            <import resource="applicationContext-security.xml" />
            -->
    
    </beans>
    Servlet

    Code:
    public class UnknownTroubleTicketViewTask extends AbstractTask {
    	
    	@Autowired
    	GenericDAO dao;

    What is the reason? Please help. Thanks.

  2. #2

    Default asd

    What is the reason?
    Last edited by peterwkc; Dec 18th, 2012 at 08:13 PM.

  3. #3
    Join Date
    Dec 2009
    Location
    India
    Posts
    108

    Default

    Your config looks ok but you haven't mentioned how are you creating objects of your
    UnknownTroubleTicketViewTask
    . Is it via XML or via annotations. If it is via annotations, this means you're not adding the package of this class to do component scanning. If it is via XML, then you're not setting the autowire attribute with proper value. Can you confirm?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Please use [ code][/code ] tags when posting code, that way it remains readable.

    Hello, I tried to autowired a dao object into servlet but it return null. Anyone know why?
    Which isn't going to work. Spring will only inject beans into other spring managed beans and not into beans it doesn't know about. So instead of injection you will have to do a lookup or change your servlet and use AspectJ and @Configurable on your servlet.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    Quote Originally Posted by objectamit View Post
    Your config looks ok but you haven't mentioned how are you creating objects of your . Is it via XML or via annotations. If it is via annotations, this means you're not adding the package of this class to do component scanning. If it is via XML, then you're not setting the autowire attribute with proper value. Can you confirm?
    The UnknownTroubleTicketTask is created by our custom class loading mechanism framework. Is it feasible to add the @Configurable into the servlet ? Does it create two instances of UnknownTroubleTicketTask ?

    I had added the @Configurable into UnknownTroubleTicketTask but the DAO still shows null.

    How to configure the AspectJ AOP?

    Thanks.
    Last edited by peterwkc; Dec 18th, 2012 at 08:24 PM.

  6. #6
    Join Date
    Dec 2009
    Location
    India
    Posts
    108

    Default

    Dude,

    for sure, your config to enable @Configurable is faulty and you need to read the reference docs. Tht should be pretty obvious, Right??? I mean, you're seeing that some of the objects have been initialized by spring as you expected but some were not. That means, some who didn't, weren't configured properly.


  7. #7

    Default

    Quote Originally Posted by objectamit View Post
    Dude,

    for sure, your config to enable @Configurable is faulty and you need to read the reference docs. Tht should be pretty obvious, Right??? I mean, you're seeing that some of the objects have been initialized by spring as you expected but some were not. That means, some who didn't, weren't configured properly.

    I reading the documentation now. Does the Spring AOP create additional (second) UnknownTroubleTicketTask object other than UnknownTroubleTicketTask object created by my custom framework (Extension of Java EE Servlet) ?

    Since Spring need to manage the bean in order to inject the additional bean into it, then I guess Spring will create additional (second) UnknownTroubleTicketTask object because Spring has no knowledge about the UnknownTroubleTicketTask object. Right.
    Last edited by peterwkc; Dec 19th, 2012 at 09:31 PM.

  8. #8

    Default

    I have a question in stackoverflow

    Please help.

    Thanks.

  9. #9

    Default

    Please help.

  10. #10

    Default

    Please help in this thread or from stackoverflow.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •