[英]How to Move application.properties or Do External Properties in Spring Boot Maven Project
So i was develop some application in Spring Boot, but i get stumbled upon application.properties file, i want to make it easier to use, cause i have some environment to test, like Dev, Uat and Production所以我在 Spring Boot 中开发了一些应用程序,但是我偶然发现了 application.properties 文件,我想让它更容易使用,因为我有一些环境要测试,比如 Dev、Uat 和 Production
When i want to launch the app, i build it using mvn clean install with application.properties profile on the environment.当我想启动应用程序时,我使用 mvn clean install 和环境中的 application.properties 配置文件来构建它。 cause my app only using war snapshot for backend service.导致我的应用程序仅将战争快照用于后端服务。
like when i want to launch in dev就像我想在开发中启动一样
spring.profiles.active=dev
or when i want to deploy it in uat environment i'm using或者当我想在我正在使用的 uat 环境中部署它时
spring.profiles.active=uat
is there a way to make it easier, like using external prop, so i don't need using application.properties anymore, cause i'll upload application.properties in each of my environment, so the app can read from external properties.有没有办法让它变得更容易,比如使用外部道具,所以我不再需要使用 application.properties,因为我会在我的每个环境中上传 application.properties,所以应用程序可以从外部属性中读取。
i already tried PropertySourcePlaceholderConfigurer, but no luck我已经尝试过 PropertySourcePlaceholderConfigurer,但没有运气
@Configuration
public class ExternalPropertyConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
final PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new Resource[ ] {
new FileSystemResource("/myth/app/data/weblogic_configuration/config/myth_uat_configuration.properties"),
new FileSystemResource("/myth/app/data/weblogic_configuration/config/myth_dev_configuration.properties"),
new FileSystemResource("/src/main/resources/myth_local_configuration.properties")
};
properties.setLocations( resources );
properties.setIgnoreResourceNotFound(true);
properties.setIgnoreUnresolvablePlaceholders(true);
return properties;
}
}
After building the jar you can launch it like this构建 jar 后,您可以像这样启动它
java -jar -Dspring.profiles.active=dev "app.jar"
and providing the active profile will make that particular profile take effect并提供活动配置文件将使该特定配置文件生效
You just create the profile scoped properties file like application-dev.properties
or application-prod.properties
您只需创建配置文件范围的属性文件,如application-dev.properties
或application-prod.properties
They are automatically loaded.它们会自动加载。 No need for code to load them无需代码加载它们
You can have a look at this Spring Boot Features你可以看看这个Spring Boot Features
you can load properties using xml configuration as below:您可以使用 xml 配置加载属性,如下所示:
<bean id="configProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesMode" value="2" />
<property name="locations">
<list>
<value>classpath:db.properties</value>
<value>classpath:nosql.properties</value>
<value>classpath:api.properties</value>
</list>
</property>
</bean>
After this you can use property spring.profiles.active defined in any of your properties file (any of db.properties
, nosql.properties
or api.properties
) in your code ->在此之后,您可以使用在您的任何属性文件中定义的属性spring.profiles.active (任何db.properties
, nosql.properties
或api.properties
)在您的属性代码中
@Service("bean")
public class Bean {
@Value("${spring.profiles.active}")
private String profile;
}
I think this is what you are looking for Spring boot YML doc .我认为这就是您正在寻找的Spring boot YML doc 。 Generally externalizing the config is achieved through ymls.通常外部化配置是通过 ymls 实现的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.