在项目实际运用中,我们往往会将配置属性分类,写到不同的配置文件中。 然而由于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());
}