springboot注释详解

1.属性注入

@ConfigurationProperties(prefix="...")

spring会从classpath下的/config目录或者classpath的根目录查找application.propertiesapplication.yml

/config优先于classpath根目录

ps:

配置文件如下(配置文件可以使用${属性名}调用其他属性,如果加冒号,${属性名:值}则表示找不到的话使用此默认值):

my.name=Isea533

my.port=8080

my.servers[0]=dev.bar.com

my.servers[1]=foo.bar.com

使用案例如下:

 1 @ConfigurationProperties(prefix="my")
 2 public class Config {
 3     private String name;
 4     private Integer port;
 5     private List<String> servers = new ArrayList<String>();
 6 
 7     public String geName(){
 8         return this.name;
 9     }
10 
11     public Integer gePort(){
12         return this.port;
13     }
14     public List<String> getServers() {
15         return this.servers;
16     }
17 }

@PropertySource

这个注解可以指定具体的属性配置文件,优先级比较低。

@Value(“${xxx}”)

这种方式是最简单的,通过@Value注解可以将属性值注入进来。

原文地址:https://www.cnblogs.com/zipon/p/6404459.html