springboot加载application.yml文件null

  • 话不多说,直接上代码
  • 本人项目为maven项目
  • 以下是项目结构
  • pom.xml文件
  • <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>testSpringBootApplication</groupId>
        <artifactId>testSpringBootApplication</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--导入配置文件处理器,配置文件进行绑定就会有提示-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
  • 配置文件:application.yml
    batch:
      buffer:
         size: 100
         max: 60
         sleep: 10
      excute:
          pool: 50
  • 写一个对应次配置文件的类
  • @Component
    public class BatchConfig {
        @Value("${batch.buffer.size}")
        public String buffer_size;
        @Value("${batch.excute.pool}")
        public String excute_pool;
        @Value("${batch.buffer.max}")
        public String buffer_max;
        @Value("${batch.buffer.sleep}")
        public String buffer_sleep;
    }
  • 因为使用注解autowired一直创建对象失败,所以只能写一个静态类来自动加载BatchConfig
  • @Component
    public class StaticConfig {
        public static BatchConfig batchConfigstatic;
        @Autowired
        private BatchConfig batchConfig;
        @PostConstruct
        public void initStaticDao() {
            batchConfigstatic = this.batchConfig;
        }
    }
  • service层调用配置文件

  •  

    @Service
    public class TestService {
        private String buffermax;
        public TestService() {
            buffermax = StaticConfig.batchConfigstatic.buffer_max;
            System.out.println("size="+buffermax);
        }
    }
  • 启动类

  • @SpringBootApplication
    @EnableConfigurationProperties
    @EnableAutoConfiguration
    @ComponentScan("com.hywc")
    public class Application {
        public static void main(String[] args) {
            // 启动springboot
            //ApplicationContext applicationContext =
            SpringApplication.run(Application.class, args);
        }
    }
  • 运行结果
  •  
  • 因之前@Value以及@ConfigurationProperties形式均为null,所以中间加了一层,亲测可用,哈哈!!!

转载于:https://www.cnblogs.com/lxyuuuuu/p/10949960.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值