SpringBoot系列之@PropertySource用法简介

SpringBoot系列之@PropertySource用法简介

继上篇博客:SpringBoot系列之@Value和@ConfigurationProperties用法对比之后,本博客继续介绍一下@PropertySource注解的用法,通过上一篇博客的知识,可以知道@Value和@ConfigurationProperties都可以用于获取配置文件的属性值,不过有个细节容易被忽略,那就是,这两个注解在Springboot项目中都是获取默认配置文件的属性值,也就是application.yml或者application.properties的属性值

不过我们想要配属性的话,肯定都不能全都往默认配置文件里堆的,如果想引用其它配置文件的属性值,就可以使用本博客介绍的@PropertySource注解

新建一个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=上海

使用@PropertySource(“classpath:user.properties”)获取对应的properties文件,再用@ConfigurationProperties(prefix = “user”)进行属性映射

package org.muses.jeeplatform.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("classpath:user.properties")
@ConfigurationProperties(prefix = "user")
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;

    @Override
    public String toString() {
        return "User{" +
                       "userName='" + userName + '\'' +
                       ", isAdmin=" + isAdmin +
                       ", regTime=" + regTime +
                       ", isOnline=" + isOnline +
                       ", maps=" + maps +
                       ", lists=" + lists +
                       ", address=" + address +
                       '}';
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public boolean isAdmin() {
        return isAdmin;
    }

    public void setAdmin(boolean admin) {
        isAdmin = admin;
    }

    public Date getRegTime() {
        return regTime;
    }

    public void setRegTime(Date regTime) {
        this.regTime = regTime;
    }

    public Long getIsOnline() {
        return isOnline;
    }

    public void setIsOnline(Long isOnline) {
        this.isOnline = isOnline;
    }

    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 Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

对应的Address类:

package org.muses.jeeplatform.bean;

/**
 * 
 *
 * 
* * @author nicky *
 * 修改记录
 *    修改后版本:     修改人:  修改日期: 2019年11月03日  修改内容:
 * 
*/
public class Address { private String tel; private String name; @Override public String toString() { return "Address{" + "tel='" + tel + '\'' + ", name='" + name + '\'' + '}'; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

写一个junit测试类

package org.muses.jeeplatform;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.muses.jeeplatform.bean.User;
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 SpringBootTest {

    @Autowired
    User user;

    @Test
    public void testConfigurationProperties(){
        System.out.println(user);
    }

}

读取成功,就不用将配置写在默认的application.properties里了

User{userName='root', isAdmin=false, regTime=Fri Nov 01 00:00:00 CST 2019, isOnline=1, maps={k1=v1, k2=v2}, lists=[list1, list2], address=Address{tel='15899988899', name='上海市'}}

注意:对于@PropertySource注解,默认是不支持yml配置文件读取的,需要修改重写才可以

你可能感兴趣的:(Java框架,SpringBoot)