當前位置:
首頁 >
前端技术
> 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属性松散绑定