springboot?yml配置文件值注入方式
yml配置文件值注入
搭建項目
參考 IDEA快速搭建spring-boot項目(Spring initializr)
pom.xml
創(chuàng)建項目后,還需在pom.xml中的<dependencies>標簽添加該依賴。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
創(chuàng)建實體類
package com.demo.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age;
@Override
public String toString() {
return "person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
spring boot核心配置文件application.yml
1.將application.properties文件后綴改為.yml,內容為:
server: port: 8080 person: name: 小狗 age: 21
測試類
package com.demo.demo;
import com.demo.demo.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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
Person person=new Person();
@Test
public void contextLoads() {
System.out.println(person);
}
}
運行

結果:

自動注入yml文件和properties文件
在springboot中,如果需要使用到配置文件中的數(shù)據(jù),自動注入即可,非常方便,但是在使用yml中的屬性時,自動注入?yún)s失效了? 發(fā)現(xiàn),如果是properties文件,直接注入即可,但是yml需要增加一個配置
yml文件的自動注入class
yml文件相對于properties較整潔和簡便,在自動注入的使用需要增加配置
增加pom依賴
? <!--*.yml auto inject config--> ? ?<dependency> ? ? ? ?<groupId>org.springframework.boot</groupId> ? ? ? ?<artifactId>spring-boot-configuration-processor</artifactId> ? ? ? ?<optional>true</optional> ? ?</dependency>
增加配置類
public class YamlPropertyLoaderFactory ?extends DefaultPropertySourceFactory {
? ? @Override
? ? public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
? ? ? ? if (null == resource) {
? ? ? ? ? ? super.createPropertySource(name, resource);
? ? ? ? }
? ? ? ? return new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource(),null);
? ? }
}注入指定yml配置文件到實體類中
/**
?* 在需要注入的class類上加上這3個注解即可
?*/
@Configuration
@PropertySource(value = "classpath:application-quartz.yml", factory = YamlPropertyLoaderFactory.class)
@ConfigurationProperties(prefix = "quartz")
public class JobTime {
? ? private String articleCarwler;
? ? private String earthlySweetSentenceApi;
? ? private String rainbowFartApi;
? ? private String qqRobotMsgTimer;
}說明:
@PropertySource(value = "classpath:application-quartz.yml", factory = YamlPropertyLoaderFactory.class)
這個注解表示注入哪個配置文件,指定第二步中的配置類
@ConfigurationProperties(prefix = "quartz")
表示指定配置文件中的屬性是什么前綴,可忽略,例如:quartz.articleCarwler=0 0/5 * * * ?
Properties配置文件自動注入
properties類型的配置文件就比較簡單,不需要增加上面的依賴和配置,直接指定注入即可
直接注入屬性到class
@Configuration
@PropertySource("classpath:/application.properties")
@ConfigurationProperties(prefix = "quartz")
public class JobTime {
? ? private String task1;
? ? private String task2;
}說明:
@PropertySource("classpath:/application.properties")指定配置文件名稱
@ConfigurationProperties(prefix = "quartz")
指定配置文件中的key的前綴,可忽略此注解, 例如:quartz.task1=0 3 * * * ?
代碼中直接注入
如果是在代碼中使用單獨的屬性,不需要將屬性都注入到class中,那么可直接使用注解注入到變量中,在代碼中直接使用
無論是yml還是properties都可以直接注入,不需要其他配置
使用注解:@Value("${key}") 就可以直接注入。
例如:
? ? @Value("${quartz.taks1}")
? ? private String taks1;以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
通過Spring Boot配置動態(tài)數(shù)據(jù)源訪問多個數(shù)據(jù)庫的實現(xiàn)代碼
這篇文章主要介紹了通過Spring Boot配置動態(tài)數(shù)據(jù)源訪問多個數(shù)據(jù)庫的實現(xiàn)代碼,需要的朋友可以參考下2018-03-03
詳解Java使用sqlite 數(shù)據(jù)庫如何生成db文件
這篇文章主要介紹了詳解Java 操作sqllite 數(shù)據(jù)庫如何生成db文件的相關資料,需要的朋友可以參考下2017-07-07

