我们在以往将配置信息从 xml或者properties文件 通过类加载器classloader 加载properties文件,将有用的信息读出,再放到对应的位置进行使用
而在spring3.x中 提供了使用注解的方式进行配置读取
@Configuration:声明当前类作为配置类使用
@Bean:声明在方法上,将方法的返回值加入Bean容器
@value:属性值注入
@PropertySource:指定外部待加载文件
快速搭建springboot项目以下为pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
com.neusoft
springtest1
0.0.1-SNAPSHOT
springtest1
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
com.alibaba
druid
1.1.11
org.springframework.boot
spring-boot-maven-plugin
aaa.properties配置文件放到resources中
aaa.a1=aa1123
aaa.a2=aa2123
aaa.a3=aa3123
aaa.a4=aa4123
再创建一个实体类
package com.neusoft.pojo;
public class ConfigPojo {
public String a1=null;
public String a2=null;
public String a3=null;
public String a4=null;
}
我们先创建一个Configtest类
package com.neusoft.config;
import com.neusoft.pojo.ConfigPojo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:testaa.properties")
public class Configtest {
@Value("${aaa.a1}")
String a1;
@Value("${aaa.a1}")
String a2;
@Value("${aaa.a3}")
String a3;
@Value("${aaa.a4}")
String a4;
@Bean
public ConfigPojo getAaa(){
ConfigPojo cp=new ConfigPojo();
cp.a1=a1;
cp.a2=a2;
cp.a3=a3;
cp.a4=a4;
return cp;
}
}
最后再创建一个control 方便请求与访问
package com.neusoft.control;
import com.neusoft.pojo.ConfigPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class HelloControl {
@Autowired
private ConfigPojo configPojo;
@GetMapping("getconfig")
public String test1(){
return configPojo.a1+configPojo.a2+configPojo.a3+configPojo.a4;
}
}
第二种方式 可以使用lombok插件
使用方式 https://www.jianshu.com/p/542bf23a9421
将配置放到 application.properties
新建类 configlombok
使用 @ConfigurationProperties(prefix = "aaa") 并指定前缀名称
package com.neusoft.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "aaa")
@Data
public class Configlombok {
public String a1;
public String a2;
public String a3;
public String a4;
修改Configtest
@EnableConfigurationProperties(Configlombok.class) 允许配置生效
package com.neusoft.config;
import com.neusoft.pojo.ConfigPojo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@EnableConfigurationProperties(Configlombok.class)
public class Configtest {
public ConfigPojo getAaa(Configlombok configlombok){
ConfigPojo cp=new ConfigPojo();
cp.a1=configlombok.getA1();
cp.a2=configlombok.getA2();
cp.a3=configlombok.getA3();
cp.a4=configlombok.getA4();
return cp;
}
}
实体类增加 toString()方法
package com.neusoft.pojo;
public class ConfigPojo {
public String a1=null;
public String a2=null;
public String a3=null;
public String a4=null;
@Override
public String toString() {
return a1+a2+a3+a4;
}
}
修改control
package com.neusoft.control;
import com.neusoft.config.Configlombok;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class HelloControl {
@Autowired
private Configlombok configlombok;
@GetMapping("getconfig")
public String test1(){
return configlombok.toString();
}
}
再次访问
升级方法二 进一步简化
在Configlombok中添加生成tostring注解
并将@ConfigurationProperties(prefix = "aaa")注释掉
package com.neusoft.config;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//@ConfigurationProperties(prefix = "aaa")
@ToString(includeFieldNames=true)
@Data
public class Configlombok {
public String a1;
public String a2;
public String a3;
public String a4;
}
再将 Configtest 中的//@EnableConfigurationProperties({Configlombok.class}) 注释掉
以及getAaa方法注释掉。
并添加新的getAaa方法与注解
@ConfigurationProperties(prefix = "aaa") 会根据返回的对象 到配置文件中aaa前缀下的名字与值 并去找 与 对象Configlombok中set 方法对应方法进行赋值 (自动将配置文件中的内容 注入到Configlombok对象属性中)
package com.neusoft.config;
import com.neusoft.pojo.ConfigPojo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
//@PropertySource("classpath:testaa.properties")
//@EnableConfigurationProperties({Configlombok.class})
public class Configtest {
// @Value("${aaa.a1}")
// String a1;
// @Value("${aaa.a1}")
// String a2;
// @Value("${aaa.a3}")
// String a3;
// @Value("${aaa.a4}")
// String a4;
// public ConfigPojo getAaa(Configlombok configlombok){
// ConfigPojo cp=new ConfigPojo();
// cp.a1=configlombok.getA1();
// cp.a2=configlombok.getA2();
// cp.a3=configlombok.getA3();
// cp.a4=configlombok.getA4();
// return cp;
// }
@Bean
@ConfigurationProperties(prefix = "aaa")
public Configlombok getAaa(){
return new Configlombok();
}
}
package com.neusoft.control;
import com.neusoft.config.Configlombok;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class HelloControl {
// @Autowired
// private ConfigPojo configPojo;
@Autowired
private Configlombok configlombok;
@GetMapping("getconfig")
public String test1(){
return configlombok.toString();
}
}
4.配置文件为 application.yaml或者application.yml
当appliation.yml与application.properties并存时 优先选择application.properties 如果properties 里没有 再选择yml
如果再加入外置配置文件
优先顺序 application.properties>appliation.yml>外置配置
appliction.properties
aaa.a1=aa1123
aaa.a2=aa2123
aaa.a3=aa3123
#aaa.a4=aa4123
application.yml
aaa:
a1: bb1123
a2: bb2123
a3: bb3123
a4: bb4123
加入外置配置文件
testaa.properties
aaa.a1=cc1123
aaa.a2=cc2123
aaa.a3=cc3123
aaa.a4=cc4123
appliction.properties
aaa.a1=aa1123
aaa.a2=aa2123
#aaa.a3=aa3123
#aaa.a4=aa4123
application.yml
aaa:
a1: bb1123
a2: bb2123
a3: bb3123
# a4: bb4123
Configtest 加入@PropertySource("classpath:testaa.properties")
package com.neusoft.config;
import com.neusoft.pojo.ConfigPojo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:testaa.properties")
//@EnableConfigurationProperties({Configlombok.class})
public class Configtest {
// @Value("${aaa.a1}")
// String a1;
// @Value("${aaa.a1}")
// String a2;
// @Value("${aaa.a3}")
// String a3;
// @Value("${aaa.a4}")
// String a4;
// public ConfigPojo getAaa(Configlombok configlombok){
// ConfigPojo cp=new ConfigPojo();
// cp.a1=configlombok.getA1();
// cp.a2=configlombok.getA2();
// cp.a3=configlombok.getA3();
// cp.a4=configlombok.getA4();
// return cp;
// }
@Bean
@ConfigurationProperties(prefix = "aaa")
public Configlombok getAaa(){
return new Configlombok();
}
}