springboot1.5+ 自定义配置文件

 

 在项目实际运用中,我们往往会将配置属性分类,写到不同的配置文件中。 然而由于spring官方不推荐这种写法,并在springboot1.5后取消了 @ConfigurationProperties locations。因此,springboot版本升级后,需要找到替代的方法。

    在网上也有很多方案,这里引用在墙外找到的一种方案。(具体出处就忘记了)

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.bind.PropertiesConfigurationFactory;
import org.springframework.boot.env.PropertySourcesLoader;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.env.PropertySources;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ClassUtils;

import java.io.IOException;
import java.lang.reflect.Constructor;

/**配置文件类
 * @author: yc
 * @date: Create in 11:20 2017/10/24
 */
public abstract class AbstractApplicationConfiguration implements ResourceLoaderAware{

    private ResourceLoader resourceLoader = new DefaultResourceLoader();

    /**
     *
     * @param clazz    配置类
     * @param prefix   前缀
     * @param locations 文件地址
     * @return
     */
    protected  <T> T bindPropertiesToTarget(Class<T> clazz, String prefix, String... locations) {
        try {
            Constructor<T> constructor = clazz.getConstructor();
            T newInstance = constructor.newInstance();

            PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<>(newInstance);
            factory.setPropertySources(loadPropertySources(locations));
            factory.setConversionService(new DefaultConversionService());
            if (StringUtils.isNotBlank(prefix)) {
                factory.setTargetName(prefix);
            }
            factory.bindPropertiesToTarget();
            return newInstance;

        } catch (Exception ex) {
            String targetClass = ClassUtils.getShortName(clazz);
            throw new BeanCreationException(clazz.getSimpleName(), "Could not bind properties to " + targetClass + " (" + clazz.getSimpleName() + ")", ex);
        }
    }

    private PropertySources loadPropertySources(String[] locations) {
        try {
            PropertySourcesLoader loader = new PropertySourcesLoader();
            for (String location : locations) {
                Resource resource = this.resourceLoader.getResource(location);
                loader.load(resource);
            }
            return loader.getPropertySources();
        }
        catch (IOException ex) {
            throw new IllegalStateException(ex);
        }
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {

    }
}

使用方法:

1.创建自己的配置文件: my.properties

my.name=test
my.pwd=123

 2.建立对应的实体类:

import lombok.Data;

/**
 * @author: yc
 */
@Data
public class MyProperties {

    private String name;
    private String pwd;
}

 3.创建config对象

import com.baobaotao.common.spring.config.custom.AbstractApplicationConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: yc
 * @date: Create in 0:35 2017/10/25
 */
@Configuration
public class MyConfig extends AbstractApplicationConfiguration{

    @Bean
    public MyProperties httpClientProperties(){

        return bindPropertiesToTarget(MyProperties.class, "my", "classpath:my.properties");
    }
}

 测试结果

	@Autowired
	MyProperties myProperties;

	@Test
	public void test1(){

		System.out.println("name = " + myProperties.getName());
		System.out.println("pwd = " + myProperties.getPwd());
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值