关注「Java视界」公众号,获取更多技术干货

如何引用 application.yml 或者 application.properties 之外的其它配置文件?

我们都知道 @Value@ConfigurationProperties 获取默认配置文件的属性值,也就是application.yml或者application.properties的属性值。

若想引用其它配置文件里的属性值怎么办呢?

可以使用 @PropertySource + @ConfigurationProperties 去实现。

一、 新建配置文件

新建一个user.properties的配置文件:

user.userName= root
user.isAdmin= true
user.regTime= 2019/11/01
user.isOnline= 1
user.maps.k1=v1
user.maps.k2=v2
user.lists=list1,list2
user.address.tel= 15899988899
user.address.name=上海```

二、 新建实体类

@Component
@PropertySource(value = "classpath:user.properties", encoding = "UTF-8")
@ConfigurationProperties(prefix = "user")
@Setter
@Getter
@ToString
public class User {

    private String userName;
    private boolean isAdmin;
    private Date regTime;
    private Long isOnline;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Address address;

}
@ToString
@Getter
@Setter
public class Address {

    private String tel;
    private String dec;
}

这里使用@PropertySource(“classpath:user.properties”)获取对应的properties文件,注意增加编码方式,否则可能中文乱码;再用@ConfigurationProperties(prefix = “user”)进行属性映射。

三、测试及结果

import fengge.test.user.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@org.springframework.boot.test.context.SpringBootTest
public class TestDemo {

    @Autowired
    User user;

    @Test
    public void testConfigurationProperties(){
       System.out.println("[===========]" + user);
    }
}
User(userName=root, isAdmin=false, regTime=Fri Nov 01 00:00:00 CST 2019, isOnline=1, maps={k2=v2, k1=v1}, lists=[list1, list2], address=Address(tel=15899988899, dec=上海))

四、@PropertySource 和 @ImportResource 的区别

这两个都是为spring导入配置文件,但是有些区别:

(1) @PropertySource(“classpath:config.properties”)

一般配置文件是 .properties 或者 .yml 形式,用于给javabean注入值,一般用在javabean的类名上。

spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true

(2) @ImportResource(“classpath:Beans.xml”)

注解的文件格式和内容应该是xml文件。并且xml内容中必须包含以下信息:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd" >
        
    <context:property-placeholder location="classpath:/config.properties"/>
</beans>

在spring boot中已经被配置类替代,一般用于启动类上。

五、FileSystemResource和ClassPathResource有何区别?

简单来说:

  • ClassPathResource类,当资源是在项目里的时候,用这个,相对路径的
  • FileSystemResource是绝对路径的

在ClassPathResource中Spring会在ClassPath中自动搜寻配置文件,所以要把ClassPathResource 文件放在ClassPath下。如果将spring-config.xml保存在了src文件夹下,只需给出配置文件的名称即可,因为src文件夹是默认的。

posted @ 2022-06-25 14:02  沙滩de流沙  阅读(499)  评论(0编辑  收藏  举报

关注「Java视界」公众号,获取更多技术干货