全局配置文件
配置文件属性值注入到对象的属性
Spring Boot自定义配置
使用@PropertySource引入properties配置文件
作用:告诉spring ,自定义配置文件在哪
1.在resources目录下,新建配置文件user.properties
#对User对象进行属性注入
user.id=110
#将来获取不到name的值,获取的是spring.security.user.name的值
user.name=jack
2.在domain包下,新建User类
@Component
@PropertySource("classpath:user.properties")// 指定自定义配置文件位置和名称
@ConfigurationProperties(prefix = "user") // 指定配置文件注入属性前缀
public class User{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//重写toString()
}
3.在测试类里添加
@Autowired
private User user;
@Test
void testUser() {
System.out.println(user);
}
使用@ImportResource加载XML配置文件
1.建子包service和建类MyService
public class MyService {
}
2.在resources目录下建beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myService" class="com.itheima.service.MyService" />
</beans>
3.启动类上添加@ImportResource("classpath:beans.xml")
4.在测试类里添加
@Autowired
private ApplicationContext applicationContext;
@Test
void testIoc() {
System.out.println(applicationContext.containsBean("myService"));
}
使用@Configuration编写自定义配置类
1.新建子包dao和新建一个类MyDao
public class MyDao {
public User selectById(int id){
return new User(id,"zhubajie");
}
}
2、新建子包config和新建一个类MyConfig
@Configuration // 定义该类是一个配置类
public class MyConfig {
@Bean // 将返回值对象作为组件添加到Spring容器中,该组件name默认为方法名
public Mydao myDao(){
return new MyDao();
}
}
3、测试
@SpringBootTest
class MyDaoTests {
//从spring容器当中拿到MyDao对象
@Autowired
private MyDao myDao;
@Test
void contextLoads() {
}
@Test
void testSelectById() {
User u = myDao.selectById(2);
System.out.println(u);
}
}
Profile多环境配置
目的让系统自动适应不同的运行环境
多个配置文件配置多环境
1.编写多个配置文件
格式:application-{profile}.properties
application-dev.properties server.port=8081
application-test.properties server.port=8082
application-prod.properties server.port=8083
2.激活指定环境
方式一
修改全局配置文件application.properties spring.profiles.active=dev 重启项目
方式二
控制台 java -jar xxx.jar --spring.profiles.active=dev
@Profile注解多环境配置
@Profile:作用于类,通过value属性指定环境配置
以模拟不同数据库配置为例
1.建包config,建接口DBConnector
public interface DBConnector {
public void configure();
}
2.建三个实现类模拟三种数据库配置
@Configuration
@Profile("dev") // 指定多环境配置类标识
public class DevDBConnector implements DBConnector {
@Override
public void configure() {
System.out.println("数据库配置环境dev");
}
}
@Configuration
@Profile("prod") // 指定多环境配置类标识
public class ProdDBConnector implements DBConnector {
@Override
public void configure() {
System.out.println("数据库配置环境prod");
}
}
@Configuration
@Profile("test") // 指定多环境配置类标识
public class TestDBConnector implements DBConnector {
@Override
public void configure() {
System.out.println("数据库配置环境test");
}
}
3.激活某个数据库配置环境
修改全局配置文件application.properties spring.profiles.active=dev
4.测试是否激活
建包controller 及控制类
@RestController
public class DBController {
@Autowired
private DBConnector dbConnector;
@GetMapping("/showDB")
public void showDB(){
dbConnector.configure();
}
}
启动项目,在浏览器里输入http://localhost:8081/showDB
随机值设置 (RandomValuePropertySource提供)
${random.xx} xx表示随机数类型和范围
示例
my.secret=${random.value} //配置随机字符串
my.number=${random.int} //配置随机整数
my.bignumber=${random.long} //配置随机长整数
my.uuid=${random.uuid} //配置随机UUID
my.number.less.than.ten=${random.int(10)} //配置小于10的随机整数
my.number.in.range=${random.int[1024,65536]}//配置在[1024,65536]的随机整数
参数间引用
${xx} xx表示先前在配置文件中已经配置过的属性名。
示例
app.name=MyApp
app.description=${app.name} is a Spring Boot application
测试随机值及参数间引用
1.在全局配置文件添加两个测试属性
tom:
age: ${random.int[10,20]}
description: tom的年龄可能是${tom.age}
2.测试类里添加
@Value("${tom.description}")
private String description;
@Test
public void placeholderTest() {
System.out.println(description);
}