<?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>
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; }
@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); } }