【Spring Boot】(4)、配置文件值注入

1、配置文件使用上节中yaml书写的配置信息:

server:  
  port: 8081  
  path: /hello  
person:  
  name: zhangsan  
  age: 20  
  boss: false  
  birth: 2017/11/12  
  #map写法1:行内  
  maps1: {k1: v1, k2: v2}  
  #map写法2  
  maps2:  
    k3: v3  
    k4: v4  
  #数组写法1  
  lists1:   
    - l1  
    - l2  
  #数组写法2:行内  
  lists2: [l3, l4]  
  # 对象  
  dog:  
    name: 小狗  
    age: 2  


2、编写javaBean

/**
 * 将配置文件中的配置的每一个属性值,映射到组件中
 * @ConfigurationProperties: 告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定。
 *      prefix: 配置文件中的prefix指定的属性下的所有属性与该组件属性一一对应。
 *
 * @ConfigurationProperties: 默认从全局配置文件中获取值
 *
 * 只有这个组件是容器中的组件,容器才能提供@ConfigurationProperties功能。
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person implements Serializable {

	private String name;
	private Integer age;
	private Boolean boss;
	private Date birth;

	private Map<String, Object> maps;
	private List<Object> lists;

	private Dog dog;
	
	//省略getter和setter
}
可以导入配置文件处理器,然后配置属性的时候就会有提示
<!--配置文件处理器-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>


3、注解

3.1、@ConfigurationProperties

  • 告诉SpringBoot将本类中的属性和配置文件中相关的配置进行绑定。

  • prefix: 配置文件中的prefix指定的属性下的所有属性与该组件属性一一对应。

  • 默认从全局配置文件中获取值


3.2、修改idea的properties默认文件编码格式


3.3、@Value和@ConfigurationProperties获取值的区别

  @ConfigurationProperties @Value
功能 批量注入配置文件中的 一个一个指定
松散绑定 支持松散绑定 不支持松散绑定
SpEL 不支持 支持
JSR303数据校验 支持,@Validated、@Email等 不支持
复杂类型封装 支持 不支持

总结:

  • 如果只是需要配置文件中的某个属性,建议使用 @Value

  • 如果需要映射到某个配置类中的属性,建议使用 @ConfigurationProperties

3.4、配置文件注入值校验:

@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person implements Serializable {

	@Email
	private String name;
	private Integer age;
	private Boolean boss;
	private Date birth;

	private Map<String, Object> maps;
	private List<Object> lists;

	private Dog dog;
    
    //省略getter和setter
}

3.5、@PropertySource

  • 用于读取application.properties之外的properties文件

  • 该注解需要配合@ConfigurationProperties一起使用

  • 需要在@ConfigurationProperties中指定使用的前缀prefix(如果有)

  • 需要在@PropertySource指定加载的properties文件

#employee.properties
employee.name=lisi
employee.age=30
employee.birth=2017/11/11
employee.boss=false
@Component
@PropertySource(value = {"classpath:employee.properties"},encoding = "UTF-8")
@ConfigurationProperties(prefix = "employee")
public class Employee implements Serializable {
	private String name;
	private Integer age;

	private Date birth;
	private Boolean boss;
    
    //省略getter和setter
}

3.6、@ImportResource

  • 导入spring的配置文件,让配置文件里面的内容生效。

SpringBoot里面没有Spring的配置文件,即使用Spring项目中的xml格式的配置文件,SpringBoot不能自动识别,如果想让Spring的配置文件生效,需要使用@ImportResource标注在一个配置类上,推荐标注在主配置类上。

<!-- 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="helloService" class="org.com.cay.spring.boot.service.HelloService"></bean>
</beans>
@ImportResource(locations = {"classpath:beans.xml"})


3.7、 @Bean

SpringBoot推荐给容器中添加组件的方式:使用注解 @Bean
/**
 * @Configuration: 指明当前类是一个注解类,用来代替原来的Spring的xml配置文件
 */
@Configuration
public class MyConfig {

	//将方法的返回值添加到容器中,容器中这个组件默认的id为方法名
	@Bean
	public HelloService helloService(){
		System.out.println("添加组件:helloService");
		return new HelloService();
	}
}


====================打个广告,欢迎关注====================

QQ:
412425870
微信公众号:Cay课堂

csdn博客:
http://blog.csdn.net/caychen
码云:
https://gitee.com/caychen/
github:
https://github.com/caychen

点击群号或者扫描二维码即可加入QQ群:

328243383(1群)




点击群号或者扫描二维码即可加入QQ群:

180479701(2群)




猜你喜欢

转载自blog.csdn.net/caychen/article/details/79967716