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

Spring Boot无法识别自定义Java类,XML引用Bean方法报错求助

问题描述

我是Spring Boot新手,尝试在Spring Boot XML配置文件中使用Java类的方法返回值填充属性,但该类似乎不被Spring Boot加载器识别,推测可能遗漏了注解,现寻求帮助。

Java类代码

public class AppMqttConfig {
    
    @Bean
    public SSLSocketFactory getSocketFactory()
            throws Exception {
            
            ResourceLoader resourceLoader = new DefaultResourceLoader();

            String caCrtFile = resourceLoader.getResource("classpath:/non-prod/AmazonRootCA1.pem").getFile().getAbsolutePath();

            String crtFile = resourceLoader.getResource("classpath:/non-prod/certificate.pem.crt").getFile().getAbsolutePath();

            String keyFile = resourceLoader.getResource("classpath:/non-prod/private.pem.key").getFile().getAbsolutePath();
            
            Security.addProvider(new BouncyCastleProvider());
            // 注意:此处缺少SSLSocketFactory的返回逻辑
    }
}

XML配置引用部分

<bean id="pahoMqttClientFactory"
          class="org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory">
        <property name="connectionOptions">
            <bean class="org.eclipse.paho.client.mqttv3.MqttConnectOptions">
                <property name="serverURIs">
                    <list>
                        <value>${mqtt.broker.url}</value>
                    </list>
                </property>
                <property name="connectionTimeout" value="${mqtt.client.connectionTimeout}" />
                <property name="keepAliveInterval" value="${mqtt.client.keepAliveInterval}" />
                <property name="cleanSession" value="${mqtt.session.clean}" />
                <property name="socketFactory" value="#{AppMqttConfig.getSocketFactory()}" />
            </bean>
        </property>
    </bean>

错误信息

属性或字段AppMqttConfig在org.springframework.beans.factory.config.BeanExpressionContext类型的对象上找不到 - 可能不是公共的或无效的?

解决步骤
  • 给配置类添加Spring注解:在AppMqttConfig类上添加@Configuration注解,让Spring Boot的组件扫描机制识别并加载这个类,将其纳入Spring容器管理。修改后的类开头如下:

    @Configuration
    public class AppMqttConfig {
        // ... 原有方法
    }
    
  • 修正XML中的EL表达式:@Bean标注的getSocketFactory是实例方法,不能直接通过类名调用。需要通过Spring容器中的bean实例调用,有两种方式:

    1. 使用方法名(默认bean名称)调用:
      <property name="socketFactory" value="#{getSocketFactory()}" />
      
    2. 使用类名首字母小写的bean名称调用:
      <property name="socketFactory" value="#{appMqttConfig.getSocketFactory()}" />
      
  • 补全方法的返回逻辑:当前getSocketFactory方法没有返回SSLSocketFactory实例,需要补充代码生成并返回对应对象(示例需根据实际SSL配置逻辑完善):

    // ... 原有代码
    Security.addProvider(new BouncyCastleProvider());
    
    // 示例:生成SSLSocketFactory的核心逻辑
    SSLContext sslContext = SSLContext.getInstance("TLS");
    // 此处补充加载证书、构建KeyManagerFactory、TrustManagerFactory等逻辑
    sslContext.init(...);
    return sslContext.getSocketFactory();
    
  • 优化ResourceLoader的使用:不要手动创建DefaultResourceLoader,通过@Autowired注入Spring容器中的ResourceLoader实例,符合Spring依赖注入规范:

    @Configuration
    public class AppMqttConfig {
    
        @Autowired
        private ResourceLoader resourceLoader;
    
        @Bean
        public SSLSocketFactory getSocketFactory() throws Exception {
            String caCrtFile = resourceLoader.getResource("classpath:/non-prod/AmazonRootCA1.pem").getFile().getAbsolutePath();
            // ... 后续代码
        }
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 05:45:05