阅读更多
public class OutPropertiesPostProcessor implements BeanPostProcessor {
private static Logger logger = LoggerFactory.getLogger(OutPropertiesPostProcessor.class);
@Autowired
private AbstractEnvironment env;
private boolean isAdded = false;
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (isAdded) {
return bean;
}
try {
Resource resource = new ClassPathResource("path-config.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);
String outPropertiesPath = props.getProperty("out.properties.file.path");// 获取application.properties中配置的项目外的配置文件路径
logger.info("add out properties:" + outPropertiesPath);
env.getPropertySources().addLast(new ResourcePropertySource("out", outPropertiesPath));
isAdded = true;
} catch (IOException e) {
logger.error("追加外部配置文件properties出错", e);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}