1.引用XML文件配置
2.引入多个@Configuration 配置类
3.引用自定义properties
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
- </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
- file.upload.stor-path=E:/test/
file.upload.stor-path=E:/test/(2)直接使用@Value注解方式,具体代码如下:
- package com.chengli.springboot.helloworld;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- @SpringBootApplication
- public class SampleController {
- @Value(value = "${file.upload.stor-path}")
- private String storPath;
- @RequestMapping("/")
- String home() {
- return "Hello World! file.upload.stor-path为:" + storPath;
- }
- public static void main(String[] args) throws Exception {
- SpringApplication springApplication = new SpringApplication(SampleController.class);
- springApplication.run(args);
- }
- }
package com.chengli.springboot.helloworld;第二种:import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@SpringBootApplication
public class SampleController {
@Value(value = "${file.upload.stor-path}")
private String storPath;@RequestMapping("/") String home() { return "Hello World! file.upload.stor-path为:" + storPath; } public static void main(String[] args) throws Exception { SpringApplication springApplication = new SpringApplication(SampleController.class); springApplication.run(args); }
}
- package com.chengli.springboot.helloworld;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
- @Component
- @ConfigurationProperties(prefix = "file.upload")
- public class FileUploadProperties {
- private String storPath;
- public String getStorPath() {
- return storPath;
- }
- public void setStorPath(String storPath) {
- this.storPath = storPath;
- }
- }
package com.chengli.springboot.helloworld;(2)入口启动类代码如下:import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "file.upload")
public class FileUploadProperties {
private String storPath;public String getStorPath() { return storPath; } public void setStorPath(String storPath) { this.storPath = storPath; }
}
- package com.chengli.springboot.helloworld;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- @SpringBootApplication
- public class SampleController {
- @Autowired
- private FileUploadProperties fileUploadProperties;
- @RequestMapping("/")
- String home() {
- return "Hello World! file.upload.stor-path为:" + fileUploadProperties.getStorPath();
- }
- public static void main(String[] args) throws Exception {
- SpringApplication springApplication = new SpringApplication(SampleController.class);
- springApplication.run(args);
- }
- }
package com.chengli.springboot.helloworld;注意:这里我对FileUploadProperties使用了@Component注解,如果没有使用@Component注解,则需要在入口启动类上加上@EnableConfigurationProperties注解。Spring Boot 在properties文件中支持使用SpEL表达式,可以进行校验(校验注解使用的是javax.validation)等操作。import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@SpringBootApplication
public class SampleController {
@Autowired
private FileUploadProperties fileUploadProperties;@RequestMapping("/") String home() { return "Hello World! file.upload.stor-path为:" + fileUploadProperties.getStorPath(); } public static void main(String[] args) throws Exception { SpringApplication springApplication = new SpringApplication(SampleController.class); springApplication.run(args); }
}
private String storPath;
4.外部化配置(配置方式与优先级)
5.application.properties文件按优先级,优先级高的会覆盖优先级低的
Spring Boot QQ交流群:599546061