带有spring bean的MDB不会从另一个包中自动装配bean

时间:2014-09-22 04:52:11

标签: java spring ioc-container message-driven-bean jboss-mdb

我正在使用以下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正在自动装配,作为上述MDB中使用的拦截器注释的一部分。 事件应用内-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>

以下是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>

我从数据库&amp;中自动装配某些bean实例。在MDB包中使用spring jms xmls,但看起来不会创建这些xmls中的所有bean。能否请您理解可能存在的问题。 MDB的autowire功能是否仅限于同一个包中的spring xmls,而且没有创建在父spring bean xml中完成的任何其他导入?

示例:EventListener位于com.abc.xyz个包中。我在eventlistener类的com.abc.core包中自动装配类A的实例。类A@Service,而B中依次为自动装配依赖,如com.abc.packB。因此,当创建类A的实例时,我会得到一个异常,说明没有找到类B定义。

1 个答案:

答案 0 :(得分:0)

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

因为MDB不是在Spring上下文中创建的,所以必须使用SpringBeanAutowiringSupportprocessInjectionBasedOnCurrentContext(Object target)方法自动装配所需的bean或从SpringBeanAutowiringSupport继承MDB。