当前位置:
首页 >
前端技术
> javascript
>内容正文
javascript
Spring boot配置文件值注入
生活随笔
收集整理的这篇文章主要介绍了
Spring boot配置文件值注入
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
@ConfigurationProperties
将配置文件中配置的每一个属性的值,映射到这个组件中
告诉SpringBoot将本类中的所有属性
和配置文件中相关的配置进行绑定
Prefix
表示前缀,配置文件中哪个
下面的所有属性进行一一映射
注意
只有这个组件是容器中的组件
才能容器使用提供的@ConfigurationProperties功能
使用注解,这个组件必须在容器中
导入配置文件处理器
<!--导入配置文件处理器,配置文件进行绑定就会有提示--> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional> </dependency>创建Bean
package com.atguigu.springboot.bean;import org.hibernate.validator.constraints.Email; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import org.springframework.validation.annotation.Validated;import javax.validation.constraints.Null; import java.util.Date; import java.util.List; import java.util.Map;/*** 将配置文件中配置的每一个属性的值,映射到这个组件中* @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;* prefix = "person":配置文件中哪个下面的所有属性进行一一映射* 表示前缀* 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;* @ConfigurationProperties(prefix = "person")默认从全局配置文件中获取值;**/ //@PropertySource(value = {"classpath:person.properties"}) @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;//@Value("${person.maps}")private Map<String,Object> maps;private List<Object> lists;private Dog dog;@Overridepublic String toString() {return "Person{" +"lastName='" + lastName + '\'' +", age=" + age +", boss=" + boss +", birth=" + birth +", maps=" + maps +", lists=" + lists +", dog=" + dog +'}';}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Boolean getBoss() {return boss;}public void setBoss(Boolean boss) {this.boss = boss;}public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}public Map<String, Object> getMaps() {return maps;}public void setMaps(Map<String, Object> maps) {this.maps = maps;}public List<Object> getLists() {return lists;}public void setLists(List<Object> lists) {this.lists = lists;}public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog = dog;} }配置文件
application.properties配置
person.last-name=张三${random.uuid} person.age=${random.int} person.birth=2017/12/15 person.boss=false person.maps.k1=v1 person.maps.k2=14 person.lists=a,b,c person.dog.name=${person.hello:hello}_dog person.dog.age=15或者
application.yml配置
测试
package com.atguigu.springboot;import com.atguigu.springboot.bean.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.context.ApplicationContext; import org.springframework.test.context.junit4.SpringRunner;/*** SpringBoot单元测试;** 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能**/ @RunWith(SpringRunner.class) @SpringBootTest public class SpringBoot02ConfigApplicationTests {@AutowiredPerson person;@AutowiredApplicationContext ioc;@Testpublic void contextLoads() {System.out.println(person);}}运行
总结
以上是生活随笔为你收集整理的Spring boot配置文件值注入的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Spring boot的properti
- 下一篇: Spring boot属性松散绑定