在我以前的Spring4web-app中,我使用了一个applicationContext.xml文件,我的默认Spring配置文件如下;
<beans profile="default">
<context:property-placeholder location="file:/opt/myapp/myapp-ws.properties" />
</beans>现在我使用的是Spring5框架,而不是Spring Boot2.x,我想在我的Java Config类中实现这一点。
我的主配置类看起来像这样;
@Configuration
@ComponentScan(basePackages = "com.tomholmes.myapp")
@EnableWebMvc
public class MyAppConfig
{
}我有如下的AppInitializer:
public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
private static final Log logger = LogFactory.getLog(ApplicationInitializer.class);
@Override
protected Class<?>[] getRootConfigClasses()
{
return new Class[]
{ MyAppConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses()
{
return new Class[]{};
}
@Override
protected String[] getServletMappings()
{
return new String[]
{ "/api/*" };
}
}我在网上做了一些研究,因为有很多关于这方面的信息,但其中很多都与Spring Boot混为一谈,我只想要一个没有Spring Boot的Spring 5解决方案。我会继续寻找,我相信这是一个简单的问题。
谢谢!
发布于 2019-05-28 23:58:56
我相信像这样的东西可能会起到作用:
@Configuration
public class PropertiesConfig {
@Bean
public PropertyPlaceholderConfigurer properties() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
final List<Resource> resources = new ArrayList<>();
resources.add(new FileSystemResource("/etc/app/application-{profile_1}.properties"));
resources.add(new FileSystemResource("/etc/app/application-{profile_2}.properties"));
ppc.setLocations(resourceLst.toArray(new Resource[]{}));
return ppc;
}请注意,这只是一个建议,这段代码没有经过测试。
特定于配置文件的应用程序属性应由当前活动配置文件自动解析。
发布于 2019-05-29 00:37:17
我还没有测试过这一点,但是同时包含@Profile和@PropertySource的@Configuration类应该可以工作:
@Configuration
@Profile("default")
@PropertySource("file:/opt/myapp/myapp-ws.properties")
public class MyappWebservicePropertyConfig {
}https://stackoverflow.com/questions/56345353
复制相似问题