You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Springboot Apache Camel连接IBM MQ时jmsConfiguration Bean创建失败

Spring Boot + Apache Camel 连接IBM MQ时Bean创建失败问题

我想用Spring Boot + Apache Camel应用从IBM MQ获取消息,配置JMS连接时遇到Bean创建错误,不清楚问题出在哪。

JmsConfiguration代码

import com.ibm.mq.MQEnvironment;
import com.ibm.msg.client.wmq.common.CommonConstants;
//import org.apache.camel.component.jms.JmsComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import org.springframework.jms.annotation.EnableJms;
import com.ibm.msg.client.wmq.WMQConstants;

import javax.net.ssl.*;
import java.security.KeyStore;


@EnableJms
@Configuration
public class JmsConfiguration {

    private static final Logger LOG = LoggerFactory.getLogger(JmsComponent.class);
    private String host = "xxx";
    private int port = 4510;
    private String queueManager = "xxx";
    private String channel = "xxx";
    //private String username = "";
    //private String password = "";
    private String sslCipherSuite = "TLS_RSA_WITH_AES_256_CBC_SHA256";

    @Value("${keyStore}")
    private String keyStore;

    @Value("${keyStorePassword}")
    private String keyStorePassword;

    @Value("${trustStore}")
    private String trustStore;

    @Value("${trustStorepassword}")
    private String trustStorePassword;


    @Bean
    public JmsComponent mq() {
        JmsComponent jmsComponent = new JmsComponent();
        jmsComponent.setConnectionFactory(mqQueueConnectionFactory());
        return jmsComponent;
    }

    @Bean
    public MQQueueConnectionFactory mqQueueConnectionFactory() {
        MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
        mqQueueConnectionFactory.setHostName(host);
        try {
            //Load trust store
            //System.setProperty("javax.net.ssl.trustStore", trustStore);
            //System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
            //Load keystore
            //System.setProperty("javax.net.ssl.keyStore", keyStore);
            //System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);

            //keystore fetch
            String keystoreKey = "${keyStorePassword}";
            KeyStore keyStore = KeyStore.getInstance("JKS");
            keyStore.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("${keyStore}"),
                    keystoreKey.toCharArray());

            // Create key manager
            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
            keyManagerFactory.init(keyStore, keystoreKey.toCharArray());
            KeyManager[] km = keyManagerFactory.getKeyManagers();

            // Create trust manager
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
            trustManagerFactory.init(keyStore);
            TrustManager[] tm = trustManagerFactory.getTrustManagers();

            // Initialize SSLContext
            SSLContext sslContext = SSLContext.getInstance("TLSv1");
            sslContext.init(km, tm, null);
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            mqQueueConnectionFactory.setPort(port);
            mqQueueConnectionFactory.setChannel(channel);
            mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
            mqQueueConnectionFactory.setObjectProperty(WMQConstants.WMQ_SSL_SOCKET_FACTORY, sslSocketFactory);
            mqQueueConnectionFactory.setSSLCipherSuite(sslCipherSuite);
            mqQueueConnectionFactory.setIntProperty(CommonConstants.WMQ_CONNECTION_MODE, CommonConstants.WMQ_CM_CLIENT);
            mqQueueConnectionFactory.setTransportType(CommonConstants.WMQ_CM_BINDINGS);

            MQEnvironment.sslSocketFactory = sslSocketFactory;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return  mqQueueConnectionFactory;
    }
}

堆栈跟踪信息

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jmsConfiguration': Injection of autowired dependencies failed
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:489) ~[spring-beans-6.0.10.jar!/:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416) ~[spring-beans-6.0.10.jar!/:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) ~[spring-beans-6.0.10.jar!/:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.10.jar!/:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.10.jar!/:6.0.10]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.10.jar!/:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.10.jar!/:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.10.jar!/:6.0.10]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.10.jar!/:6.0.10]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:941) ~[spring-context-6.0.10.jar!/:6.0.10]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.10.jar!/:6.0.10]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[spring-boot-3.0.8.jar!/:3.0.8]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-3.0.8.jar!/:3.0.8]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:310) ~[spring-boot-3.0.8.jar!/:3.0.8]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) ~[spring-boot-3.0.8.jar!/:3.0.8]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) ~[spring-boot-3.0.8.jar!/:3.0.8]
at nl.rabobank.mqputter.MqPutterApplication.main(MqPutterApplication.java:10) ~[classes!/:0.0.1-SNAPSHOT]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[app.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:95) ~[app.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[app.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65) ~[app.jar:0.0.1-SNAPSHOT]
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'keyStore' in value "${keyStore}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180) ~[spring-core-6.0.10.jar!/:6.0.10]
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-6.0.10.jar!/:6.0.10]
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239) ~[spring-core-6.0.10.jar!/:6.0.10]
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-6.0.10.jar!/:6.0.10]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:191) ~[spring-context-6.0.10.jar!/:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:920) ~[spring-beans-6.0.10.jar!/:6.0.10]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1358) ~[spring-beans-6.0.10.jar!/:6.0.10]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) ~[spring-beans-6.0.10.jar!/:6.0.10]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:711) ~[spring-beans-6.0.10.jar!/:6.0.10]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:694) ~[spring-beans-6.0.10.jar!/:6.0.10]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145) ~[spring-beans-6.0.10.jar!/:6.0.10]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:483) ~[spring-beans-6.0.10.jar!/:6.0.10]
... 24 common frames omitted

问题分析与修复方案

核心问题

堆栈跟踪明确显示:Could not resolve placeholder 'keyStore' in value "${keyStore}",说明Spring无法找到keyStore对应的配置属性,同时代码中还有几处硬编码占位符、配置逻辑错误。

修复步骤

  1. 补充配置文件属性
    在application.properties(或yml)中添加缺失的配置项:

    keyStore=classpath:你的密钥库文件名.jks
    keyStorePassword=密钥库密码
    trustStore=classpath:你的信任库文件名.jks
    trustStorepassword=信任库密码
    
  2. 修正硬编码占位符
    代码中直接写了"${keyStorePassword}"和"${keyStore}",这会导致Spring无法解析,替换为注入的成员变量:

    // 错误代码
    // String keystoreKey = "${keyStorePassword}";
    // keyStore.load(..., "${keyStore}");
    
    // 修正后
    String keystoreKey = this.keyStorePassword;
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(this.keyStore),
            keystoreKey.toCharArray());
    
  3. 修复信任管理器初始化逻辑
    当前代码用密钥库初始化信任管理器,应该改用信任库:

    // 加载信任库
    KeyStore trustStore = KeyStore.getInstance("JKS");
    trustStore.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(this.trustStore),
            this.trustStorePassword.toCharArray());
    
    // 初始化信任管理器
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
    trustManagerFactory.init(trustStore);
    
  4. 恢复JmsComponent导入
    取消注释import org.apache.camel.component.jms.JmsComponent;,否则Logger初始化会报错。

  5. 修正TransportType重复设置
    代码中重复设置了连接模式,保留客户端模式即可:

    // 保留这一行,删除后面的WMQ_CM_BINDINGS设置
    mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
    

内容的提问来源于stack exchange,提问作者Beerus239

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 01:19:53