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層調用配置文件java

  •  

    @Service
    public class TestService {
        private String buffermax;
        public TestService() {
            buffermax = StaticConfig.batchConfigstatic.buffer_max;
            System.out.println("size="+buffermax);
        }
    }
  • 啓動類web

  • @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,因此中間加了一層,親測可用,哈哈!!!
相關文章
相關標籤/搜索