开心比较堵 2024-02-01 17:10 采纳率: 73.2%
浏览 7

在application.properties写好了配置信息获取不到值?

使用springboot,在application.properties写好了配置信息,但是获取不到。报错如下;

img

相关代码:

package com.hewen.Config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("appConfig")
public class AppConfig {

    @Value("${oss.endpoint}")
    String endpoint ;
    @Value("${oss.accessKeyId}")
    String accessKeyId ;
    @Value("${oss.accessKeySecret}")
    String accessKeySecret;
    @Value("${oss.bucketName}")
    String bucketName;
    @Value("${oss.filedir}")
    String fileDir;

    public String getEndpoint() {
        return endpoint;
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public String getBucketName() {
        return bucketName;
    }

    public String getFileDir() {
        return fileDir;
    }
}
@Configuration
public class OssClientConfig {

    @Autowired
    private AppConfig appConfig;

    @Bean
    public OSSClient createOssClient() {
        //System.out.println("Endpoint: " + appConfig.getEndpoint()); // 打印以检查值
        return (OSSClient)new OSSClientBuilder().build(appConfig.getEndpoint(), appConfig.getAccessKeyId(), appConfig.getAccessKeySecret());
    }

}

@SpringBootApplication(scanBasePackages = "com.hewen")
@EnableTransactionManagement// 开启事务管理
@MapperScan("com.hewen.mappers")
public class GraRearApplication {

    public static void main(String[] args) {
        SpringApplication.run(GraRearApplication.class, args);
    }

}

  • 写回答

3条回答 默认 最新

  • 专家-郭老师 Java领域新星创作者 2024-02-01 17:22
    关注

    参考通义千问和郭老师的小迷弟雅思莫了-编写提供,主要是为了帮助您快速解决问题,如下回答,您可以参考下,如有问题,评论区留言。

    根据您提供的信息,问题可能出在Spring Boot无法正确注入application.properties中的属性值。以下是一些可能的原因和解决方法:

    1. 配置文件未加载:确保您的application.properties文件位于正确的目录下,对于Spring Boot项目,默认应该在src/main/resources目录下。

    2. 属性名错误:请检查application.properties中的属性名称是否与Java类中@Value注解的属性名称完全一致且大小写匹配,例如:

      # application.properties
      oss.endpoint=your-endpoint
      oss.accessKeyId=your-access-key-id
      oss.accessKeySecret=your-access-key-secret
      oss.bucketName=your-bucket-name
      oss.filedir=your-file-dir
      
    3. 自动装配问题:确认AppConfig类已被正确扫描并注册为Spring Bean。虽然您已经在类上使用了@Component注解,但还是建议在GraRearApplication主类上添加@EnableConfigurationProperties注解,并指定您的配置类:

      @SpringBootApplication(scanBasePackages = "com.hewen")
      @EnableTransactionManagement
      @MapperScan("com.hewen.mappers")
      @EnableConfigurationProperties(AppConfig.class) // 添加此行
      public class GraRearApplication {
          // ...
      }
      
    4. 依赖注入问题:在OssClientConfig类中,确保它也被正确地扫描为Spring Bean。如果依然有问题,尝试通过构造器注入方式初始化AppConfig

      @Configuration
      public class OssClientConfig {
      
          private final AppConfig appConfig;
      
          @Autowired
          public OssClientConfig(AppConfig appConfig) {
              this.appConfig = appConfig;
          }
      
          @Bean
          public OSSClient createOssClient() {
              System.out.println("Endpoint: " + appConfig.getEndpoint());
              return new OSSClientBuilder().build(appConfig.getEndpoint(), appConfig.getAccessKeyId(), appConfig.getAccessKeySecret());
          }
      }
      
    5. 重新构建项目:有时候IDE没有正确识别到更改,尝试清理并重新构建项目,或者重启IDE。

    评论

报告相同问题?

问题事件

  • 创建了问题 2月1日

悬赏问题

  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入
  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。