Search code examples
javaweblogic12cspring-jms

Getting BeanNotOfRequiredTypeException in Spring JMS module


I am using Spring jms module in my project.My aim is to create an executable jar file that will be triggered by other applications.I am configuring the jms in weblogic server,so,I am not going for Spring-Boot solution.

@Configuration
@ComponentScan(basePackages = "com.myapp")
@EnableJms
public class MyApp {
  public static void main(String[] args) {
    ApplicationContext context  = null;
    try {
          context = new AnnotationConfigApplicationContext(MyApp.class);
          MyBeanProvider beanProvider = context.getBean(MyBeanProvider.class);
          System.out.println(beanProvider);
    } catch (BeansException e) {
        e.printStackTrace();
    } finally{
        ((AbstractApplicationContext)context).close();
      }
    }
 }

@Component
public class MyBeanProvider {

  @Bean
  public JndiTemplate getJndiTemplate() {
     JndiTemplate jndiTemplate = new JndiTemplate();
     Properties properties = new Properties();
     properties.put("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
     properties.put("java.naming.provider.url", "t3://localhost:7001");
     jndiTemplate.setEnvironment(properties);
     return jndiTemplate;
   }

  @Bean(value="MyBeanConnectionFactory")
  public JndiObjectFactoryBean getConnectionFactory() {
      JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
    jndiObjectFactoryBean.setJndiTemplate(getJndiTemplate()); 
    jndiObjectFactoryBean.setJndiName("jms/MyAppConnectionFactory");
    return jndiObjectFactoryBean;
  }

  @Bean
  public JndiDestinationResolver getDestinationResolver() {
      JndiDestinationResolver jndiDestinationResolver = new JndiDestinationResolver();
      jndiDestinationResolver.setCache(true); 
      jndiDestinationResolver.setJndiTemplate(getJndiTemplate());
      return jndiDestinationResolver;
   }

   @Bean
   public JmsTemplate getJmsTemplate() {
      JmsTemplate jmsTemplate = new JmsTemplate();
      jmsTemplate.setConnectionFactory
      ((javax.jms.ConnectionFactory)getConnectionFactory().getObject());
            jmsTemplate.setDestinationResolver(getDestinationResolver()); 
            return jmsTemplate;
        }
      }
    }

I have a message receiver class as below:

 @Component
 public class MyBeanMessageReceiver {
  @JmsListener(destination = "jms/queue/MyAppMessageQueue",containerFactory="MyBeanConnectionFactory")
  public void receiveMessage(String message) {
    System.out.println("Received <" + message + ">");
  }
 }

During execution time I am running into following exception trace:

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyBeanMessageReceiver' defined in file : Initialization of bean failed; nested exception is  
 org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'MyBeanConnectionFactory' is expected to be of type  
'org.springframework.jms.config.JmsListenerContainerFactory' but was actually of type 'weblogic.jms.client.JMSXAConnectionFactory'

Solution

  • containerFactory="MyBeanConnectionFactory"

    The containerFactory property must reference a JmsListenerContainerFactory not a ConnectionFactory.

    The container factory, in turn, gets a reference to the connection factory.

    See the documentation.