private Date bir;
@Value("${strs}")
private String[] strs;
@Value("${list}")
private List<String> list;
@Value("#{${maps}}") // map注入取值有点特殊
private Map<String, String> maps;
@GetMapping("hello")
public String hello() {
// port = 8989
System.out.println("port = " + port);
// str = zhenyu
System.out.println("str= " + str);
// time = Wed Dec 12 12:12:12 CST 2012
System.out.println("time = " + bir);
// aa
// bb
// cc
for (String str : strs) {
System.out.println(str);
}
// zhangsan
// lisi
// wangwu
list.forEach( v -> System.out.println(v));
// k = aa, v = xiaoyi
// k = bb, v = xiaoer
// k = cc, v = xiaosan
maps.forEach((k, v) -> {
System.out.println("k = " + k + ", v = " + v);
});
return "hello spring boot!";
}
}
`application.properties` 中配置:
server.port = 8989
属性注入
str = zhenyu
bir = 2012/12/12 12:12:12
strs = aa, bb, cc
list = zhangsan, lisi, wangwu
maps = {‘aa’:‘xiaoyi’, ‘bb’:‘xiaoer’, ‘cc’:‘xiaosan’}
[](https://gitee.com/vip204888/java-p7)对象方式注入 @ConfigurationProperties
--------------------------------------------------------------------------------------------------
对象方式注入使用注解:`@ConfigurationProperties(prefix="前缀")`
`@ConfigurationProperties` 告诉 SpringBoot 本类中的所有属性在配置文件中进行绑定;
* 只有这个组件是容器中的组件,才能容器提供的 `@ConfigurationProperties` 功能,所以需要 `@Component`
@ToString
@Data // 必要
@Component // @Configuration 也可以
@ConfigurationProperties(prefix = “user”) // 必要
public class User {
private String id;
private String name;
private Integer age;
private Date bir;
}
控制器中使用:`@Autowired` 完成自动注入;
@RestController
@RequestMapping(“hello”)
public class HelloController {
@Autowired
private User user;
@GetMapping("hello")
public String hello() {
System.out.println(user);
// User(id=1721, name=yusael, age=20, bir=Wed Dec 12 12:12:12 CST 2012)
return "hello spring boot!";
}
}
`application.properties` 中配置:
自定义对象属性注入
user.id = 1721
user.name = zhenyu
user.age = 20
user.bir = 2012/12/12 12:12:12
* * *
注:可以通过引入依赖 —— **配置文件处理器**,构建自定义注入元数据。
* 意思就是引入这个依赖后,在 配置文件中写注入对象会有提示,不引入也不影响什么。
org.springframework.boot
spring-boot-configuration-processor
true
[](https://gitee.com/vip204888/java-p7)两种注入方式比较
---------------------------------------------------------------------------
| | @ConfigurationProperties | @Value |
| --- | --- | --- |
| 功能 | 批量注入配置文件中的属性 | 一个个指定 |
| 松散绑定(松散语法) | 支持 | 不支持 |
| SpEL | 不支持 | 支持 |
| JSR303 数据校验 | 支持 | 不支持 |
| 复杂类型封装 | 支持 | 不支持 |
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用 `@Value`;
如果说,我们专门编写了一个 javaBean 来和配置文件进行映射,我们就直接使用`@ConfigurationProperties`;
[](https://gitee.com/vip204888/java-p7)注入细节
-----------------------------------------------------------------------
### [](https://gitee.com/vip204888/java-p7)配置文件注入值数据校验 @Validated
@Component
@ConfigurationProperties(prefix = “person”)
@Validated // 配置文件注入值数据校验
public class Person {
/**
* <bean class="Person">
* <property name="lastName" value="字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property>
* <bean/>
*/
//lastName必须是邮箱格式
@Email
//@Value("${person.last-name}")
private String lastName;
//@Value("#{11*2}")
private Integer age;
//@Value("true")
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
### [](https://gitee.com/vip204888/java-p7)加载指定的配置文件 @PropertySource
`@PropertySource` 加载指定的配置文件;
// 加载指定的配置文件
@PropertySource(value = {“classpath:person.properties”})
@Component
@ConfigurationProperties(prefix = “person”)
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
}
### [](https://gitee.com/vip204888/java-p7)导入 Spring 的配置文件 @ImportResource
`@ImportResource`:导入 Spring 的配置文件,让配置文件里面的内容生效;
* Spring Boot 里面没有 Spring 的配置文件,我们自己编写的配置文件,也不能自动识别;
想让 Spring 的配置文件生效,加载进来;`@ImportResource` 标注在一个配置类上;
// 导入Spring的配置文件让其生效
@ImportResource(locations = {“classpath:beans.xml”})
然后编写 Spring 的配置文件:
<?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="helloService" class="com.atguigu.springboot.service.HelloService"></bean>
### [](https://gitee.com/vip204888/java-p7)配置文件占位符
随机数:
${random.uuid}
${random.value}
${random.int}
${random.long}
${random.int(10)}
${random.int[1024,65536]}
`:` 指定占位符中的默认值:person.last-name=张三${random.uuid} # 随机数
person.age=${random.int} # 随机数
person.birth=2017/12/15
随机数:
${random.uuid}
${random.value}
${random.int}
${random.long}
${random.int(10)}
${random.int[1024,65536]}
:
指定占位符中的默认值:
person.last-name=张三${random.uuid} # 随机数
person.age=${random.int} # 随机数
person.birth=2017/12/15