1

I am using following MDB to connect to 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. event-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:

<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:

<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. 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?

Example: EventListener is in com.abc.xyz package. I am autowiring instance of class A from com.abc.core package in eventlistener class. Class A is @Service and that in turn has autowired dependency say Class B which in com.abc.packB. So when the instance of class A is created I get an exception saying that no Class B definition is found.

Aleksandr Podkutin
  • 2,532
  • 1
  • 20
  • 31
Nishant Prabhu
  • 85
  • 1
  • 2
  • 11
  • what package is EventListener in? – msknapp Sep 22 '14 at 04:57
  • its in com.abc.xyz and core xml,database and spring are in com.abc.core – Nishant Prabhu Sep 22 '14 at 05:03
  • I don't see any auto wiring material (no `@Autowired` or `@Inject` for instance). When using the `SpringBeanAutowiringInterceptor` a file called `beanRefContext.xml` is needed which loads all the xml files containing the configuration. It doesn't use the `ContextLoaderListener`. Which is explained in [the documentation](http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/ejb/interceptor/SpringBeanAutowiringInterceptor.html) of the `SpringBeanAutowiringInterceptor`. – M. Deinum Sep 22 '14 at 05:36
  • Yes you are right. I have created a ref bean xml and used classpath xml context to refer the beans. But for importing xmls from another package I am simply using import. Do I need to explicity add them to classpath . If yes how can I achieve that. – Nishant Prabhu Sep 22 '14 at 05:39
  • I don't really get this comment. If an xml file isn't in the root you need to specify the package it is in. This `classpath:core-app-config.xml` will load a file from the root of the class path if it is in a package add the package it isn't going to scan the class path to find the file. Also what is in your `beanRefContext.xml` file? Or is it already posted (then it isn't clear which one it is). – M. Deinum Sep 22 '14 at 09:25
  • Added beanRefContext xml //beanRefContext.xml to main thread – Nishant Prabhu Sep 22 '14 at 09:42
  • Did you find a solution for this? I am also getting a NullPointerException in wiring a Spring Bean to the MDB. – Chinthaka Dharmasiri Dec 11 '14 at 05:28
  • No. We changed the implementation – Nishant Prabhu Dec 12 '14 at 10:43

1 Answers1

0

To solve this problem you need to use 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.

Aleksandr Podkutin
  • 2,532
  • 1
  • 20
  • 31