如何解决Spring @PropertySource使用带有PropertyPlaceholderConfigurer的占位符
我有两个查询文件,在一种情况下,我希望存储库使用@PropertySource批注中的占位符加载文件A,在其他情况下,文件B:
@Configuration
@PropertySource(
value={"classpath:properties/${databaseName}.properties"})
public class ConfigExample {
...
}
此PropertyPlaceholderConfigurer类编写正确吗?
@Configuration
public class PropertyPlaceholderConfigurerConfig {
@Value("${fileA}")
private String db1;
@Value("${fileB}")
private String db2;
@Bean
public static PropertyPlaceholderConfigurer ppc() throws IOException {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
if(fileA) {
ppc.setLocations(new ClassPathResource("properties/" + db1+ ".properties"));
}
else if (fileB) {
ppc.setLocations(new ClassPathResource("properties/" + db2+ ".properties"));
}
return ppc;
}
}
请注意,此代码仅是示例,因此语法可能已关闭。这是解决我问题的正确方法吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。