gpt4 book ai didi

java - Spring @Autowired 注入(inject)不起作用 : DAO is always null

转载 作者:行者123 更新时间:2023-11-30 08:17:14 32 4
gpt4 key购买 nike

我正在开发一个 EAR,其操作由 Struts 处理,bean 由 Spring 处理。该 EAR 包括 3 个 .jar 文件、公共(public)文件 (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"/>

操作依赖项:

<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-dependency.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-dependency.xml

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

3-ref-commons-daos-dependency.xml

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

所有库都在我的 EAR 中:

enter image description here

并导入到我的 Web .war 中:

enter image description here

我的EAR模块组装:

enter image description here

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

mainDao.setNewPreferencesValue(newValue);

ma​​inDao 始终为 null,并且我收到 NullPointerException... 似乎 @Autowired 注入(inject)在那里不起作用。

感谢您的帮助...

最佳答案

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将首选项注入(inject)到Action中

关于java - Spring @Autowired 注入(inject)不起作用 : DAO is always null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29487543/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com