2014-09-22 97 views
1

我使用以下MDB連接到WMQ自動裝配豆:MDB與彈簧豆不從另一個包

@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 {} 

以下彈簧豆被自動裝配在上述MDB使用攔截器註釋的一部分。 事件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> 

以下是核心應用-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> 

我自動裝配來自數據庫&的某些bean實例在MDB包中彈出jms xmls,但看起來像這些xml中的所有bean都未創建。你能否理解可能是什麼問題。 是否將MDB的autowire功能限制在同一個包中的spring xml以及是否在父級spring bean xml內完成任何其他導入?

示例:EventListener包含在com.abc.xyz包中。我在eventlistener類的com.abc.core包中自動裝配類A的實例。類A@Service,並且依次具有自動佈線依賴關係,例如com.abc.packB中的類B。所以當創建類A的實例時,我收到一個異常,說沒有找到類B定義。

+0

EventListener在哪個包中? – msknapp 2014-09-22 04:57:14

+0

它在com.abc.xyz和core xml中,數據庫和彈簧都在com.abc.core – 2014-09-22 05:03:09

+0

我沒有看到任何自動佈線材料(例如'@ Autowired'或'@Inject')。當使用'SpringBeanAutowiringInterceptor'時,需要一個名爲'beanRefContext.xml'的文件,它加載所有包含配置的xml文件。它不使用'ContextLoaderListener'。這在'SpringBeanAutowiringInterceptor'的[文檔](http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/ejb/interceptor/SpringBeanAutowiringInterceptor.html)中有解釋。 – 2014-09-22 05:36:33

回答

0

要解決此問題,您需要使用SpringBeanAutowiringSupport

由於MDB不是在Spring上下文中創建的,因此您必須使用SpringBeanAutowiringSupportprocessInjectionBasedOnCurrentContext(Object target)方法自動調用所需的bean或從SpringBeanAutowiringSupport繼承MDB。