简体   繁体   English

带有Spring Bean的MDB不会自动将另一个软件包中的Bean接线

[英]MDB with spring beans doesn't autowire beans from another package

I am using following MDB to connect to WMQ: 我正在使用以下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 {}

Following spring beans are being autowired as part of the interceptor annotation used in the above MDB. 作为上述MDB中使用的拦截器注释的一部分,将自动连接以下Spring bean。 event-app-config.xml: 事件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: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>

Following is the core-app-config.xml: 以下是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>

Main xml beanRefContext.xml: 主要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>

I am autowiring certain bean instances from database & spring jms xmls in the MDB package but looks like all the beans inside these xmls are not created. 我正在从MDB软件包中的数据库和spring jms xml自动装配某些bean实例,但看起来好像未创建这些xml中的所有bean。 Can you please understand what can be the issue. 您能否理解可能是什么问题。 Does the autowire functionality of the MDB restricted to the spring xmls in the same package and any other imports being done inside parent spring bean xml is not created? 是否没有创建MDB的自动装配功能,使其局限于同一软件包中的spring xml,并且未创建在父spring bean xml中进行的任何其他导入?

Example: EventListener is in com.abc.xyz package. 示例: EventListenercom.abc.xyz软件包中。 I am autowiring instance of class A from com.abc.core package in eventlistener class. 我正在从eventlistener类的com.abc.core包中自动装配AA实例。 Class A is @Service and that in turn has autowired dependency say Class B which in com.abc.packB . A@Service并且反过来又自动装配依赖性说类Bcom.abc.packB So when the instance of class A is created I get an exception saying that no Class B definition is found. 因此,当创建类A的实例时,我得到一个异常,指出未找到类B定义。

To solve this problem you need to use SpringBeanAutowiringSupport . 要解决此问题,您需要使用SpringBeanAutowiringSupport

Because MDB created not in Spring context, you must autowire beans that you need with SpringBeanAutowiringSupportprocessInjectionBasedOnCurrentContext(Object target) method or inherit MDB from SpringBeanAutowiringSupport . 因为MDB不是在Spring上下文中创建的,所以必须使用SpringBeanAutowiringSupportprocessInjectionBasedOnCurrentContext(Object target)方法自动装配所需的bean,或者从SpringBeanAutowiringSupport继承MDB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM