Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <util:properties id="Job1Props"
- location="classpath:job1.properties"></util:properties>
- <util:properties id="Job2Props"
- location="classpath:job2.properties"></util:properties>
- java -jar myapp.jar app.config.location=file:./config
- <util:properties id="Job2Props"
- location="{app.config.location}/job2.properties"></util:properties>
- //psuedo code
- if (StringUtils.isBlank(app.config.location)) {
- System.setProperty(APP_CONFIG_LOCATION, "classpath:");
- }
- -Dspring.config.location=your/config/dir/
- -Dspring.config.location=classpath:job1.properties,classpath:job2.properties
- @Configuration
- public class PropertiesConfiguration {
- @Bean
- public PropertyPlaceholderConfigurer properties() {
- final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
- // ppc.setIgnoreUnresolvablePlaceholders(true);
- ppc.setIgnoreResourceNotFound(true);
- final List<Resource> resourceLst = new ArrayList<Resource>();
- resourceLst.add(new ClassPathResource("myapp_base.properties"));
- resourceLst.add(new FileSystemResource("/etc/myapp/overriding.propertie"));
- resourceLst.add(new ClassPathResource("myapp_test.properties"));
- resourceLst.add(new ClassPathResource("myapp_developer_overrides.properties")); // for Developer debugging.
- ppc.setLocations(resourceLst.toArray(new Resource[]{}));
- return ppc;
- }
- @PropertySource(ignoreResourceNotFound=true,value="classpath:jdbc-${spring.profiles.active}.properties")
- public class DBConfig{
- @Value("${jdbc.host}")
- private String jdbcHostName;
- }
- }
- java -jar target/myapp.jar --spring.config.location=classpath:file:///C:/Apps/springtest/jdbc.properties,classpath:file:///C:/Apps/springtest/jdbc-dev.properties
- set spring.profiles.active=dev
- set spring.profiles.active=default
- $ chmod 766 application.properties
- $ chown tomcat application.properties
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.core.io.PathResource;
- import org.springframework.core.io.Resource;
- import java.io.IOException;
- import java.util.Properties;
- import static java.util.Arrays.stream;
- @Configuration
- public class PropertiesConfig {
- private static final Logger LOG = LoggerFactory.getLogger(PropertiesConfig.class);
- private final static String PROPERTIES_FILENAME = "user.properties";
- @Value("${properties.location:}")
- private String propertiesLocation;
- @Bean
- Properties userProperties() throws IOException {
- final Resource[] possiblePropertiesResources = {
- new ClassPathResource(PROPERTIES_FILENAME),
- new PathResource("config/" + PROPERTIES_FILENAME),
- new PathResource(PROPERTIES_FILENAME),
- new PathResource(getCustomPath())
- };
- // Find the last existing properties location to emulate spring boot application.properties discovery
- final Resource propertiesResource = stream(possiblePropertiesResources)
- .filter(Resource::exists)
- .reduce((previous, current) -> current)
- .get();
- final Properties userProperties = new Properties();
- userProperties.load(propertiesResource.getInputStream());
- LOG.info("Using {} as user resource", propertiesResource);
- return userProperties;
- }
- private String getCustomPath() {
- return propertiesLocation.endsWith(".properties") ? propertiesLocation : propertiesLocation + PROPERTIES_FILENAME;
- }
- }
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.core.io.PathResource;
- import org.springframework.core.io.Resource;
- import java.io.IOException;
- import java.util.Map;
- import java.util.Properties;
- import static java.util.Arrays.stream;
- import static java.util.stream.Collectors.toMap;
- @Configuration
- class PropertiesConfig {
- private final static Logger LOG = LoggerFactory.getLogger(PropertiesConfig.class);
- private final static String[] PROPERTIES_FILENAMES = {"job1.properties", "job2.properties", "job3.properties"};
- @Value("${properties.location:}")
- private String propertiesLocation;
- @Bean
- Map<String, Properties> myProperties() {
- return stream(PROPERTIES_FILENAMES)
- .collect(toMap(filename -> filename, this::loadProperties));
- }
- private Properties loadProperties(final String filename) {
- final Resource[] possiblePropertiesResources = {
- new ClassPathResource(filename),
- new PathResource("config/" + filename),
- new PathResource(filename),
- new PathResource(getCustomPath(filename))
- };
- final Resource resource = stream(possiblePropertiesResources)
- .filter(Resource::exists)
- .reduce((previous, current) -> current)
- .get();
- final Properties properties = new Properties();
- try {
- properties.load(resource.getInputStream());
- } catch(final IOException exception) {
- throw new RuntimeException(exception);
- }
- LOG.info("Using {} as user resource", resource);
- return properties;
- }
- private String getCustomPath(final String filename) {
- return propertiesLocation.endsWith(".properties") ? propertiesLocation : propertiesLocation + filename;
- }
- }
- @Configuration
- @Profile("one")
- @PropertySource("file:/{selected location}/app.properties")
- public class TestClass {
- @Autowired
- Environment env;
- @Bean
- public boolean test() {
- System.out.println(env.getProperty("test.one"));
- return true;
- }
- }
- test.one = 1234
- @SpringBootApplication
- public class TestApplication {
- public static void main(String[] args) {
- SpringApplication.run(testApplication.class, args);
- }
- }
- spring.profiles.active = one
- @Value("${test.one}")
- String str;
- spring.profiles.active=local
- spring.data.mongodb.host=localhost
- spring.data.mongodb.port=27017
- spring.data.mongodb.database=users
- spring.data.mongodb.username=humble_freak
- spring.data.mongodb.password=freakone
- spring.rabbitmq.host=localhost
- spring.rabbitmq.username=guest
- spring.rabbitmq.password=guest
- spring.rabbitmq.port=5672
- rabbitmq.publish=true
- mvn spring-boot:run -Drun.profiles=local
- mvn spring-boot:run -Drun.profiles=qa
- mvn spring-boot:run -Drun.profiles=prod
Add Comment
Please, Sign In to add comment