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,所以中间加了一层,亲测可用,哈哈!!!

猜你喜欢

转载自www.cnblogs.com/lxyuuuuu/p/10949960.html