这篇文章跟大家分析一下“springboot yml配置文件值的注入方式是什么”。内容详细易懂,对“springboot yml配置文件值的注入方式是什么”感兴趣的朋友可以跟着小编的思路慢慢深入来阅读一下,希望阅读后能够对大家有所帮助。下面跟着小编一起深入学习“springboot yml配置文件值的注入方式是什么”的知识吧。
参考 IDEA快速搭建spring-boot项目(Spring initializr)
pom.xml
创建项目后,还需在pom.xml中的<dependencies>标签添加该依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
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;
}
}
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);
}
}
结果:
在springboot中,如果需要使用到配置文件中的数据,自动注入即可,非常方便,但是在使用yml中的属性时,自动注入却失效了? 发现,如果是properties文件,直接注入即可,但是yml需要增加一个配置
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类型的配置文件就比较简单,不需要增加上面的依赖和配置,直接指定注入即可
直接注入属性到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;
springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。
关于springboot yml配置文件值的注入方式是什么就分享到这里啦,希望上述内容能够让大家有所提升。如果想要学习更多知识,请大家多多留意小编的更新。谢谢大家关注一下亿速云网站!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。