在 Spring JMS 模块中获取 BeanNotOfRequiredTypeException



我正在我的项目中使用 Spring jms 模块。我的目标是创建一个可执行的jar文件,该文件将由其他应用程序触发。我正在网络逻辑服务器中配置 jms,所以,我不打算使用弹簧引导解决方案。

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

我有一个消息接收器类,如下所示:

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

在执行期间,我遇到了以下异常跟踪:

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'

containerFactory="MyBeanConnectionFactory"

containerFactory属性必须引用JmsListenerContainerFactory而不是ConnectionFactory

反过来,容器工厂获取对连接工厂的引用。

请参阅文档。

最新更新