Spring5Java配置集默认配置文件
在我以前的Spring4Web应用程序中,我使用了一个applicationContext.xml文件,我的默认Spring配置文件如下:Spring5Java配置集默认配置文件,java,spring,Java,Spring,在我以前的Spring4Web应用程序中,我使用了一个applicationContext.xml文件,我的默认Spring配置文件如下: <beans profile="default"> <context:property-placeholder location="file:/opt/myapp/myapp-ws.properties" /> </beans> public class ApplicationInitializer extend
<beans profile="default">
<context:property-placeholder location="file:/opt/myapp/myapp-ws.properties" />
</beans>
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/*" };
}
}
我有如下的AppInitializer
<beans profile="default">
<context:property-placeholder location="file:/opt/myapp/myapp-ws.properties" />
</beans>
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/*" };
}
}
公共类应用程序初始化器扩展了AbstractAnnotationConfigDispatcherServletInitializer
{
私有静态最终日志记录器=LogFactory.getLog(ApplicationInitializer.class);
@凌驾
受保护类[]getRootConfigClasses()
{
返回新类[]
{MyAppConfig.class};
}
@凌驾
受保护类[]getServletConfigClasses()
{
返回新类[]{};
}
@凌驾
受保护的字符串[]getServletMappings()
{
返回新字符串[]
{“/api/*”};
}
}
我一直在网上做一些研究,因为有很多关于这方面的信息,但是很多都把Spring Boot混为一谈,我只想要一个没有Spring Boot的Spring 5解决方案。我会继续寻找,我相信这是一个简单的问题
谢谢 我相信这样的事情可能会奏效:
@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;
}
@配置
公共类属性配置{
@豆子
公共属性PlaceHolderConfigurer属性(){
final PropertyPlaceHolderConfigure ppc=新的PropertyPlaceHolderConfigure();
最终列表资源=新的ArrayList();
添加(新文件系统资源(“/etc/app/application-{profile_1}.properties”);
添加(新文件系统资源(“/etc/app/application-{profile_2}.properties”);
ppc.setLocations(resourceLst.toArray(新资源[]{}));
返回ppc;
}
请注意,这只是建议,此代码未经测试
特定于配置文件的应用程序属性应该由当前活动配置文件自动解析。我相信这样做可能会奏效:
@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;
}
@配置
公共类属性配置{
@豆子
公共属性PlaceHolderConfigurer属性(){
final PropertyPlaceHolderConfigure ppc=新的PropertyPlaceHolderConfigure();
最终列表资源=新的ArrayList();
添加(新文件系统资源(“/etc/app/application-{profile_1}.properties”);
添加(新文件系统资源(“/etc/app/application-{profile_2}.properties”);
ppc.setLocations(resourceLst.toArray(新资源[]{}));
返回ppc;
}
请注意,这只是建议,此代码未经测试
特定于配置文件的应用程序属性应该由当前活动配置文件自动解析。我还没有测试过这一点,但是一个同时具有和的类应该可以工作:
@Configuration
@Profile("default")
@PropertySource("file:/opt/myapp/myapp-ws.properties")
public class MyappWebservicePropertyConfig {
}
我还没有测试过这一点,但是一个同时具有和的类应该可以工作:
@Configuration
@Profile("default")
@PropertySource("file:/opt/myapp/myapp-ws.properties")
public class MyappWebservicePropertyConfig {
}