spring默认使用yml中的配置,但有时候要用传统的xml或properties配置,就需要使用spring-boot-configuration-processor了
依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency>
|
example
以阿里云配置为例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import lombok.Data;
import java.io.Serializable;
@Data public class AliyunProperties implements Serializable {
private String endpoint; private String accessKeyId; private String accessKeySecret; private String bucketName;
private String bucketDomain; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import lombok.Getter; import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;
import java.io.Serializable;
@Setter @Getter @Component @ConfigurationProperties(prefix = "community.aliyun") public class ArticleProperties implements Serializable { private AliyunProperties aliyun; }
|
1 2 3 4 5 6 7 8 9 10 11 12
| community: aliyun: endpoint: https://oss-cn-hangzhou.aliyuncs.com accessKeyId: ceshi accessKeySecret: ceshi bucketName: ceshi bucketDomain: https://ceshi.oss-cn-hangzhou.aliyuncs.com/ server: servlet: context-path: /ceshi
|
测试
1 2 3 4 5 6 7 8 9 10 11 12 13
| @RestController @RequestMapping("/test") public class DemoController {
@Autowired private ArticleProperties articleProperties;
@GetMapping("/a") public String search() { AliyunProperties aliyunProperties = articleProperties.getAliyun(); return aliyunProperties.getAccessKeyId(); } }
|