目录
3.使用@ConfigurationProperties注入属性
1.application.properties配置文件
一、普通属性配置
例:改变Tomcat的端口号和默认路径名称
二、对象类型配置
用配置方式创建一个person实例
2.application.yaml配置文件
启动顺序
yaml配置方式
1.对数组hobby[3]或单列集合的配置
2.对Map集合的配置
3.对实体类person的配置
3.使用@ConfigurationProperties注入属性
目的:将application.yaml声明的实体类对象注入到具体的类中
application.yaml
Person.java
在测试类中测试:
测试结果:
代码:
package com.lcu.hmgzs.domain;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private int id;
private String name;
private List hobby;
private String[] family;
private Map map;
private Pet pet;
。。。省略get和set方法
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", hobby=" + hobby +
", family=" + Arrays.toString(family) +
", map=" + map +
", pet=" + pet +
'}';
}
}
package com.lcu.hmgzs.domain;
public class Pet {
private String type;
private String name;
。。。省略get和set方法
@Override
public String toString() {
return "Pet{" +
"type='" + type + '\'' +
", name='" + name + '\'' +
'}';
}
}
-------------------------
application.yaml文件
server:
#端口号
port: 8086
#对实体类对象person进行属性配置
person:
id: 2
name: list
hobby: [sing,read,sleep]
family: [father,mother]
map: {k1: v1,k2: v2}
pet: {type: cat,name: tom}
-----------------------------
测试类:
package com.lcu.hmgzs;
import com.lcu.hmgzs.com.lcu.hmgzs.HelloController;
import com.lcu.hmgzs.domain.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest //标记SpringBoot单元测试,并加载项目的applicationContext上下文环境
class HmgzsApplicationTests {
@Autowired
private HelloController helloController;
@Autowired
private Person person;
@Value("${person.name}")
private String name;
@Test
void contextLoads() {
String hello = helloController.testHello();
System.out.println(hello);
System.out.println(person);
System.out.println(name);
}
}
------运行结果--------
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
2020-03-24 12:24:32.377 INFO 22324 --- [main] com.lcu.hmgzs.HmgzsApplicationTests : Starting HmgzsApplicationTests on DESKTOP-PV8AL1A with PID 22324 (started by YanWeihe in E:\OnlinQuestSystemSpringBoot)
2020-03-24 12:24:32.378 INFO 22324 --- [main] com.lcu.hmgzs.HmgzsApplicationTests : No active profile set, falling back to default profiles: default
2020-03-24 12:24:34.143 INFO 22324 --- [main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-24 12:24:34.490 INFO 22324 --- [main] com.lcu.hmgzs.HmgzsApplicationTests : Started HmgzsApplicationTests in 2.577 seconds (JVM running for 4.081)
hello2!
Person{id=2, name='list', hobby=[sing, read, sleep], family=[father, mother], map={k1=v1, k2=v2}, pet=Pet{type='cat', name='tom'}}
list
2020-03-24 12:24:34.824 INFO 22324 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
Process finished with exit code 0
4.两种注解对比分析
5.使用@PropertySource加载配置文件
或者使用@Component代替@Configuration
注意:使用@Component代替@Configuration不用写@EnableConfigurationPropries,使用@Cofiguration必须写@EnableConfigurationPropries.
6.使用@ImportResource加载XML配置文件
4.测试
7.使用Configuration编写自定义配置类
8.使用Profile文件进行多环境配置
9.使用@Profile注解进行多环境配置
测试dev
10.随机值设置以及参数间引用
配置
测试