Spring @Autowired注入不起作用:DAO始终为null

时间:2015-04-07 08:53:17

标签: java spring spring-mvc autowired struts-1

我正在开发一个EAR,其中由Spring处理Struts和bean处理的操作。此EAR包含3个.jar文件,公共文件的引用(DS / US / DAO)和.war myProjectWeb

代码:

Struts动作(在 myProjectWeb 中):

public class MyAction extends DispatchAction {

    private IMyPreferencesDS myPreferences;

    protected ActionForward executeAction(ActionMapping mapping, ActionForm form, 
        HttpServletRequest request, HttpServletResponse response) throws Exception {
        // newValue is got from my form
        myPreferences.updatePreferences(newValue);
    }
}

不同的DS(在 myProject-commons 中):

IMyPreferencesDS

public interface IMyPreferencesDS extends IAbstractDSGeneric<MyDTO> {

    void updatePreferences(String newValue) throws JrafDomainException;

}

MyPreferencesDS

public class MyPreferencesDS implements IMyPreferencesDS {

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

    @Autowired
    @Qualifier("myPreferencesDAOBean")
    private IMyPreferencesDAO mainDao;

    @Transactional(rollbackFor = JrafDomainRollbackException.class, 
        noRollbackFor = JrafDomainNoRollbackException.class)
    public void updatePreferences(String newValue) throws JrafDomainException {

        mainDao.setNewPreferencesValue(newValue);

    }
}

IMyPreferencesDAO

public interface IMyPreferencesDAO extends IAbstractDAOGeneric<MyEntity> {

    void setNewPreferencesValue(String newValue) throws JrafDaoException;

}

MyPreferencesDAO

public class MyPreferencesDAO extends AbstractDAOGenericImpl<MyEntity> implements IMyPreferencesDAO {

    public void setNewPreferencesValue(String newValue) throws JrafDaoException {
        StringBuilder strQuery = new StringBuilder();
        strQuery.append("update MYBASE.MYTABLE ");
        strQuery.append("set");
        strQuery.append(" PREFS=:newValue, ");

        final Query myQuery = getEntityManager().createNativeQuery(strQuery.toString(), MyEntity.class);
        myQuery.setParameter("newValue", newValue);

        try {
            return myQuery.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

配置:

myProjectWeb 中:

struts-config

<form-bean name="MyForm" type="com.my.MyForm"/>
<action input="/media/modificationPopup.jsp" name="MyForm" path="/media/prefModif" 
    scope="request" type="org.springframework.web.struts.DelegatingActionProxy" 
    validate="true"/>

actions-dependencies

<bean name="/media/prefModif" class="com.my.action.MyAction" scope="singleton">
    <property name="myPreferences" ref="myPreferencesDS" />
</bean> 

application-context-spring

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-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/jee 
    http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

    <!-- Enterprise layer's dependencies -->
    <import resource="classpath:ioc/0-ref-commons-enterpriselayer-dependencies.xml" />

    <bean id="springLocator" class="com.afklm.jraf.bootstrap.locator.SpringLocator"></bean>

    <bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="persistenceXmlLocations">
            <list>
                <value>classpath*:META-INF/persistence-web.xml</value>
            </list>
        </property>
    </bean>

    <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitManager" ref="persistenceUnitManager" />
        <property name="persistenceUnitName" value="myPersistenceUnit" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="myEmf" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <context:component-scan base-package="com.my.action" /> 
</beans>

0-ref-commons-enterpriselayer-dependencies.xml 包含在 commons jar中:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <import resource="1-ref-commons-ds-dependencies.xml"/>
    <import resource="2-ref-commons-us-dependencies.xml"/>
    <import resource="3-ref-commons-daos-dependencies.xml"/>
</beans>

它包含请求的DS和DAO,如下所示:

1-ref-commons-ds-dependencies.xml

<!-- MyPreferencesDS Component -->
<bean id="myPreferencesDS" class="com.commons.ds.MyPreferencesDS" scope="prototype">
</bean>

3-ref-commons-daos-dependencies.xml

<!-- MyPreferencesDAO Component -->
<bean id="myPreferencesDAOBean" class="com.commons.daos.MyPreferencesDAO" scope="prototype">
</bean>

所有的库都在我的EAR中:

enter image description here

并导入我的网站.war:

enter image description here

我的 EAR 的模块组装:

enter image description here

好的,这是......但是当我尝试在 MyPreferencesDS 中调用此行时:

mainDao.setNewPreferencesValue(newValue);

mainDao 始终为空,我得到NullPointerException ...似乎@Autowired注射不能在那里工作。

感谢您的帮助......

2 个答案:

答案 0 :(得分:1)

Ypu混合使用@Autowiring并通过xml直接分配。

您的myAction没有自动配送首选项。你通过ref

设置它
<bean name="/media/prefModif" class="com.my.action.MyAction" scope="singleton">
    <property name="myPreferences" ref="myPreferencesDS" />
</bean> 

所以通过ref。

以相同的方式定义dao ref
<bean id="myPreferencesDS" class="com.commons.ds.MyPreferencesDS" scope="prototype">
    <property name="mainDao" ref="myPreferencesDAOBean" />
</bean>

或使用@Autowire将首选项注入Action

答案 1 :(得分:0)

您没有扫描&#34; com.commons&#34;包。自&#34; com.commons.ds.MyPreferencesDS&#34;在那个包中,spring甚至不需要自动装配这个包的bean。您可以尝试application-context-spring中的以下行:

<context:component-scan base-package="com.my.action, com.commons" />

如果你不想要&#34; com.commons&#34;下的所有包裹?并且更具体地说明扫描了哪些包并列出所有包:

<context:component-scan base-package="com.my.action, com.commons.ds, com.commons.daos" />