一、@PropertySource注解
我们已经知道,
@ConfigurationProperties
与
@Value
都可以用于获取配置文件的属性值,但是我们发现,这两个注解在SpringBoot项目中都是获取默认配置文件的属性值——即
application.yml
或者
application.properties
配置文件的属性值的。
关于
@ConfigurationProperties
与
@Value
简单使用可以参考上一篇:
SpringBoot总结(四)——@Value和@ConfigurationProperties的区别
下面将介绍,怎样来引用其它配置文件的属性值。这时候我们就可以使用
@PropertySource
注解。
School.java
package com.example.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@PropertySource(value = {"classpath:school.properties"})
@ConfigurationProperties(prefix = "school")
public class School {
private String lastName;
private String address;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Student student;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
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 Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "School{" +
"lastName='" + lastName + '\'' +
", address='" + address + '\'' +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", student=" + student +
'}';
}
}
Student.java
package com.example.bean;
import org.springframework.stereotype.Component;
@Component
public class Student {
private String name;
private Integer age;
private String score;
public Student() {
}
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;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score='" + score + '\'' +
'}';
}
}
新建school.properties配置文件
school.last-name=学校
school.address=江苏
school.birth=2020/1/1
school.maps.k1=v1
school.maps.k2=v2
school.lists=a,b,c,d
school.student.name=李四
school.student.age=19
编写测试类:
package com.example;
import com.example.bean.School;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot02ApplicationTests {
@Autowired
School school;
@Test
public void contextLoads() {
System.out.println(school);
}
}
测试结果:
注:
@Component
@PropertySource("classpath:school.properties")
@ConfigurationProperties(prefix = "school")
- @PropertySource注解:来获取我们编写的school.properties文件。
- @ConfigurationProperties注解:进行属性的映射,获取值。
二、@ImportResource注解
@ImportResource注解的作用
:
-
@ImportResource注解用于
导入Spring的配置文件
,让配置文件(如applicationContext.xml)里面的内容生效。 -
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;让Spring的配置文件生效,加载进来。
下面是用创建一个单独的配置类,全注解的方式来加载此XML bean定义文件的使用:
package com.example.config;
import com.example.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
@Bean
public HelloService helloService() {
return new HelloService();
}
}
- @Configuration注解:指定当前的类是一个配置类。
- @Bean注解:给容器中添加组件。将方法的返回值添加到容器中,容器中的默认id就是方法名。
版权声明:本文为weixin_43759352原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。