[英]Spring @Autowired injection doesn't work : DAO is always null
我正在研究EAR,并在Spring之前由Struts和bean处理动作。 该EAR包含3个.jar
文件,commons文件(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中:
并导入到我的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.