在 Spring Boot 应用程序中禁用 Spring JMS 自动配置

Posted

技术标签:

【中文标题】在 Spring Boot 应用程序中禁用 Spring JMS 自动配置【英文标题】:Disabling Spring JMS Auto configuration in Spring Boot Application 【发布时间】:2016-01-21 09:09:17 【问题描述】:

在我的 Spring Boot 应用程序中,我配置了两个不同的 MQQueueConnectionFactory 实例(不同的 id),因为它是应用程序的需要。为此,我添加了 ibm 客户端 jars。

我还在我的代码中添加了 spring-jms 依赖项,因为我想要 JmsTemplate 等类。添加此依赖后,JmsAutoConfiguration 在类路径中找到 JmsTemplate 并尝试配置 bean。在这个过程中,它尝试注入 ConnectionFactory 类型的 bean,这是代码失败的地方,我开始收到错误。下面是来自 JmsAutoConfiguration 的代码

@Configuration
@ConditionalOnClass(JmsTemplate.class)
@ConditionalOnBean(ConnectionFactory.class)
@EnableConfigurationProperties(JmsProperties.class)
@Import(JmsAnnotationDrivenConfiguration.class)
public class JmsAutoConfiguration 

    @Autowired
    private JmsProperties properties;

    @Autowired
    private ConnectionFactory connectionFactory;

    @Autowired(required = false)
    private DestinationResolver destinationResolver;

我是否有机会关闭 Spring Boot 的 JmsAutoconfiguration 功能?如果不是,那么有什么替代解决方案?

【问题讨论】:

注意未来 Apache Tomcat Java WAR 部署查看器,如果您在 catalina.out 中看到 Caused by: java.lang.NoSuchMethodException: org.springframework.jms.annotation.JmsBootstrapConfiguration$$EnhancerBySpringCGLIB$$9f8a2d0.CGLIB$SET_THREAD_CALLBACKS([Lorg.springframework.cglib.proxy.Callback;) 错误,您可以在下面尝试其中一些答案。 【参考方案1】:

您可以将要禁用的自动配置添加到 SpringBootApplication 注解中:

@SpringBootApplication(exclude = JmsAutoConfiguration.class)

【讨论】:

或:@EnableAutoConfiguration(exclude = JmsAutoConfiguration.class)【参考方案2】:

如果想通过属性(在本例中为 application.yml)来控制它,那么你可以这样做。

spring:
  autoconfigure:
    exclude: org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration

【讨论】:

【参考方案3】:

仅供参考,使用它来禁用 ActiveMQ

@SpringBootApplication(exclude = ActiveMQAutoConfiguration.class)

【讨论】:

【参考方案4】:

在我的情况下,它在排除两个类后起作用:

 @EnableAutoConfiguration(exclude=JmsAutoConfiguration.class, ActiveMQAutoConfiguration.class)

【讨论】:

以上是关于在 Spring Boot 应用程序中禁用 Spring JMS 自动配置的主要内容,如果未能解决你的问题,请参考以下文章

在 Spring Boot 应用程序的生产环境中禁用 GraphiQL Playground

在spring boot / spring data elasticsearch中禁用自动索引创建

无法在 Spring Boot 中禁用安全性 [重复]

如何在 Spring Boot 应用程序中禁用/忽略 @PreAuthorize

Spring boot 和 Spring Actuator - 禁用安全性

如何在 Spring Boot 中禁用或覆盖 RequestCacheAwareFilter