英文:
How to set auto location in PropertySourcesPlaceholderConfigurer spring boot?
问题
以下是翻译好的部分:
package com.org.tre.myth.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.FileSystemResource;
@Configuration
public class ExternalPropertyConfig {
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
properties.setLocation(new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties")); //devpconfprop
properties.setLocation(new FileSystemResource("src/main/resources/conf.properties")); //localconfprop
properties.setIgnoreResourceNotFound(false);
return properties;
}
}
当我在本地环境时,我需要通过注释来停用开发位置:
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
//properties.setLocation(new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties")); //devpconfprop
properties.setLocation(new FileSystemResource("src/main/resources/conf.properties")); //localconfprop
properties.setIgnoreResourceNotFound(false);
return properties;
}
在将应用部署到开发环境之前,我需要通过注释本地位置并激活开发位置:
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
properties.setLocation(new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties")); //devpconfprop
//properties.setLocation(new FileSystemResource("src/main/resources/conf.properties")); //localconfprop
properties.setIgnoreResourceNotFound(false);
return properties;
}
是否有一种方式可以自动根据环境设置位置,或者类似的方法?请帮帮我,随意发表评论。谢谢!
英文:
So i was develop some app in spring boot, i'm so confused how to used PropertySourcesPlaceholderConfigurer setlocation automatically, here is my code
package com.org.tre.myth.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.FileSystemResource;
@Configuration
public class ExternalPropertyConfig {
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
properties.setLocation(new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties")); //devpconfprop
properties.setLocation(new FileSystemResource("src/main/resources/conf.properties")); //localconfprop
properties.setIgnoreResourceNotFound(false);
return properties;
}
}
When i'm on local i need to deactivate dev location using comment
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
//properties.setLocation(new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties")); //devpconfprop
properties.setLocation(new FileSystemResource("src/main/resources/conf.properties")); //localconfprop
properties.setIgnoreResourceNotFound(false);
return properties;
}
And before i deploy my app in dev, i need to do opposite thing by comment my local location and activate dev location
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
properties.setLocation(new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties")); //devpconfprop
//properties.setLocation(new FileSystemResource("src/main/resources/conf.properties")); //localconfprop
properties.setIgnoreResourceNotFound(false);
return properties;
}
is there is a way it can do automatically set the location by detecting environment or some kind like that? please help me, feel free to comment
Thanks guys.
答案1
得分: 0
使用Spring Boot的配置文件特性。
您可以按照以下步骤操作:
- 创建环境特定的配置文件:
application.properties
application-dev.properties
Spring Boot将自动加载application.properties
中的所有属性以及您定义并设置为活动的特定配置文件属性。
-
将开发环境属性文件 (
/myth/app/data/weblogic_configuration/config/conf.properties
) 的内容保存到application-dev.properties
中,
并将本地属性文件 (src/main/resources/conf.properties
) 的内容保存到application.properties
中。 -
在部署到开发环境之前,确保通过将以下内容添加到VM选项中,将开发环境配置设置为Spring的活动配置:
-Dspring.profiles.active=dev
这将加载
application-dev.properties
中的属性,并覆盖application.properties
中的属性。b. 在本地运行应用程序时,删除或不指定活动配置。
参考链接:
https://docs.spring.io/spring-boot/docs/1.1.x/reference/html/boot-features-profiles.html
英文:
Use Spring Boot's Profiles feature.
You can follow the steps below:
- Create environment specific profiles:
application.properties
application-dev.properties
Spring Boot will automatically load all the properties of the application.properties and the profile specific properties you defined and set as active.
-
Save the content of your dev properties file (
/myth/app/data/weblogic_configuration/config/conf.properties
) insideapplication-dev.properties
and the content of your local properties file (src/main/resources/conf.properties
) inside application.properties. -
Before deploying to Dev environment, make sure to set the dev profile as the spring active profile by adding this to the VM Options:
-Dspring.profiles.active=dev
This will load the properties inside your
application-dev.properties
and overrides the properties inside your application.properties.b. When running your your app locally, remove or do not specify active profile.
See link below for reference:
https://docs.spring.io/spring-boot/docs/1.1.x/reference/html/boot-features-profiles.html
专注分享java语言的经验与见解,让所有开发者获益!
评论