SpingBoot之配置文件的值注入问题
我们在这里研究的是以yml配置文件值注入的问题:
Person:
lastName: 张三
age: 23
boss: false
birth: 2018-10-11
maps: {k1: v1,k2: v2}
lists:
- pig
- dog
dog:
name: 半半
age: 1
定义了一个Person的实体类:
package com.example.demo.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 将配置文件中配置的属性值映射到组件中
* @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
* prefix = "person" 配置文件中哪些下面的所有属性进行一一映射
* 只有这个组件是容器中的组件才能使用容器提供的@ConfigurationProperties的功能
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
再定义一个dog的实体类
package com.example.demo.entity;
public class Dog {
private String name;
private Integer age; @Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
set/get方法......
再定义一个测试类:
package com.example.demo;
import com.example.demo.entity.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* springBoot测试
* 可以在测试期间可以很方便的类似编码一样进行自动注入等容器的功能
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
测试结果:
重点:
2、@Value获取值和@ConfigurationProperties获取值比较
@ConfigurationProperties | @Value | |
---|---|---|
功能 | 批量注入配置文件中的属性 | 一个个指定 |
松散绑定(松散语法) | 支持 | 不支持 |
SpEL | 不支持 | 支持 |
JSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |
配置文件yml还是properties他们都能获取到值;
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;
所以我们也可以利用@Value()进行赋值:
@Componentpublic class Person {
//lastName必须是邮箱格式@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;
在配置文件值注入的时候进行数据校验:
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
//lastName必须是邮箱格式
private String lastName;
private Integer age;private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
private Boolean boss;
3、@PropertySource&@ImportResource&@Bean
@PropertySource:加载指定的配置文件,将配置文件中配置的每一个属性的值,映射到这个组件中
@ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定,
prefix = "person":配置文件中哪个下面的所有属性进行一一映射只有这个组件是容器中的组件,才能
容器提供的@ConfigurationProperties功能,@ConfigurationProperties(prefix = "person")默认从全局
配置文件中获取值。
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
}
set/get方法.....
@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上
@ImportResource(locations = {"classpath:beans.xml"})
导入Spring的配置文件让其生效
SpringBoot推荐给容器中添加组件的方式;推荐使用全注解的方式
1、配置类@Configuration
2、使用@Bean给容器中添加组件
@Configuration
public class MyAppConfig {
//将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
@Bean
public HelloService helloServicfun(){
System.out.println("配置类@Bean给容器中添加组件了...");
return new HelloService();
}
}
SpingBoot之配置文件的值注入问题的更多相关文章
- spring 读取配置文件,将值注入到静态字段
resources/config/config-dev.properties es.ip.node=xxxxxxxcluster.name=xxxxxxxclient.transport.sniff= ...
- Spring Boot之配置文件值注入(@ConfigurationProperties)
前言:Spring Boot配置文件值的注入有两种方式,分别是 @ConfigurationProperties @Value 这里我们使用第一种 首先我们创建一个application.yml文件, ...
- 3springboot:springboot配置文件(配置文件、YAML、属性文件值注入<@Value、@ConfigurationProperties、@PropertySource,@ImportResource、@Bean>)
1.配置文件: springboot默认使用一个全局配置文件 配置文件名是固定的 配置文件有两种(开头均是application,主要是文件的后缀): ->application.prope ...
- 结合SpEL使用@Value-基于配置文件或非配置的文件的值注入-Spring Boot
本文主要介绍Spring @Value 注解注入属性值的使用方法的分析,文章通过示例代码非常详细地介绍,对于每个人的学习或工作都有一定的参考学习价值 在使用spring框架的项目中,@Value是经常 ...
- SpringBoot基础学习(二) SpringBoot全局配置文件及配置文件属性值注入
全局配置文件 全局配置文件能够对一些默认配置值进行修改.SpringBoot 使用一个名为 application.properties 或者 application.yaml的文件作为全局配置文件, ...
- Spring 设值注入 构造注入 p命名空间注入
注入Bean属性---构造注入配置方案 在Spring配置文件中通过<constructor-arg>元素为构造方法传参 注意: 1.一个<constructor-arg>元素 ...
- Spring接口编程_设值注入和构造注入
说明: UserManagerImp是设值注入,UserManagerImp2是构造注入 接口不注入,也就是在Spring配置文件中没有接口的<bean>,但是定义的时候是用接口 priv ...
- spring 运行时属性值注入
继续spring学习,今天介绍两种外部属性值注入的方式.当你需要读取配置信息时,可以快速读取. 开始之前先创建属性文件site.properties,放在classpath下面 #数据库配置 ### ...
- 【Spring学习笔记-2.1】Spring的设值注入和构造注入
设值注入: 先通过无参数的构造函数创建一个Bean实例,然后调用对应的setter方法注入依赖关系: 配置文件: <?xml version="1.0" encoding=& ...
随机推荐
- 了解Unix进程(3)
fork() 系统调用可以创建新的进程.然后查看进程ID和父进程ID使用getpid()和getppid()函数. 使用C语言描述: #include <unistd.h> #includ ...
- matlab 文件打开设置
平台 macOS MATLAB 版本 matlab 2017a 需要设置文件打开编码的情况 从windows平台迁移过来的.m文件的编码格式是GB2312的, 而macOS的MATLAB默认是UTF- ...
- php 转码函数 你还在用iconv吗?-- 解决sqlserver插入中文失败问题
文章来源 :http://www.veryhuo.com/a/view/41348.html 这次给客户同步sqlserver数据,临时搭的 PHP Query Analyzer 插入某些中文一直有些 ...
- svn报错cleanup failed–previous operation has not finished; run cleanup if it was interrupte...
今天在svn提交的时候它卡顿了一下,我以为已经提交完了,就按了一下,结果就再也恢复不了,也继续不了了... 报错 cleanup failed–previous operation has not f ...
- orcale错题分析
删除同义词语法正确的是: Drop synonym sy nonym_name; 关于Oracle创建间隔分区后,正确的是: 使用partition(分区名)可以查看特定分区内存放的表记录 关于序列 ...
- iOS开发ReactiveCocoa学习笔记(三)
RAC常用用法: 1.监听按钮的点击事件: UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame ...
- 用xaml画的带阴影3D感的圆球
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <E ...
- 基于python3.6的如何爬取百度地图
先前参考了其他的代码,大多数是python2.7写的,而3.6用的类库以及规则有了很大的变动,所以自己写了一个这样的代码,供给大家参考. def get_station(i): station=[] ...
- 使用kvm制作Eucalyptus镜像(Windows Server 2008r2为例)
1.前言 Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...
- pecl install msgpack
Before the beginning: There are two php version, php5.5, php7.1. we need to install msgpack under ph ...