Announcement Announcement Module
Collapse
No announcement yet.
autowiring issues Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      Hi,

      first of all, thx for your reply

      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, 04:06 AM.

      Comment


      • #4
        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.

        Comment


        • #5
          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, 06:33 AM.

          Comment


          • #6
            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?

            Comment


            • #7
              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

              Comment

              Working...
              X