[BAEL-15990] - Moved properties articles

This commit is contained in:
amit2103
2019-08-11 18:30:21 +05:30
parent 32127cc5b5
commit 84364759d6
84 changed files with 55 additions and 516 deletions
@@ -1,30 +0,0 @@
package org.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"));
}
}
@@ -1,30 +0,0 @@
package org.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;
import org.springframework.stereotype.Component;
@Component
public class ComponentUsingProperties implements InitializingBean {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
public ComponentUsingProperties() {
super();
}
//
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("in afterPropertiesSet via @Value: " + injectedProperty);
System.out.println("in afterPropertiesSet Environment: " + env.getProperty("key.something"));
}
}
@@ -1,25 +0,0 @@
package org.baeldung.properties.external;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@ComponentScan("org.baeldung.properties.core")
@PropertySource("classpath:foo.properties")
public class ExternalPropertiesWithJavaConfig {
public ExternalPropertiesWithJavaConfig() {
super();
}
// beans
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
@@ -1,16 +0,0 @@
package org.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("org.baeldung.core")
public class ExternalPropertiesWithXmlConfig {
public ExternalPropertiesWithXmlConfig() {
super();
}
}
@@ -1,16 +0,0 @@
package org.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("org.baeldung.core")
public class ExternalPropertiesWithXmlConfigOne {
public ExternalPropertiesWithXmlConfigOne() {
super();
}
}
@@ -1,14 +0,0 @@
package org.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();
}
}
@@ -1,14 +0,0 @@
package org.baeldung.properties.spring;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:foo.properties")
public class BasicPropertiesWithJavaConfig {
public BasicPropertiesWithJavaConfig() {
super();
}
}
@@ -1,24 +0,0 @@
package org.baeldung.properties.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@PropertySource("classpath:foo.properties")
@PropertySource("classpath:bar.properties")
public class PropertiesWithJavaConfig {
public PropertiesWithJavaConfig() {
super();
}
// beans
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
@@ -1,16 +0,0 @@
package org.baeldung.properties.spring;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:bar.properties")
public class PropertiesWithJavaConfigOther {
public PropertiesWithJavaConfigOther() {
super();
}
// beans
}
@@ -1,21 +0,0 @@
package org.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;
}
}
@@ -1 +0,0 @@
key.something2=val2
@@ -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>
@@ -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>
@@ -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>
@@ -1 +0,0 @@
child.name=child
@@ -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,classpath:bar.properties"/>
<bean id="componentInXmlUsingProperties" class="org.baeldung.properties.core.ComponentInXmlUsingProperties">
<constructor-arg value="${key.something}"/>
</bean>
</beans>
@@ -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="org.baeldung.properties.core.ComponentInXmlUsingProperties">
<constructor-arg value="${key.something2}"/>
</bean>
</beans>
@@ -1 +0,0 @@
key.something=val
@@ -1 +0,0 @@
parent.name=parent
@@ -1,29 +0,0 @@
package org.baeldung.properties.basic;
import org.baeldung.properties.spring.BasicPropertiesWithJavaConfig;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { BasicPropertiesWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
public class BasicPropertiesWithJavaIntegrationTest {
@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"));
}
}
@@ -1,30 +0,0 @@
package org.baeldung.properties.basic;
import org.baeldung.properties.spring.BasicPropertiesWithJavaConfig;
import org.baeldung.properties.spring.PropertiesWithJavaConfigOther;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { BasicPropertiesWithJavaConfig.class, PropertiesWithJavaConfigOther.class }, loader = AnnotationConfigContextLoader.class)
public class ExtendedPropertiesWithJavaIntegrationTest {
@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"));
}
}
@@ -1,29 +0,0 @@
package org.baeldung.properties.basic;
import org.baeldung.properties.spring.PropertiesWithJavaConfig;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PropertiesWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
public class PropertiesWithJavaIntegrationTest {
@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"));
}
}
@@ -1,27 +0,0 @@
package org.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"));
}
}
@@ -1,27 +0,0 @@
package org.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"));
}
}
@@ -1,37 +0,0 @@
package org.baeldung.properties.external;
import org.baeldung.properties.spring.PropertiesWithJavaConfigOther;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ExternalPropertiesWithJavaConfig.class, PropertiesWithJavaConfigOther.class }, loader = AnnotationConfigContextLoader.class)
@Ignore("manual only")
public class ExternalPropertiesWithJavaIntegrationTest {
@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"));
}
}
@@ -1,36 +0,0 @@
package org.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;
@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"));
}
}
@@ -1,36 +0,0 @@
package org.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;
@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"));
}
}
@@ -1,25 +0,0 @@
package org.baeldung.properties.multiple;
import org.baeldung.properties.spring.PropertiesWithJavaConfig;
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(PropertiesWithJavaConfig.class)
public class MultiplePropertiesJavaConfigIntegrationTest {
@Value("${key.something}")
private String something;
@Value("${key.something2}")
private String something2;
@Test
public void whenReadInjectedValues_thenGetCorrectValues() {
assertThat(something).isEqualTo("val");
assertThat(something2).isEqualTo("val2");
}
}
@@ -1,21 +0,0 @@
package org.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")
public class MultiplePropertiesXmlConfigIntegrationTest {
@Value("${key.something}") private String something;
@Value("${key.something2}") private String something2;
@Test
public void whenReadInjectedValues_thenGetCorrectValues() {
assertThat(something).isEqualTo("val");
assertThat(something2).isEqualTo("val2");
}
}
@@ -1,22 +0,0 @@
package org.baeldung.properties.parentchild;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ChildValueHolder {
@Value("${parent.name:-}")
private String parentName;
@Value("${child.name:-}")
private String childName;
public String getParentName() {
return parentName;
}
public String getChildName() {
return childName;
}
}
@@ -1,50 +0,0 @@
package org.baeldung.properties.parentchild;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.baeldung.properties.parentchild.config.ChildConfig2;
import org.baeldung.properties.parentchild.config.ParentConfig2;
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;
@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");
}
}
@@ -1,50 +0,0 @@
package org.baeldung.properties.parentchild;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.baeldung.properties.parentchild.config.ChildConfig;
import org.baeldung.properties.parentchild.config.ParentConfig;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextHierarchy({ @ContextConfiguration(classes = ParentConfig.class), @ContextConfiguration(classes = ChildConfig.class) })
public class ParentChildPropertySourcePropertiesIntegrationTest {
@Autowired
private WebApplicationContext wac;
@Test
public void givenPropertySource_whenGetPropertyUsingEnv_thenCorrect() {
final Environment childEnv = wac.getEnvironment();
final Environment parentEnv = wac.getParent().getEnvironment();
assertEquals(parentEnv.getProperty("parent.name"), "parent");
assertNull(parentEnv.getProperty("child.name"));
assertEquals(childEnv.getProperty("parent.name"), "parent");
assertEquals(childEnv.getProperty("child.name"), "child");
}
@Test
public void givenPropertySource_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(), "parent");
assertEquals(childValueHolder.getChildName(), "child");
}
}
@@ -1,22 +0,0 @@
package org.baeldung.properties.parentchild;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ParentValueHolder {
@Value("${parent.name:-}")
private String parentName;
@Value("${child.name:-}")
private String childName;
public String getParentName() {
return parentName;
}
public String getChildName() {
return childName;
}
}
@@ -1,21 +0,0 @@
package org.baeldung.properties.parentchild.config;
import org.baeldung.properties.parentchild.ChildValueHolder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@PropertySource("classpath:child.properties")
public class ChildConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public ChildValueHolder childValueHolder() {
return new ChildValueHolder();
}
}
@@ -1,23 +0,0 @@
package org.baeldung.properties.parentchild.config;
import org.baeldung.properties.parentchild.ChildValueHolder;
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;
@Configuration
public class ChildConfig2 {
@Bean
public ChildValueHolder childValueHolder() {
return new ChildValueHolder();
}
@Bean
public static PropertyPlaceholderConfigurer configurer() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(new ClassPathResource("child.properties"));
return ppc;
}
}
@@ -1,23 +0,0 @@
package org.baeldung.properties.parentchild.config;
import org.baeldung.properties.parentchild.ParentValueHolder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@PropertySource("classpath:parent.properties")
public class ParentConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public ParentValueHolder parentValueHolder() {
return new ParentValueHolder();
}
}
@@ -1,23 +0,0 @@
package org.baeldung.properties.parentchild.config;
import org.baeldung.properties.parentchild.ParentValueHolder;
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;
@Configuration
public class ParentConfig2 {
@Bean
public ParentValueHolder parentValueHolder() {
return new ParentValueHolder();
}
@Bean
public static PropertyPlaceholderConfigurer configurer() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(new ClassPathResource("parent.properties"));
return ppc;
}
}
@@ -1,24 +0,0 @@
package org.baeldung.test;
import org.baeldung.properties.basic.ExtendedPropertiesWithJavaIntegrationTest;
import org.baeldung.properties.basic.PropertiesWithMultipleXmlsIntegrationTest;
import org.baeldung.properties.basic.PropertiesWithXmlIntegrationTest;
import org.baeldung.properties.external.ExternalPropertiesWithJavaIntegrationTest;
import org.baeldung.properties.external.ExternalPropertiesWithMultipleXmlsIntegrationTest;
import org.baeldung.properties.external.ExternalPropertiesWithXmlManualTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ //@formatter:off
PropertiesWithXmlIntegrationTest.class,
ExternalPropertiesWithJavaIntegrationTest.class,
ExternalPropertiesWithMultipleXmlsIntegrationTest.class,
ExternalPropertiesWithXmlManualTest.class,
ExtendedPropertiesWithJavaIntegrationTest.class,
PropertiesWithMultipleXmlsIntegrationTest.class,
})// @formatter:on
public final class IntegrationTestSuite {
//
}