Results 1 to 7 of 7

Thread: autowiring issues

  1. #1
    Join Date
    Jun 2012
    Posts
    4

    Default autowiring issues

    Hi all,

    I have a little problem that I just can't figure out how to make this work.
    I have written some services that I want to autowire into a class so I can access the database.
    Spring however seems to ignore the autowired annotations or something like that, because my services (that should be autowired) are always null.
    In other classes there is no problem autowiring the same services.

    Just to make sure I have everything configured as it should here's the code for applicationContext.xml
    Code:
     <tx:annotation-driven />
    
        <context:component-scan base-package="..." />
        <context:annotation-config />
    and here is the class I want to autowire the services
    Code:
    @Component
    public class SurveyResultFilter implements Serializable {
    
    	private static final long serialVersionUID = 499484529915188060L;
    
        @Autowired
        private transient CategoryService categoryService;
        @Autowired
        private transient SkillService skillService;
    }
    I left out the other code which I thought was not relevant
    Thanks for your help!

  2. #2

    Default

    Hi,

    Post up your full web.xml and application-context as it helps.

    Are you scanning the correct classpath for your services and are your services annotated too?
    If this is null it can down to number of reasons, but could be because spring cannot resolve the bean name to be autowired.

    If you have STS create a template spring mvc project and compare the configuration etc and try to get that working.

  3. #3
    Join Date
    Jun 2012
    Posts
    4

    Default

    Hi,

    first of all, thx for your reply

    Quote Originally Posted by DJC_Spring View Post
    Are you scanning the correct classpath for your services and are your services annotated too?
    yes the services I want to autowire are both annotated with @Service.
    the autowiring works in other places too, so I suppose it's not the problem that they aren't spring managed.

    here is the full code for my 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"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xsi:schemaLocation="
    	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
        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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:config.properties</value>
                </list>
            </property>
        </bean>
        
        <bean id="propertiesLocation" class="java.lang.String">
        	<constructor-arg value="config.properties"/>
        </bean>
            
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
       	 	<property name="driverClassName" value="${hibernate.connection.driver_class}"/>
    		<property name="url"             value="${hibernate.connection.url}"/>
    		<property name="username"        value="${hibernate.connection.username}"/>
                    <property name="password" 		value="${hibernate.connection.password}" />
                    <property name="initialSize"                               value="2"/>
                    <property name="maxActive"                                 value="30"/>
                    <property name="maxIdle"                                   value="5"/>
                    <property name="minIdle"                                   value= "1"/>
                    <property name="maxWait"                                   value= "7000"/>
                    <property name="removeAbandoned"                           value= "true"/>
                    <property name="removeAbandonedTimeout"                    value= "100"/>
                    <property name="logAbandoned"                              value= "true"/>
                    <property name="defaultAutoCommit"                         value= "false"/>
                    <!-- READ_COMMITTED -->
                    <property name="defaultTransactionIsolation"               value= "2"/>
                    <property name="validationQuery"                           value= "select 1"/>
                    <property name="testOnBorrow"                              value= "true"/>
                    <property name="testOnReturn"                              value= "false"/>
                    <property name="testWhileIdle"                             value= "true"/>
                    <property name="timeBetweenEvictionRunsMillis"             value= "30000"/>
                    <property name="numTestsPerEvictionRun"                    value= "3"/>
                    <property name="minEvictableIdleTimeMillis"                value= "25000"/>
                    <property name="poolPreparedStatements"                    value= "true"/>
                    <property name="maxOpenPreparedStatements"                 value= "50" />
                </bean>
        <bean id="entityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="persistence-CST" />
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter">
                <bean
    				class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="databasePlatform" value="${hibernate.dialect}" />
                    <property name="generateDdl" value="true" />
                    <property name="showSql" value="${hibernate.showSQL}" />
                </bean>
            </property>
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                    <prop key="hibernate.format_sql">true</prop>
                </props>
            </property>
        </bean>
    
    
        <bean
    		class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
            <description>
    			Needed so the @PersistenceContext annotation is
    			recognized.
            </description>
        </bean>
    
        <bean
    		class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor">
            <description>
    			Translates native data exceptions to Spring's
    			DataAccessException class.
            </description>
        </bean>
    
        <bean id="transactionManager"
    		class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
    
        <tx:annotation-driven />
    
        <context:component-scan base-package="..." />
        <context:annotation-config />
        
        <bean class="com.realdolmen.jpp.cst.web.login.ApplicationSecurityListener"/>
    	  
    	<aop:aspectj-autoproxy/>
    	<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
    	
    </beans>
    the web.xml doesn't contain any relevant information in regards to autowiring or spring
    Last edited by denertog; Jun 20th, 2012 at 04:06 AM.

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

    Default

    Judging by the name of the class you are creating new instances yourself, also it looks like this class is being used/stored in the session (due to the Serializable and transient fields). So make sure that you use the instance (or use the ApplicationContext as a factory) for your instances instead of creating new-ones.
    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
    Join Date
    Jun 2012
    Posts
    4

    Default

    Quote Originally Posted by Marten Deinum View Post
    Judging by the name of the class you are creating new instances yourself, also it looks like this class is being used/stored in the session (due to the Serializable and transient fields). So make sure that you use the instance (or use the ApplicationContext as a factory) for your instances instead of creating new-ones.
    I am indeed creating objects of this class myself.
    I need to create them dynamically to add them to a list.
    Is there any way to do this so Spring can autowire the services into the bean?
    Last edited by denertog; Jun 20th, 2012 at 06:33 AM.

  6. #6

    Default

    Does spring complain in the logs that the service methods cannot be autowired ? Are the service methods interfaces by any chance?

    Try to put a class in the same directory that the componet scan "scans" and see if you can autowire that?

  7. #7
    Join Date
    Jun 2012
    Posts
    4

    Default

    Quote Originally Posted by denertog View Post
    I am indeed creating objects of this class myself.
    I need to create them dynamically to add them to a list.
    Is there any way to do this so Spring can autowire the services into the bean?
    I solved the problem by getting the beans out of the context instead of creating them myself.

    Thanks to Marten Deinum for pointing this out

Tags for this Thread

Posting Permissions

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