簡體   English   中英

Spring @Autowired注入無效:DAO始終為null

[英]Spring @Autowired injection doesn't work : DAO is always null

我正在研究EAR,並在Spring之前由Struts和bean處理動作。 該EAR包含3個.jar文件,commons文件(DS / US / DAO)的引用以及.warmyProjectWeb

代碼:

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中:

在此處輸入圖片說明

並導入到我的Web .war中:

在此處輸入圖片說明

我的EAR的模塊組裝如下:

在此處輸入圖片說明

好的,這是...但是,當我嘗試在MyPreferencesDS中調用此行時

mainDao.setNewPreferencesValue(newValue);

mainDao始終為null,並且我收到NullPointerException ...似乎@Autowired注入在那里不起作用。

謝謝你的幫助...

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中

您沒有在掃描“ com.commons ”軟件包。 由於“ com.commons.ds.MyPreferencesDS ”位於該軟件包中,因此spring甚至不需要為其自動com.commons.ds.MyPreferencesDS該軟件包的bean。 您可能想在application-context-spring嘗試以下行:

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

如果您不希望“ com.commons ”下的所有軟件包,並且更具體地說明要掃描的軟件包並列出所有軟件包:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM