带有@Autowired注释的Spring自动接线Bean返回null

时间:2017-03-29 19:42:49

标签: spring struts2 autowired entitymanager

我试图将我的ejbs迁移到Dao'这是我的实现: 构架 : Struts 2,Spring 4.3.7,Hibernate 4.3.10

Web.xml:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/appContext.xml</param-value>
    </context-param>
<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
<filter>
        <filter-name>struts2-prepare</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
    </filter>
  <filter>
      <filter-name>sitemesh</filter-name>
      <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
  </filter>
  <filter>
      <filter-name>struts2-execute</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
  </filter>
  <listener>
      <listener-class>com.abc.filter.AppContextListener</listener-class>
  </listener>

我的Spring配置:appContext.xml

<!-- data source and sessionFactory -->

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="ABCJNDI" />
</bean>


<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="abcPersistenceUnit" />
        <property name="dataSource" ref="dataSource" />
</bean>

<context:component-scan base-package="com.abc" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="employeeService" class="com.abc.daoimpl.EmployeeDAOImpl "></bean>

    <bean id="templateAction" class="com.abc.action.TemplateAction">
        <property name="employeeService" ref="employeeService" />
    </bean>


</beans>

我的行动类:

package com.abc.action

    public class TemplateAction extends ActionSupport implements Preparable {


        @Autowired
        public EmployeeDAO employeeService;
        /**
        *   All other stuff
        **/
    }



package com.abc.action
    public class Login extends TemplateAction{


//      @Autowired
//      public EmployeeDAO employeeService; // tried here also returns null;

        public String login(){

            employeeService.getEmployees(); // employeeService returns null here 

            return SUCCESS;
        }
    }

Listner类:AppContextListener:

package com.abc.filter
    public class AppContextListener{

        @Autowired
        public EmployeeDAO employeeService;

        public void contextInitialized(ServletContextEvent contextEvent) {
            applicationContext  = new ClassPathXmlApplicationContext("appContext.xml");

            employeeService = applicationContext.getBean(EmployeeDAO.class);

            employeeService.getEmployees();
        }

    }

员工类:

package com.abc.dao
    public interface EmployeeDAO  {
        public List<EmployeeEntity> getEmployees();
    }

    package com.abc.daoimpl
    @Repository("employeeService")
    public class EmployeeDAOImpl extends SessionAdapter implements EmployeeDAO{
        public List<EmployeeEntity> getEmployees(){
            Query selectQuery = getEntityManager().createNativeQuery("select * from ...");
        }       
    }



@Entity
    @Table(name="EMPLOYEE_TABLE")
    public class EmployeeEntity{
        /**
         * private fields and getters and setters
        **/
    }


public class SessionAdapter {

    @PersistenceContext(unitName="abcPersistenceUnit")
    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
}

问题在于TemplateActio n&amp;在Login我将自动装配的财产employeeService设为null。 但是在我的Listner类AppContextListener中,如果我尝试手动加载appContext.xml,那么它可以正常加载服务,我可以访问employeeService并获得结果。

0 个答案:

没有答案