Announcement Announcement Module
Collapse
No announcement yet.
Spring not fully injecting all beans (Servlet) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring not fully injecting all beans (Servlet)

    I was able to inject a bean in a servlet for a Database Access Object successfully but with a Business Object it had issues. The issue I had was it injected the Business Object fine but the Business Object's class members the DAOs were null. Inside the Business Object class the DAOs are @Autowired. Can anybody tell me why injecting a DAO is fine in a servlet but a BO has issues.

    applicationContext.xml
    Code:
    <?xml version="1.0" encoding="windows-1252"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:jpa="http://www.springframework.org/schema/data/jpa"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:flow="http://www.springframework.org/schema/webflow-config"
           xmlns:lang="http://www.springframework.org/schema/lang"
           xmlns:osgi="http://www.springframework.org/schema/osgi"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:cache="http://www.springframework.org/schema/cache"
    
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
              http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
              http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
              http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-3.0.xsd
              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
              http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
              http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
    ">
        <bean id="propertyConfigurer" 
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                  <property name="location">
                      <value>file:///configuration.properties</value>
                  </property>
        </bean>
        
        <bean id="config" class="com.foo.config.Config">
            <property name="customBinaryLocation" value="${custom.location}" />
            <property name="globalBinaryLocation" value="${global.location}" />
        </bean>
        
        <bean id="SetSessionData" class="com.foo.filters.SetSessionData"/>
        
        <!-- Directory to scan for repository classes -->
        <jpa:repositories
            base-package="com.foo.db.model.repository" />
        
        <cache:annotation-driven />
            <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/>
            <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
                        p:config-location="/WEB-INF/ehcache.xml"
                        p:shared="true"/>
       
        <context:component-scan base-package="com.foo.db.model.service" />
        <context:component-scan base-package="com.foo.data.access.objects" />
        <context:component-scan base-package="com.foo.business.objects" />
        <context:annotation-config />
       
        <bean id="converter" class="org.springframework.context.support.ConversionServiceFactoryBean">
            <property name="converters">
                <list>
                    <bean class="com.foo.common.DozerConverter">
                        <property name="mapper">
                            <bean class="org.dozer.DozerBeanMapper">
                                <property name="mappingFiles">
                                    <list>
                                        <value>dozerBeanMapping.xml</value>
                                    </list>
                                </property>
                            </bean>
                        </property>
                    </bean>
                </list>
            </property>
        </bean>
        
        <bean class="org.springframework.orm.jpa.JpaTransactionManager"
          id="transactionManager">
            <property name="entityManagerFactory"
                ref="entityManagerFactory" />
            <property name="jpaDialect">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
            </property>
        </bean>
    
        <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="packagesToScan" value="com.foo.db.model"  />
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="generateDdl" value="${hibernate.generate.ddl}" />
                    <property name="database" value="${hibernate.database}" />
                    <property name="showSql" value="${show.sql}" />
                </bean>
            </property>
            <property name="jpaProperties">
                <map>
                    <entry key="hibernate.default_schema" value="${default.schema}"/>
                    <entry key="hibernate.dialect" value="${jdbc.hibernate.dialect}"/>
                    <entry key="hibernate.id.new_generator_mappings" value="${new.generator.mappings}"/>
                    <entry key="hibernate.cache.region.factory_class" value="${hibernate.cache.factory.class}"/>
                    <entry key="hibernate.cache.use_second_level_cache" value="${hibernate.use.second.level.cache}"/>
                    <entry key="hibernate.cache.use_query_cache" value="${hibernate.use.query.cache}"/>
                    <entry key="net.sf.ehcache.configurationResourceName" value="${ehcache.config.file}"/>
                </map>
            </property>
        </bean>
    
        <bean id="dataSource"
          class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
            <property name="driverClassName" value="org.postgresql.Driver" />
            <property name="url" value="jdbc:postgresql://127.0.0.1:5432/foo" />
            <property name="username" value="postgres" />
            <property name="password" value="password" />
        </bean>
    </beans>
    Servlet
    Code:
    @Autowired
        private CustomerBO customerBo;
        private static final Logger LOG = Logger.getLogger(GetString.class.getCanonicalName());
        private static final String SESSION_USER_KEY_NAME = "user_id";
        private static final String SESSION_CUSTOMER_KEY_NAME = "customer_id";
    
        @Override
        public final void init(final ServletConfig servletConfig) throws ServletException {
            SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, servletConfig.getServletContext());
            super.init(servletConfig);
        }

  • #2
    If there is nothing injected you probably recreate an instance yourself somewhere, else you would get an exception stating that dependencies couldn't be injected. So I suggest you check your code.

    Comment


    • #3
      The codes are working for me.

      Comment


      • #4
        Originally posted by Marten Deinum View Post
        If there is nothing injected you probably recreate an instance yourself somewhere, else you would get an exception stating that dependencies couldn't be injected. So I suggest you check your code.
        I isolated the problem by trying another Business Object and it worked. The only difference with the one that is not working is I am using ehcache on methods and evicting some methods from the cache. Thanks for the replies I am going to work with a co-worker on this on Monday and will post the solution to the problem in case anyone has the same issue.

        Comment


        • #5
          There is no problem with injecting. The problem was browser caching. >
          I spent a day trying to solve this.

          Comment

          Working...
          X