Merge pull request #10295 from rozagerardo/rozagerardo/JAVA-3029_Update-Properties-with-Spring-and-Spring-Boot-article

[JAVA-3029] Update "Properties with Spring and Spring Boot" article
This commit is contained in:
Loredana Crusoveanu 2020-12-01 14:51:23 +02:00 committed by GitHub
commit d6e0e1c3e6
36 changed files with 160 additions and 532 deletions

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
@ -47,12 +48,10 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${httpcore.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${configuration-processor.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
@ -126,16 +125,23 @@
</profile>
</profiles>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<spring-cloud.version>2020.0.0-M5</spring-cloud.version>
<commons-configuration.version>1.10</commons-configuration.version>
<guava.version>20.0</guava.version>
<httpcore.version>4.4.11</httpcore.version>
<resource.delimiter>@</resource.delimiter>
<configuration-processor.version>2.2.4.RELEASE</configuration-processor.version>
<!-- <start-class>com.baeldung.buildproperties.Application</start-class> -->
<!-- <start-class>com.baeldung.buildproperties.Application</start-class> -->
<start-class>com.baeldung.yaml.MyApplication</start-class>
<spring-boot.version>2.4.0</spring-boot.version>
</properties>
</project>

View File

@ -1,30 +0,0 @@
package com.baeldung.properties.core;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
public class ComponentInXmlUsingProperties implements InitializingBean {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
public ComponentInXmlUsingProperties(final String propertyValue) {
super();
System.out.println("Constructor Injection - Property Value resolved to: " + propertyValue);
}
//
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("in afterPropertiesSet via @Value: " + injectedProperty);
System.out.println("in afterPropertiesSet Environment: " + env.getProperty("key.something"));
}
}

View File

@ -1,16 +0,0 @@
package com.baeldung.properties.external;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:configForProperties.xml")
@ComponentScan("com.baeldung.core")
public class ExternalPropertiesWithXmlConfig {
public ExternalPropertiesWithXmlConfig() {
super();
}
}

View File

@ -1,16 +0,0 @@
package com.baeldung.properties.external;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:configForPropertiesOne.xml")
@ComponentScan("com.baeldung.core")
public class ExternalPropertiesWithXmlConfigOne {
public ExternalPropertiesWithXmlConfigOne() {
super();
}
}

View File

@ -1,14 +0,0 @@
package com.baeldung.properties.external;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:basicConfigForPropertiesTwo.xml")
public class ExternalPropertiesWithXmlConfigTwo {
public ExternalPropertiesWithXmlConfigTwo() {
super();
}
}

View File

@ -1,21 +0,0 @@
package com.baeldung.properties.spring;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PropertiesWithPlaceHolderConfigurer {
public PropertiesWithPlaceHolderConfigurer() {
super();
}
@Bean
public PropertyPlaceholderConfigurer propertyConfigurer() {
final PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer();
props.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_FALLBACK);
return props;
}
}

View File

@ -1,23 +0,0 @@
package com.baeldung.properties.spring;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.*;
@Configuration
public class PropertyPlaceholderConfig {
public PropertyPlaceholderConfig(){
super();
}
@Bean
public static PropertyPlaceholderConfigurer properties() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[]{ new ClassPathResource("foo.properties") };
ppc.setLocations( resources );
ppc.setIgnoreUnresolvablePlaceholders( true );
return ppc;
}
}

View File

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
>
<context:property-placeholder location="classpath:foo.properties"/>
</beans>

View File

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
>
<context:property-placeholder location="classpath:foo.properties" ignore-unresolvable="true" order="1"/>
</beans>

View File

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:property-placeholder location="classpath:bar.properties"
order="2" />
</beans>

View File

@ -1,19 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
>
<context:property-placeholder location="classpath:database.properties"/>
<bean id="dataSource" class="com.baeldung.configurationproperties.Database">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
</beans>

View File

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
>
<context:property-placeholder location="classpath:foo.properties,classpath:bar.properties,classpath:database.properties"/>
<bean id="componentInXmlUsingProperties" class="com.baeldung.properties.core.ComponentInXmlUsingProperties">
<constructor-arg value="${key.something}"/>
</bean>
</beans>

View File

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
>
<context:property-placeholder location="classpath:foo.properties" ignore-unresolvable="true" order="1"/>
<bean id="componentInXmlUsingProperties" class="com.baeldung.properties.core.ComponentInXmlUsingProperties">
<constructor-arg value="${key.something2}"/>
</bean>
</beans>

View File

@ -1,23 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"
>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:foo.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:foo.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
</beans>

View File

@ -0,0 +1,29 @@
package com.baeldung.properties.additionalproperties;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
@ActiveProfiles("default")
@SpringBootTest(classes = { AdditionalPropertiesTestConfig.class })
public class AdditionalPropertiesFilesIntegrationTest {
Logger logger = LoggerFactory.getLogger(AdditionalPropertiesFilesIntegrationTest.class);
@Value("${baeldung.additionalProperty}")
private String additionalProperty;
@Value("${baeldung.otherProperty}")
private String otherProperty;
@Test
public void givenMultidocumentPropertiesFileWhenBootContextLoadedThenDocumentProcessedCorrectly() {
assertThat(additionalProperty).isEqualTo("additionalValue2");
assertThat(otherProperty).isEqualTo("latterValue");
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.properties.additionalproperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.baeldung.properties.additionalproperties")
public class AdditionalPropertiesTestConfig {
}

View File

@ -1,27 +0,0 @@
package com.baeldung.properties.basic;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:basicConfigForPropertiesOne.xml", "classpath:basicConfigForPropertiesTwo.xml" })
public class PropertiesWithMultipleXmlsIntegrationTest {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
@Test
public final void givenContextIsInitialized_thenNoException() {
System.out.println("in test via @Value: " + injectedProperty);
System.out.println("in test Environment: " + env.getProperty("key.something"));
}
}

View File

@ -1,27 +0,0 @@
package com.baeldung.properties.basic;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:basicConfigForProperties.xml")
public class PropertiesWithXmlIntegrationTest {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
@Test
public final void givenContextIsInitialized_thenNoException() {
System.out.println("in test via @Value: " + injectedProperty);
System.out.println("in test Environment: " + env.getProperty("key.something"));
}
}

View File

@ -1,39 +0,0 @@
package com.baeldung.properties.external;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.properties.external.ExternalPropertiesWithXmlConfigOne;
import com.baeldung.properties.external.ExternalPropertiesWithXmlConfigTwo;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ExternalPropertiesWithXmlConfigOne.class, ExternalPropertiesWithXmlConfigTwo.class }, loader = AnnotationConfigContextLoader.class)
@Ignore("manual only")
public class ExternalPropertiesWithMultipleXmlsIntegrationTest {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
@Value("${external.something}")
private String injectedExternalProperty;
@Test
public final void givenContextIsInitialized_thenNoException() {
System.out.println("in test via @Value: " + injectedProperty);
System.out.println("in test Environment: " + env.getProperty("key.something"));
System.out.println("in test via @Value - external: " + injectedExternalProperty);
System.out.println("in test Environment - external: " + env.getProperty("external.something"));
}
}

View File

@ -1,38 +0,0 @@
package com.baeldung.properties.external;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.properties.external.ExternalPropertiesWithXmlConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ExternalPropertiesWithXmlConfig.class }, loader = AnnotationConfigContextLoader.class)
@Ignore("manual only")
public class ExternalPropertiesWithXmlManualTest {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
@Value("${external.something}")
private String injectedExternalProperty;
@Test
public final void givenContextIsInitialized_thenNoException() {
System.out.println("in test via @Value: " + injectedProperty);
System.out.println("in test Environment: " + env.getProperty("key.something"));
System.out.println("in test via @Value - external: " + injectedExternalProperty);
System.out.println("in test Environment - external: " + env.getProperty("external.something"));
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.properties.multidocument;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
@ActiveProfiles("default")
@SpringBootTest(classes = { MultidocumentTestConfig.class })
public class MultidocumentPropertiesFileIntegrationTest {
Logger logger = LoggerFactory.getLogger(MultidocumentPropertiesFileIntegrationTest.class);
@Value("${baeldung.customProperty}")
private String customProperty;
@Test
public void givenMultidocumentPropertiesFileWhenBootContextLoadedThenDocumentProcessedCorrectly() {
assertThat(customProperty).isEqualTo("valueDefault");
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.properties.multidocument;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
@ActiveProfiles("test")
@SpringBootTest(classes = { MultidocumentTestConfig.class })
public class MultidocumentPropertiesFileWithDevProfileIntegrationTest {
Logger logger = LoggerFactory.getLogger(MultidocumentPropertiesFileWithDevProfileIntegrationTest.class);
@Value("${baeldung.customProperty}")
private String customProperty;
@Test
public void givenMultidocumentPropertiesFileWhenBootContextLoadedThenDocumentProcessedCorrectly() {
assertThat(customProperty).isEqualTo("valueTest");
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.properties.multidocument;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
@ActiveProfiles("dev")
@SpringBootTest(classes = { MultidocumentTestConfig.class })
public class MultidocumentPropertiesFileWithTestProfileIntegrationTest {
Logger logger = LoggerFactory.getLogger(MultidocumentPropertiesFileWithTestProfileIntegrationTest.class);
@Value("${baeldung.customProperty}")
private String customProperty;
@Test
public void givenMultidocumentPropertiesFileWhenBootContextLoadedThenDocumentProcessedCorrectly() {
assertThat(customProperty).isEqualTo("valueDev");
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.properties.multidocument;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.baeldung.properties.multidocument")
public class MultidocumentTestConfig {
}

View File

@ -4,12 +4,11 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import com.baeldung.properties.spring.PropertyPlaceholderConfig;
import com.baeldung.properties.spring.PropertySourcesPlaceholderConfig;
import static org.assertj.core.api.Assertions.assertThat;
@SpringJUnitConfig({PropertyPlaceholderConfig.class, PropertySourcesPlaceholderConfig.class})
@SpringJUnitConfig({PropertySourcesPlaceholderConfig.class})
public class MultiplePlaceholdersJavaConfigIntegrationTest {
@Value("${key.something}")

View File

@ -1,25 +0,0 @@
package com.baeldung.properties.multiple;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@SpringJUnitConfig(locations = "classpath:configForPropertyPlaceholderBeans.xml")
public class MultiplePlaceholdersXmlConfigIntegrationTest {
@Value("${foo}")
private String something;
@Value("${key.something}")
private String something2;
@Test
public void whenReadInjectedValues_thenGetCorrectValues() {
assertThat(something).isEqualTo("bar");
assertThat(something2).isEqualTo("val");
}
}

View File

@ -1,24 +0,0 @@
package com.baeldung.properties.multiple;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@SpringJUnitConfig(locations = {"classpath:configForProperties.xml", "classpath:configForDbProperties.xml"})
public class MultiplePropertiesXmlConfigIntegrationTest {
@Value("${key.something}") private String something;
@Value("${key.something2}") private String something2;
@Value("${jdbc.url}") private String jdbcUrl;
@Test
public void whenReadInjectedValues_thenGetCorrectValues() {
assertThat(something).isEqualTo("val");
assertThat(something2).isEqualTo("val2");
assertThat(jdbcUrl).isEqualTo("jdbc:postgresql:/localhost:5432");
}
}

View File

@ -1,51 +0,0 @@
package com.baeldung.properties.parentchild;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.properties.parentchild.config.ChildConfig2;
import com.baeldung.properties.parentchild.config.ParentConfig2;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextHierarchy({ @ContextConfiguration(classes = ParentConfig2.class), @ContextConfiguration(classes = ChildConfig2.class) })
public class ParentChildPropertyPlaceHolderPropertiesIntegrationTest {
@Autowired
private WebApplicationContext wac;
@Test
public void givenPropertyPlaceHolder_whenGetPropertyUsingEnv_thenCorrect() {
final Environment childEnv = wac.getEnvironment();
final Environment parentEnv = wac.getParent().getEnvironment();
assertNull(parentEnv.getProperty("parent.name"));
assertNull(parentEnv.getProperty("child.name"));
assertNull(childEnv.getProperty("parent.name"));
assertNull(childEnv.getProperty("child.name"));
}
@Test
public void givenPropertyPlaceHolder_whenGetPropertyUsingValueAnnotation_thenCorrect() {
final ChildValueHolder childValueHolder = wac.getBean(ChildValueHolder.class);
final ParentValueHolder parentValueHolder = wac.getParent().getBean(ParentValueHolder.class);
assertEquals(parentValueHolder.getParentName(), "parent");
assertEquals(parentValueHolder.getChildName(), "-");
assertEquals(childValueHolder.getParentName(), "-");
assertEquals(childValueHolder.getChildName(), "child");
}
}

View File

@ -1,24 +0,0 @@
package com.baeldung.properties.parentchild.config;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import com.baeldung.properties.parentchild.ChildValueHolder;
@Configuration
public class ChildConfig2 {
@Bean
public ChildValueHolder childValueHolder() {
return new ChildValueHolder();
}
@Bean
public static PropertyPlaceholderConfigurer properties() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(new ClassPathResource("child.properties"));
return ppc;
}
}

View File

@ -1,24 +0,0 @@
package com.baeldung.properties.parentchild.config;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import com.baeldung.properties.parentchild.ParentValueHolder;
@Configuration
public class ParentConfig2 {
@Bean
public ParentValueHolder parentValueHolder() {
return new ParentValueHolder();
}
@Bean
public static PropertyPlaceholderConfigurer properties() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(new ClassPathResource("parent.properties"));
return ppc;
}
}

View File

@ -5,22 +5,12 @@ import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import com.baeldung.properties.basic.ExtendedPropertiesWithJavaIntegrationTest;
import com.baeldung.properties.basic.PropertiesWithMultipleXmlsIntegrationTest;
import com.baeldung.properties.basic.PropertiesWithXmlIntegrationTest;
import com.baeldung.properties.external.ExternalPropertiesWithJavaIntegrationTest;
import com.baeldung.properties.external.ExternalPropertiesWithMultipleXmlsIntegrationTest;
import com.baeldung.properties.external.ExternalPropertiesWithXmlManualTest;
import com.baeldung.properties.multiple.MultiplePropertiesXmlConfigIntegrationTest;
import com.baeldung.properties.multiple.MultiplePlaceholdersXmlConfigIntegrationTest;
@RunWith(Suite.class)
@SuiteClasses({ //@formatter:off
PropertiesWithXmlIntegrationTest.class,
ExternalPropertiesWithJavaIntegrationTest.class,
ExternalPropertiesWithMultipleXmlsIntegrationTest.class,
ExternalPropertiesWithXmlManualTest.class,
ExtendedPropertiesWithJavaIntegrationTest.class, MultiplePropertiesXmlConfigIntegrationTest.class,
PropertiesWithMultipleXmlsIntegrationTest.class, MultiplePlaceholdersXmlConfigIntegrationTest.class
ExtendedPropertiesWithJavaIntegrationTest.class,
})// @formatter:on
public final class IntegrationTestSuite {
//

View File

@ -0,0 +1,7 @@
baeldung:
additionalProperty: additionalValue
otherProperty: otherValue
spring:
config:
import: classpath:additional-application2.properties

View File

@ -0,0 +1,5 @@
spring.config.activate.on-profile=test
baeldung.customProperty=valueTest
#---
spring.config.activate.on-profile=prod
baeldung.customProperty=valueProd

View File

@ -0,0 +1 @@
baeldung.additionalProperty=additionalValue2

View File

@ -2,4 +2,10 @@ management.endpoints.web.exposure.include=refresh
spring.properties.refreshDelay=1000
spring.config.location=file:extra.properties
spring.main.allow-bean-definition-overriding=true
spring.config.import=classpath:additional-application.properties,classpath:additional-application[.yml],optional:file:./external.properties,classpath:additional-application-properties/
baeldung.customProperty=valueDefault
#---
spring.config.activate.on-profile=dev
baeldung.customProperty=valueDev