Java 如何根据弹簧轮廓加载属性文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41263105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load property file based on spring profiles
提问by Santoshkumar Kalasa
How to create project architecture to support multiple envionment. Each environment will have different datasource from different property file like(dev-propertfile,test-propertyFil,Production-propertyfile) with help of spring's
如何创建项目架构以支持多环境。在 spring 的帮助下,每个环境都有来自不同属性文件的不同数据源,如(dev-propertyfile,test-propertyFil,Production-propertyfile)
org.springframework.core.env.Environment;
采纳答案by USM
I'll give step by step procedure for Spring boot applications.
我将逐步介绍 Spring 启动应用程序的过程。
- Inside /src/main/resources/application.propertiesmention spring.profiles.active=dev(or Prod)
- Create /src/main/resources/application-dev.propertiesand give your custom dev configurations here.
- Create /src/main/resources/application-prod.propertiesand give your custom prod configurations here.
- 在/src/main/resources/application.properties 中提到spring.profiles.active=dev(或 Prod)
- 创建/src/main/resources/application-dev.properties并在此处提供您的自定义开发配置。
- 创建/src/main/resources/application-prod.properties并在此处提供您的自定义产品配置。
Run.
跑。
回答by Bruno DM
Take a look at Spring Profile. You will define a set of profiles configurations, like Test, Dev, Production. And then, when you launch the application, you can define wich profile it should use.
看看Spring Profile。您将定义一组配置文件配置,例如测试、开发、生产。然后,当您启动应用程序时,您可以定义它应该使用的配置文件。
Here are some tutorialsof how to use.
这里有一些关于如何使用的教程。
And this guys had the same problem as yours: How to config @ComponentScan dynamic?
这家伙和你有同样的问题:如何配置@ComponentScan dynamic?
回答by Chandu
Put property file in same location as application.property
and follow
the naming convention application-{profile}.properties
like
application-dev.properties
,application-test.properties
,
application-prod.properties
将属性文件放在与相同的位置application.property
并遵循命名约定,application-{profile}.properties
如
application-dev.properties
, application-test.properties
,
application-prod.properties
And in application.properties
set spring.profiles.active=dev,test
etc
并在application.properties
集合spring.profiles.active=dev,test
等
回答by Abhishek Galoda
For Spring Boot applications it will work easily even by using a YAML File
对于 Spring Boot 应用程序,即使使用 YAML 文件也能轻松工作
spring:
profiles: dev
property: this is a dev env
---
spring:
profiles: prod
property: this is a production env
---
However, for a Spring MVC application, it needs more work. Have a look at this link
但是,对于 Spring MVC 应用程序,它需要更多的工作。看看这个链接
Basically, it involves 2 steps
基本上,它涉及2个步骤
- Get the Spring Profile within servlet context
- 在 servlet 上下文中获取 Spring Profile
If you have set the profile on the server and want it to retrieve it within your application you can use System.getProperty or System.getenv methods. Here is the code which fetches the profile and defaults it to a local profile, if no profile has been found.
如果您在服务器上设置了配置文件并希望它在您的应用程序中检索它,您可以使用 System.getProperty 或 System.getenv 方法。这是获取配置文件并将其默认为本地配置文件(如果未找到配置文件)的代码。
private static final String SPRING_PROFILES_ACTIVE = "SPRING_PROFILES_ACTIVE";
String profile;
/**
* In local system getProperty() returns the profile correctly, however in docker getenv() return profile correctly
* */
protected void setSpringProfile(ServletContext servletContext) {
if(null!= System.getenv(SPRING_PROFILES_ACTIVE)){
profile=System.getenv(SPRING_PROFILES_ACTIVE);
}else if(null!= System.getProperty(SPRING_PROFILES_ACTIVE)){
profile=System.getProperty(SPRING_PROFILES_ACTIVE);
}else{
profile="local";
}
log.info("***** Profile configured is ****** "+ profile);
servletContext.setInitParameter("spring.profiles.active", profile);
}
- To access the application-dev.properties, say now you will need to use @Profile("dev") at the class level
- 要访问 application-dev.properties,现在说您需要在类级别使用 @Profile("dev")
The following code will fetch the application-dev.properties and common.properties
以下代码将获取 application-dev.properties 和 common.properties
@Configuration
@Profile("dev")
public class DevPropertyReader {
@Bean
public static PropertyPlaceholderConfigurer properties() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[] { new ClassPathResource("properties/common.properties"), new ClassPathResource("properties/application-dev.properties") };
ppc.setLocations(resources);
ppc.setIgnoreUnresolvablePlaceholders(true);
return ppc;
}
}
For accessing say application-prod.properties you have to use @Profile("prod")
at the class level. More details can be found here
要访问 application-prod.properties,您必须@Profile("prod")
在类级别使用。可以在此处找到更多详细信息