gpt4 book ai didi

java - 带有 spring bean 的 MDB 不会从另一个包 Autowiring bean

转载 作者:太空宇宙 更新时间:2023-11-04 14:32:31 25 4
gpt4 key购买 nike

我正在使用以下 MDB 连接到 WMQ:

@MessageDriven(name = "EventListener", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "ABC"),
@ActivationConfigProperty(propertyName = "hostName", propertyValue = "ABC"),
@ActivationConfigProperty(propertyName = "port", propertyValue = "ABC"),
@ActivationConfigProperty(propertyName = "channel", propertyValue = "ABC"),
@ActivationConfigProperty(propertyName = "queueManager", propertyValue = "ABC"),
@ActivationConfigProperty(propertyName = "sslCipherSuite", propertyValue = "ABC"),
@ActivationConfigProperty(propertyName = "transportType", propertyValue = "CLIENT") })
@ResourceAdapter(value = "wmq.jmsra.rar")
@TransactionManagement(value = TransactionManagementType.CONTAINER)
@TransactionAttribute(value = TransactionAttributeType.REQUIRED)
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class EventListener implements MessageListener {}

以下 spring bean 被 Autowiring 为上述 MDB 中使用的拦截器注释的一部分。事件应用程序配置.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="com.abc" />
<context:annotation-config/>
<import resource="classpath:core-app-config.xml" /> //This is another package

</beans>

以下是 core-app-config.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">

<import resource="classpath:database-config.xml" />
<import resource="classpath:spring-jms-config.xml" />

</beans>

主要 xml beanRefContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config/>
<context:spring-configured/>

<bean id="beanRefFactory" class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="classpath*:event-app-config.xml" />
</bean>
</beans>

我正在从MDB包中的数据库和spring jms xml Autowiring 某些bean实例,但看起来这些xml中的所有bean都没有创建。您能理解可能是什么问题吗?MDB 的 Autowiring 功能是否仅限于同一包中的 spring xml,并且不会创建在父 spring bean xml 内完成的任何其他导入?

示例:EventListener 位于 com.abc.xyz 包中。我正在 Autowiring eventlistener 类中 com.abc.core 包中的类 A 的实例。类 A@Service ,而它又具有 Autowiring 依赖项,例如类 B ,位于 com.abc.packB 中。因此,当创建类 A 的实例时,我收到一个异常,指出未找到类 B 定义。

最佳答案

要解决此问题,您需要使用 SpringBeanAutowiringSupport .

由于 MDB 不是在 Spring 上下文中创建的,因此您必须使用 SpringBeanAutowiringSupportprocessInjectionBasedOnCurrentContext(Object target) 方法 Autowiring 所需的 bean,或者从 SpringBeanAutowiringSupport 继承 MDB。

关于java - 带有 spring bean 的 MDB 不会从另一个包 Autowiring bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25966888/

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