BAEL-20882 Move Spring Boot Properties module to Spring Boot modules
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
package com.baeldung.configurationproperties;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:configprops.properties")
|
||||
@ConfigurationProperties(prefix = "mail")
|
||||
@Validated
|
||||
public class ConfigProperties {
|
||||
|
||||
@Validated
|
||||
public static class Credentials {
|
||||
|
||||
@Length(max = 4, min = 1)
|
||||
private String authMethod;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public String getAuthMethod() {
|
||||
return authMethod;
|
||||
}
|
||||
|
||||
public void setAuthMethod(String authMethod) {
|
||||
this.authMethod = authMethod;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
|
||||
@NotBlank
|
||||
private String hostName;
|
||||
|
||||
@Min(1025)
|
||||
@Max(65536)
|
||||
private int port;
|
||||
|
||||
@Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$")
|
||||
private String from;
|
||||
|
||||
private Credentials credentials;
|
||||
private List<String> defaultRecipients;
|
||||
private Map<String, String> additionalHeaders;
|
||||
|
||||
public String getHostName() {
|
||||
return hostName;
|
||||
}
|
||||
|
||||
public void setHostName(String hostName) {
|
||||
this.hostName = hostName;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public Credentials getCredentials() {
|
||||
return credentials;
|
||||
}
|
||||
|
||||
public void setCredentials(Credentials credentials) {
|
||||
this.credentials = credentials;
|
||||
}
|
||||
|
||||
public List<String> getDefaultRecipients() {
|
||||
return defaultRecipients;
|
||||
}
|
||||
|
||||
public void setDefaultRecipients(List<String> defaultRecipients) {
|
||||
this.defaultRecipients = defaultRecipients;
|
||||
}
|
||||
|
||||
public Map<String, String> getAdditionalHeaders() {
|
||||
return additionalHeaders;
|
||||
}
|
||||
|
||||
public void setAdditionalHeaders(Map<String, String> additionalHeaders) {
|
||||
this.additionalHeaders = additionalHeaders;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "item")
|
||||
public Item item(){
|
||||
return new Item();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.configurationproperties;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private String name;
|
||||
private double salary;
|
||||
|
||||
public Employee(String name, double salary) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(double salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.configurationproperties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationPropertiesBinding
|
||||
public class EmployeeConverter implements Converter<String, Employee> {
|
||||
|
||||
@Override
|
||||
public Employee convert(String from) {
|
||||
String[] data = from.split(",");
|
||||
return new Employee(data[0], Double.parseDouble(data[1]));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.configurationproperties;
|
||||
|
||||
public class Item {
|
||||
|
||||
private String name;
|
||||
private int size;
|
||||
|
||||
public Item() {
|
||||
}
|
||||
|
||||
public Item(String name, int size) {
|
||||
this.name = name;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.configurationproperties;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackageClasses = { PropertyConversion.class, EmployeeConverter.class })
|
||||
public class PropertiesConversionApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PropertiesConversionApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.baeldung.configurationproperties;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.convert.DataSizeUnit;
|
||||
import org.springframework.boot.convert.DurationUnit;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
import org.springframework.util.unit.DataUnit;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:conversion.properties")
|
||||
@ConfigurationProperties(prefix = "conversion")
|
||||
public class PropertyConversion {
|
||||
private Duration timeInDefaultUnit;
|
||||
|
||||
private Duration timeInNano;
|
||||
|
||||
@DurationUnit(ChronoUnit.DAYS)
|
||||
private Duration timeInDays;
|
||||
|
||||
private DataSize sizeInDefaultUnit;
|
||||
|
||||
private DataSize sizeInGB;
|
||||
|
||||
@DataSizeUnit(DataUnit.TERABYTES)
|
||||
private DataSize sizeInTB;
|
||||
|
||||
private Employee employee;
|
||||
|
||||
// Getters and setters
|
||||
|
||||
public Duration getTimeInDefaultUnit() {
|
||||
return timeInDefaultUnit;
|
||||
}
|
||||
|
||||
public void setTimeInDefaultUnit(Duration timeInDefaultUnit) {
|
||||
this.timeInDefaultUnit = timeInDefaultUnit;
|
||||
}
|
||||
|
||||
public Duration getTimeInNano() {
|
||||
return timeInNano;
|
||||
}
|
||||
|
||||
public void setTimeInNano(Duration timeInNano) {
|
||||
this.timeInNano = timeInNano;
|
||||
}
|
||||
|
||||
public Duration getTimeInDays() {
|
||||
return timeInDays;
|
||||
}
|
||||
|
||||
public void setTimeInDays(Duration timeInDays) {
|
||||
this.timeInDays = timeInDays;
|
||||
}
|
||||
|
||||
public DataSize getSizeInDefaultUnit() {
|
||||
return sizeInDefaultUnit;
|
||||
}
|
||||
|
||||
public void setSizeInDefaultUnit(DataSize sizeInDefaultUnit) {
|
||||
this.sizeInDefaultUnit = sizeInDefaultUnit;
|
||||
}
|
||||
|
||||
public DataSize getSizeInGB() {
|
||||
return sizeInGB;
|
||||
}
|
||||
|
||||
public void setSizeInGB(DataSize sizeInGB) {
|
||||
this.sizeInGB = sizeInGB;
|
||||
}
|
||||
|
||||
public DataSize getSizeInTB() {
|
||||
return sizeInTB;
|
||||
}
|
||||
|
||||
public void setSizeInTB(DataSize sizeInTB) {
|
||||
this.sizeInTB = sizeInTB;
|
||||
}
|
||||
|
||||
public Employee getEmployee() {
|
||||
return employee;
|
||||
}
|
||||
|
||||
public void setEmployee(Employee employee) {
|
||||
this.employee = employee;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(AdditionalProperties.class)
|
||||
public class AdditionalConfiguration {
|
||||
|
||||
@Autowired
|
||||
private AdditionalProperties additionalProperties;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "additional")
|
||||
public class AdditionalProperties {
|
||||
|
||||
private String unit;
|
||||
private int max;
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
public int getMax() {
|
||||
return max;
|
||||
}
|
||||
|
||||
public void setMax(int max) {
|
||||
this.max = max;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
import com.baeldung.configurationproperties.ConfigProperties;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class })
|
||||
public class ConfigPropertiesDemoApplication {
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer())
|
||||
.run();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "custom")
|
||||
public class CustomJsonProperties {
|
||||
|
||||
private String host;
|
||||
|
||||
private int port;
|
||||
|
||||
private boolean resend;
|
||||
|
||||
private Person sender;
|
||||
|
||||
public static class Person {
|
||||
|
||||
private String name;
|
||||
private String address;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public boolean isResend() {
|
||||
return resend;
|
||||
}
|
||||
|
||||
public void setResend(boolean resend) {
|
||||
this.resend = resend;
|
||||
}
|
||||
|
||||
public Person getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(Person sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class)
|
||||
@ConfigurationProperties
|
||||
public class JsonProperties {
|
||||
|
||||
private String host;
|
||||
|
||||
private int port;
|
||||
|
||||
private boolean resend;
|
||||
|
||||
private List<String> topics;
|
||||
|
||||
private LinkedHashMap<String, ?> sender;
|
||||
|
||||
public LinkedHashMap<String, ?> getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(LinkedHashMap<String, ?> sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
public List<String> getTopics() {
|
||||
return topics;
|
||||
}
|
||||
|
||||
public void setTopics(List<String> topics) {
|
||||
this.topics = topics;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public boolean isResend() {
|
||||
return resend;
|
||||
}
|
||||
|
||||
public void setResend(boolean resend) {
|
||||
this.resend = resend;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JsonPropertyContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
|
||||
private final static String CUSTOM_PREFIX = "custom.";
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
|
||||
try {
|
||||
Resource resource = configurableApplicationContext.getResource("classpath:configprops.json");
|
||||
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
|
||||
Set<Map.Entry> set = readValue.entrySet();
|
||||
List<MapPropertySource> propertySources = convertEntrySet(set, Optional.empty());
|
||||
for (PropertySource propertySource : propertySources) {
|
||||
configurableApplicationContext.getEnvironment()
|
||||
.getPropertySources()
|
||||
.addFirst(propertySource);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<MapPropertySource> convertEntrySet(Set<Map.Entry> entrySet, Optional<String> parentKey) {
|
||||
return entrySet.stream()
|
||||
.map((Map.Entry e) -> convertToPropertySourceList(e, parentKey))
|
||||
.flatMap(Collection::stream)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static List<MapPropertySource> convertToPropertySourceList(Map.Entry e, Optional<String> parentKey) {
|
||||
String key = parentKey.map(s -> s + ".")
|
||||
.orElse("") + (String) e.getKey();
|
||||
Object value = e.getValue();
|
||||
return covertToPropertySourceList(key, value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static List<MapPropertySource> covertToPropertySourceList(String key, Object value) {
|
||||
if (value instanceof LinkedHashMap) {
|
||||
LinkedHashMap map = (LinkedHashMap) value;
|
||||
Set<Map.Entry> entrySet = map.entrySet();
|
||||
return convertEntrySet(entrySet, Optional.ofNullable(key));
|
||||
}
|
||||
String finalKey = CUSTOM_PREFIX + key;
|
||||
return Collections.singletonList(new MapPropertySource(finalKey, Collections.singletonMap(finalKey, value)));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.support.EncodedResource;
|
||||
import org.springframework.core.io.support.PropertySourceFactory;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JsonPropertySourceFactory implements PropertySourceFactory {
|
||||
|
||||
@Override
|
||||
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
|
||||
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
|
||||
return new MapPropertySource("json-property", readValue);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
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"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
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;
|
||||
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"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
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("org.baeldung.core")
|
||||
public class ExternalPropertiesWithXmlConfig {
|
||||
|
||||
public ExternalPropertiesWithXmlConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
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("org.baeldung.core")
|
||||
public class ExternalPropertiesWithXmlConfigOne {
|
||||
|
||||
public ExternalPropertiesWithXmlConfigOne() {
|
||||
super();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.properties.reloading;
|
||||
|
||||
import com.baeldung.properties.reloading.configs.ReloadableProperties;
|
||||
import java.io.File;
|
||||
import java.util.Properties;
|
||||
import org.apache.commons.configuration.PropertiesConfiguration;
|
||||
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootPropertiesApplication {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "spring.config.location", matchIfMissing = false)
|
||||
public PropertiesConfiguration propertiesConfiguration(
|
||||
@Value("${spring.config.location}") String path,
|
||||
@Value("${spring.properties.refreshDelay}") long refreshDelay) throws Exception {
|
||||
String filePath = path.substring("file:".length());
|
||||
PropertiesConfiguration configuration = new PropertiesConfiguration(new File(filePath).getCanonicalPath());
|
||||
FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
|
||||
fileChangedReloadingStrategy.setRefreshDelay(refreshDelay);
|
||||
configuration.setReloadingStrategy(fileChangedReloadingStrategy);
|
||||
return configuration;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(PropertiesConfiguration.class)
|
||||
@Primary
|
||||
public Properties properties(PropertiesConfiguration propertiesConfiguration) throws Exception {
|
||||
ReloadableProperties properties = new ReloadableProperties(propertiesConfiguration);
|
||||
return properties;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootPropertiesApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.properties.reloading.configs;
|
||||
|
||||
public class PropertiesException extends RuntimeException {
|
||||
public PropertiesException() {
|
||||
}
|
||||
|
||||
public PropertiesException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.properties.reloading.configs;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.util.Properties;
|
||||
import javax.naming.OperationNotSupportedException;
|
||||
import org.apache.commons.configuration.PropertiesConfiguration;
|
||||
|
||||
public class ReloadableProperties extends Properties {
|
||||
private PropertiesConfiguration propertiesConfiguration;
|
||||
|
||||
public ReloadableProperties(PropertiesConfiguration propertiesConfiguration) throws IOException {
|
||||
super.load(new FileReader(propertiesConfiguration.getFile()));
|
||||
this.propertiesConfiguration = propertiesConfiguration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Object setProperty(String key, String value) {
|
||||
propertiesConfiguration.setProperty(key, value);
|
||||
return super.setProperty(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProperty(String key) {
|
||||
String val = propertiesConfiguration.getString(key);
|
||||
super.setProperty(key, val);
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProperty(String key, String defaultValue) {
|
||||
String val = propertiesConfiguration.getString(key, defaultValue);
|
||||
super.setProperty(key, val);
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void load(Reader reader) throws IOException {
|
||||
throw new PropertiesException(new OperationNotSupportedException());
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void load(InputStream inStream) throws IOException {
|
||||
throw new PropertiesException(new OperationNotSupportedException());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.properties.reloading.configs;
|
||||
|
||||
import org.apache.commons.configuration.PropertiesConfiguration;
|
||||
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class ReloadablePropertySource extends PropertySource {
|
||||
|
||||
PropertiesConfiguration propertiesConfiguration;
|
||||
|
||||
public ReloadablePropertySource(String name, PropertiesConfiguration propertiesConfiguration) {
|
||||
super(name);
|
||||
this.propertiesConfiguration = propertiesConfiguration;
|
||||
}
|
||||
|
||||
public ReloadablePropertySource(String name, String path) {
|
||||
super(StringUtils.isEmpty(name) ? path : name);
|
||||
try {
|
||||
this.propertiesConfiguration = new PropertiesConfiguration(path);
|
||||
FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
|
||||
strategy.setRefreshDelay(1000);
|
||||
this.propertiesConfiguration.setReloadingStrategy(strategy);
|
||||
} catch (Exception e) {
|
||||
throw new PropertiesException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getProperty(String s) {
|
||||
return propertiesConfiguration.getProperty(s);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.properties.reloading.configs;
|
||||
|
||||
import org.apache.commons.configuration.PropertiesConfiguration;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
|
||||
@Configuration
|
||||
public class ReloadablePropertySourceConfig {
|
||||
|
||||
private ConfigurableEnvironment env;
|
||||
|
||||
public ReloadablePropertySourceConfig(@Autowired ConfigurableEnvironment env) {
|
||||
this.env = env;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "spring.config.location", matchIfMissing = false)
|
||||
public ReloadablePropertySource reloadablePropertySource(PropertiesConfiguration properties) {
|
||||
ReloadablePropertySource ret = new ReloadablePropertySource("dynamic", properties);
|
||||
MutablePropertySources sources = env.getPropertySources();
|
||||
sources.addFirst(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.properties.reloading.configs;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.FileUrlResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.DefaultPropertySourceFactory;
|
||||
import org.springframework.core.io.support.EncodedResource;
|
||||
|
||||
public class ReloadablePropertySourceFactory extends DefaultPropertySourceFactory {
|
||||
@Override
|
||||
public PropertySource<?> createPropertySource(String s, EncodedResource encodedResource) throws IOException {
|
||||
Resource internal = encodedResource.getResource();
|
||||
if (internal instanceof FileSystemResource) {
|
||||
return new ReloadablePropertySource(s, ((FileSystemResource) internal).getPath());
|
||||
}
|
||||
if (internal instanceof FileUrlResource) {
|
||||
return new ReloadablePropertySource(s, ((FileUrlResource) internal)
|
||||
.getURL()
|
||||
.getPath());
|
||||
}
|
||||
return super.createPropertySource(s, encodedResource);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.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
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.value;
|
||||
|
||||
public class ClassNotManagedBySpring {
|
||||
|
||||
private String customVariable;
|
||||
private String anotherCustomVariable;
|
||||
|
||||
public ClassNotManagedBySpring(String someInitialValue, String anotherManagedValue) {
|
||||
this.customVariable = someInitialValue;
|
||||
this.anotherCustomVariable = anotherManagedValue;
|
||||
}
|
||||
|
||||
public String getCustomVariable() {
|
||||
return customVariable;
|
||||
}
|
||||
|
||||
public void setCustomVariable(String customVariable) {
|
||||
this.customVariable = customVariable;
|
||||
}
|
||||
|
||||
public String getAnotherCustomVariable() {
|
||||
return anotherCustomVariable;
|
||||
}
|
||||
|
||||
public void setAnotherCustomVariable(String anotherCustomVariable) {
|
||||
this.anotherCustomVariable = anotherCustomVariable;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.value;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class InitializerBean {
|
||||
|
||||
private String someInitialValue;
|
||||
private String anotherManagedValue;
|
||||
|
||||
public InitializerBean(@Value("someInitialValue") String someInitialValue, @Value("anotherValue") String anotherManagedValue) {
|
||||
this.someInitialValue = someInitialValue;
|
||||
this.anotherManagedValue = anotherManagedValue;
|
||||
}
|
||||
|
||||
public ClassNotManagedBySpring initClass() {
|
||||
return new ClassNotManagedBySpring(this.someInitialValue, this.anotherManagedValue);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.value;
|
||||
|
||||
public class SomeBean {
|
||||
private int someValue;
|
||||
|
||||
public SomeBean(int someValue) {
|
||||
this.someValue = someValue;
|
||||
}
|
||||
|
||||
public int getSomeValue() {
|
||||
return someValue;
|
||||
}
|
||||
|
||||
public void setSomeValue(int someValue) {
|
||||
this.someValue = someValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.baeldung.value;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@Configuration
|
||||
@PropertySource(name = "myProperties", value = "values.properties")
|
||||
public class ValuesApp {
|
||||
|
||||
@Value("string value")
|
||||
private String stringValue;
|
||||
|
||||
@Value("${value.from.file}")
|
||||
private String valueFromFile;
|
||||
|
||||
@Value("${systemValue}")
|
||||
private String systemValue;
|
||||
|
||||
@Value("${unknown_param:some default}")
|
||||
private String someDefault;
|
||||
|
||||
@Value("${priority}")
|
||||
private String prioritySystemProperty;
|
||||
|
||||
@Value("${listOfValues}")
|
||||
private String[] valuesArray;
|
||||
|
||||
@Value("#{systemProperties['priority']}")
|
||||
private String spelValue;
|
||||
|
||||
@Value("#{systemProperties['unknown'] ?: 'some default'}")
|
||||
private String spelSomeDefault;
|
||||
|
||||
@Value("#{someBean.someValue}")
|
||||
private Integer someBeanValue;
|
||||
|
||||
@Value("#{'${listOfValues}'.split(',')}")
|
||||
private List<String> valuesList;
|
||||
|
||||
@Value("#{${valuesMap}}")
|
||||
private Map<String, Integer> valuesMap;
|
||||
|
||||
@Value("#{${valuesMap}.key1}")
|
||||
private Integer valuesMapKey1;
|
||||
|
||||
@Value("#{${valuesMap}['unknownKey']}")
|
||||
private Integer unknownMapKey;
|
||||
|
||||
@Value("#{${unknownMap : {key1:'1', key2 : '2'}}}")
|
||||
private Map<String, Integer> unknownMap;
|
||||
|
||||
@Value("#{${valuesMap}['unknownKey'] ?: 5}")
|
||||
private Integer unknownMapKeyWithDefaultValue;
|
||||
|
||||
@Value("#{${valuesMap}.?[value>'1']}")
|
||||
private Map<String, Integer> valuesMapFiltered;
|
||||
|
||||
@Value("#{systemProperties}")
|
||||
private Map<String, String> systemPropertiesMap;
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("systemValue", "Some system parameter value");
|
||||
System.setProperty("priority", "System property");
|
||||
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValuesApp.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SomeBean someBean() {
|
||||
return new SomeBean(10);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void afterInitialize() {
|
||||
System.out.println(stringValue);
|
||||
System.out.println(valueFromFile);
|
||||
System.out.println(systemValue);
|
||||
System.out.println(someDefault);
|
||||
System.out.println(prioritySystemProperty);
|
||||
System.out.println(Arrays.toString(valuesArray));
|
||||
System.out.println(spelValue);
|
||||
System.out.println(spelSomeDefault);
|
||||
System.out.println(someBeanValue);
|
||||
System.out.println(valuesList);
|
||||
System.out.println(valuesMap);
|
||||
System.out.println(valuesMapKey1);
|
||||
System.out.println(unknownMapKey);
|
||||
System.out.println(unknownMap);
|
||||
System.out.println(unknownMapKeyWithDefaultValue);
|
||||
System.out.println(valuesMapFiltered);
|
||||
System.out.println(systemPropertiesMap);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.baeldung.valuewithdefaults;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.primitives.Ints;
|
||||
|
||||
/**
|
||||
* Demonstrates setting defaults for @Value annotation. Note that there are no properties
|
||||
* defined in the specified property source. We also assume that the user here
|
||||
* does not have a system property named some.key.
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
@PropertySource(name = "myProperties", value = "valueswithdefaults.properties")
|
||||
public class ValuesWithDefaultsApp {
|
||||
|
||||
@Value("${some.key:my default value}")
|
||||
private String stringWithDefaultValue;
|
||||
|
||||
@Value("${some.key:}")
|
||||
private String stringWithBlankDefaultValue;
|
||||
|
||||
@Value("${some.key:true}")
|
||||
private boolean booleanWithDefaultValue;
|
||||
|
||||
@Value("${some.key:42}")
|
||||
private int intWithDefaultValue;
|
||||
|
||||
@Value("${some.key:one,two,three}")
|
||||
private String[] stringArrayWithDefaults;
|
||||
|
||||
@Value("${some.key:1,2,3}")
|
||||
private int[] intArrayWithDefaults;
|
||||
|
||||
@Value("#{systemProperties['some.key'] ?: 'my default system property value'}")
|
||||
private String spelWithDefaultValue;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValuesWithDefaultsApp.class);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void afterInitialize() {
|
||||
// strings
|
||||
Assert.isTrue(stringWithDefaultValue.equals("my default value"));
|
||||
Assert.isTrue(stringWithBlankDefaultValue.equals(""));
|
||||
|
||||
// other primitives
|
||||
Assert.isTrue(booleanWithDefaultValue);
|
||||
Assert.isTrue(intWithDefaultValue == 42);
|
||||
|
||||
// arrays
|
||||
List<String> stringListValues = Lists.newArrayList("one", "two", "three");
|
||||
Assert.isTrue(Arrays.asList(stringArrayWithDefaults).containsAll(stringListValues));
|
||||
|
||||
List<Integer> intListValues = Lists.newArrayList(1, 2, 3);
|
||||
Assert.isTrue(Ints.asList(intArrayWithDefaults).containsAll(intListValues));
|
||||
|
||||
// SpEL
|
||||
Assert.isTrue(spelWithDefaultValue.equals("my default system property value"));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.baeldung.yaml;
|
||||
|
||||
import java.util.Collections;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MyApplication implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
private YAMLConfig myConfig;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication app = new SpringApplication(MyApplication.class);
|
||||
app.run();
|
||||
}
|
||||
|
||||
public void run(String... args) throws Exception {
|
||||
System.out.println("using environment:" + myConfig.getEnvironment());
|
||||
System.out.println("name:" + myConfig.getName());
|
||||
System.out.println("servers:" + myConfig.getServers());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.yaml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
@ConfigurationProperties
|
||||
public class YAMLConfig {
|
||||
private String name;
|
||||
private String environment;
|
||||
private List<String> servers = new ArrayList<String>();
|
||||
|
||||
public List<String> getServers() {
|
||||
return servers;
|
||||
}
|
||||
|
||||
public void setServers(List<String> servers) {
|
||||
this.servers = servers;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEnvironment() {
|
||||
return environment;
|
||||
}
|
||||
|
||||
public void setEnvironment(String environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
management.endpoints.web.exposure.include=refresh
|
||||
spring.properties.refreshDelay=1000
|
||||
spring.config.location=file:extra.properties
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
@@ -0,0 +1,17 @@
|
||||
spring:
|
||||
profiles: test
|
||||
name: test-YAML
|
||||
environment: test
|
||||
servers:
|
||||
- www.abc.test.com
|
||||
- www.xyz.test.com
|
||||
|
||||
---
|
||||
|
||||
spring:
|
||||
profiles: prod
|
||||
name: prod-YAML
|
||||
environment: production
|
||||
servers:
|
||||
- www.abc.com
|
||||
- www.xyz.com
|
||||
@@ -0,0 +1 @@
|
||||
key.something2=val2
|
||||
@@ -0,0 +1,12 @@
|
||||
<?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>
|
||||
@@ -0,0 +1,12 @@
|
||||
<?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>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?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>
|
||||
@@ -0,0 +1 @@
|
||||
child.name=child
|
||||
@@ -0,0 +1,16 @@
|
||||
<?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="com.baeldung.properties.core.ComponentInXmlUsingProperties">
|
||||
<constructor-arg value="${key.something}"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,16 @@
|
||||
<?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>
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"host" : "mailer@mail.com",
|
||||
"port" : 9090,
|
||||
"resend" : true,
|
||||
"topics" : ["spring", "boot"],
|
||||
"sender" : {
|
||||
"name": "sender",
|
||||
"address": "street"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#Simple properties
|
||||
mail.hostname=host@mail.com
|
||||
mail.port=9000
|
||||
mail.from=mailer@mail.com
|
||||
|
||||
#List properties
|
||||
mail.defaultRecipients[0]=admin@mail.com
|
||||
mail.defaultRecipients[1]=owner@mail.com
|
||||
|
||||
#Map Properties
|
||||
mail.additionalHeaders.redelivery=true
|
||||
mail.additionalHeaders.secure=true
|
||||
mail.additionalHeaders.p3=value
|
||||
|
||||
#Object properties
|
||||
mail.credentials.username=john
|
||||
mail.credentials.password=password
|
||||
mail.credentials.authMethod=SHA1
|
||||
|
||||
#Bean method properties
|
||||
item.name=Item name
|
||||
item.size=42
|
||||
|
||||
|
||||
#Additional properties
|
||||
additional.unit=km
|
||||
additional.max=100
|
||||
@@ -0,0 +1,2 @@
|
||||
conversion.timeInDefaultUnit=10
|
||||
conversion.timeInNano=9ns
|
||||
@@ -0,0 +1 @@
|
||||
key.something=val
|
||||
@@ -0,0 +1 @@
|
||||
parent.name=parent
|
||||
@@ -0,0 +1,4 @@
|
||||
value.from.file=Value got from the file
|
||||
priority=Properties file
|
||||
listOfValues=A,B,C
|
||||
valuesMap={key1:'1', key2 : '2', key3 : '3'}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.configurationproperties;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.configurationproperties.ConfigProperties;
|
||||
import com.baeldung.properties.AdditionalProperties;
|
||||
import com.baeldung.properties.ConfigPropertiesDemoApplication;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ConfigPropertiesDemoApplication.class)
|
||||
@TestPropertySource("classpath:configprops-test.properties")
|
||||
public class ConfigPropertiesIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ConfigProperties properties;
|
||||
|
||||
@Autowired
|
||||
private AdditionalProperties additionalProperties;
|
||||
|
||||
@Test
|
||||
public void whenSimplePropertyQueriedthenReturnsProperty() throws Exception {
|
||||
Assert.assertTrue("From address is read as null!", properties.getFrom() != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenListPropertyQueriedthenReturnsProperty() throws Exception {
|
||||
Assert.assertTrue("Couldn't bind list property!", properties.getDefaultRecipients().size() == 2);
|
||||
Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", properties.getDefaultRecipients().size() == 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMapPropertyQueriedthenReturnsProperty() throws Exception {
|
||||
Assert.assertTrue("Couldn't bind map property!", properties.getAdditionalHeaders() != null);
|
||||
Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", properties.getAdditionalHeaders().size() == 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenObjectPropertyQueriedthenReturnsProperty() throws Exception {
|
||||
Assert.assertTrue("Couldn't bind map property!", properties.getCredentials() != null);
|
||||
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getAuthMethod().equals("SHA1"));
|
||||
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getUsername().equals("john"));
|
||||
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getPassword().equals("password"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAdditionalPropertyQueriedthenReturnsProperty() {
|
||||
Assert.assertTrue(additionalProperties.getUnit().equals("km"));
|
||||
Assert.assertTrue(additionalProperties.getMax() == 100);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.configurationproperties;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
|
||||
import com.baeldung.configurationproperties.PropertiesConversionApplication;
|
||||
import com.baeldung.configurationproperties.PropertyConversion;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = PropertiesConversionApplication.class)
|
||||
@TestPropertySource("classpath:conversion.properties")
|
||||
public class PropertiesConversionIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private PropertyConversion properties;
|
||||
|
||||
@Test
|
||||
public void whenUseTimeUnitPropertyConversion_thenSuccess() throws Exception {
|
||||
assertEquals(Duration.ofMillis(10), properties.getTimeInDefaultUnit());
|
||||
assertEquals(Duration.ofNanos(9), properties.getTimeInNano());
|
||||
assertEquals(Duration.ofDays(2), properties.getTimeInDays());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseDataSizePropertyConversion_thenSuccess() throws Exception {
|
||||
assertEquals(DataSize.ofBytes(300), properties.getSizeInDefaultUnit());
|
||||
assertEquals(DataSize.ofGigabytes(2), properties.getSizeInGB());
|
||||
assertEquals(DataSize.ofTerabytes(4), properties.getSizeInTB());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseCustomPropertyConverter_thenSuccess() throws Exception {
|
||||
assertEquals("john", properties.getEmployee().getName());
|
||||
assertEquals(2000.0, properties.getEmployee().getSalary());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = ConfigPropertiesDemoApplication.class, initializers = JsonPropertyContextInitializer.class)
|
||||
public class JsonPropertiesIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private JsonProperties jsonProperties;
|
||||
|
||||
@Autowired
|
||||
private CustomJsonProperties customJsonProperties;
|
||||
|
||||
@Test
|
||||
public void whenPropertiesLoadedViaJsonPropertySource_thenLoadFlatValues() {
|
||||
Assert.assertEquals("mailer@mail.com", jsonProperties.getHost());
|
||||
Assert.assertEquals(9090, jsonProperties.getPort());
|
||||
Assert.assertTrue(jsonProperties.isResend());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPropertiesLoadedViaJsonPropertySource_thenLoadListValues() {
|
||||
Assert.assertThat(jsonProperties.getTopics(), Matchers.is(Arrays.asList("spring", "boot")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPropertiesLoadedViaJsonPropertySource_thenNestedLoadedAsMap() {
|
||||
Assert.assertEquals("sender", jsonProperties.getSender()
|
||||
.get("name"));
|
||||
Assert.assertEquals("street", jsonProperties.getSender()
|
||||
.get("address"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadedIntoEnvironment_thenFlatValuesPopulated() {
|
||||
Assert.assertEquals("mailer@mail.com", customJsonProperties.getHost());
|
||||
Assert.assertEquals(9090, customJsonProperties.getPort());
|
||||
Assert.assertTrue(customJsonProperties.isResend());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() {
|
||||
Assert.assertNotNull(customJsonProperties.getSender());
|
||||
Assert.assertEquals("sender", customJsonProperties.getSender()
|
||||
.getName());
|
||||
Assert.assertEquals("street", customJsonProperties.getSender()
|
||||
.getAddress());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
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;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.properties.spring.BasicPropertiesWithJavaConfig;
|
||||
|
||||
@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"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
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;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.properties.spring.BasicPropertiesWithJavaConfig;
|
||||
import com.baeldung.properties.spring.PropertiesWithJavaConfigOther;
|
||||
|
||||
@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"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
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;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.properties.spring.PropertiesWithJavaConfig;
|
||||
|
||||
@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"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
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"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
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"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
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.ExternalPropertiesWithJavaConfig;
|
||||
import com.baeldung.properties.spring.PropertiesWithJavaConfigOther;
|
||||
|
||||
@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"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
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"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
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"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
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 com.baeldung.properties.spring.PropertiesWithJavaConfig;
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
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")
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
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.ChildConfig;
|
||||
import com.baeldung.properties.parentchild.config.ParentConfig;
|
||||
|
||||
@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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.properties.parentchild.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||
|
||||
import com.baeldung.properties.parentchild.ChildValueHolder;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:child.properties")
|
||||
public class ChildConfig {
|
||||
@Bean
|
||||
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
|
||||
return new PropertySourcesPlaceholderConfigurer();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ChildValueHolder childValueHolder() {
|
||||
return new ChildValueHolder();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.properties.parentchild.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||
|
||||
import com.baeldung.properties.parentchild.ParentValueHolder;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:parent.properties")
|
||||
public class ParentConfig {
|
||||
|
||||
@Bean
|
||||
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
|
||||
return new PropertySourcesPlaceholderConfigurer();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ParentValueHolder parentValueHolder() {
|
||||
return new ParentValueHolder();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
package com.baeldung.properties.reloading;
|
||||
|
||||
import com.baeldung.properties.reloading.beans.ConfigurationPropertiesRefreshConfigBean;
|
||||
import com.baeldung.properties.reloading.beans.EnvironmentConfigBean;
|
||||
import com.baeldung.properties.reloading.beans.PropertiesConfigBean;
|
||||
import com.baeldung.properties.reloading.beans.ValueRefreshConfigBean;
|
||||
import java.io.FileOutputStream;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = SpringBootPropertiesTestApplication.class)
|
||||
public class PropertiesReloadIntegrationTest {
|
||||
|
||||
protected MockMvc mvc;
|
||||
|
||||
protected long refreshDelay = 3000;
|
||||
|
||||
@Autowired
|
||||
WebApplicationContext webApplicationContext;
|
||||
|
||||
@Autowired
|
||||
ValueRefreshConfigBean valueRefreshConfigBean;
|
||||
|
||||
@Autowired
|
||||
ConfigurationPropertiesRefreshConfigBean configurationPropertiesRefreshConfigBean;
|
||||
|
||||
@Autowired
|
||||
EnvironmentConfigBean environmentConfigBean;
|
||||
|
||||
@Autowired
|
||||
PropertiesConfigBean propertiesConfigBean;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("singletonValueRefreshConfigBean")
|
||||
ValueRefreshConfigBean singletonValueRefreshConfigBean;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
mvc = MockMvcBuilders
|
||||
.webAppContextSetup(webApplicationContext)
|
||||
.build();
|
||||
createConfig("extra.properties", "application.theme.color", "blue");
|
||||
createConfig("extra2.properties", "application.theme.background", "red");
|
||||
Thread.sleep(refreshDelay);
|
||||
callRefresh();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
createConfig("extra.properties", "application.theme.color", "blue");
|
||||
createConfig("extra2.properties", "application.theme.background", "red");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEnvironmentReader_whenColorChanged_thenExpectChangeValue() throws Exception {
|
||||
Assert.assertEquals("blue", environmentConfigBean.getColor());
|
||||
|
||||
createConfig("extra.properties", "application.theme.color", "red");
|
||||
Thread.sleep(refreshDelay);
|
||||
|
||||
Assert.assertEquals("red", environmentConfigBean.getColor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEnvironmentReader_whenBackgroundChanged_thenExpectChangeValue() throws Exception {
|
||||
Assert.assertEquals("red", environmentConfigBean.getBackgroundColor());
|
||||
|
||||
createConfig("extra2.properties", "application.theme.background", "blue");
|
||||
Thread.sleep(refreshDelay);
|
||||
|
||||
Assert.assertEquals("blue", environmentConfigBean.getBackgroundColor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPropertiesReader_whenColorChanged_thenExpectChangeValue() throws Exception {
|
||||
Assert.assertEquals("blue", propertiesConfigBean.getColor());
|
||||
|
||||
createConfig("extra.properties", "application.theme.color", "red");
|
||||
Thread.sleep(refreshDelay);
|
||||
|
||||
Assert.assertEquals("red", propertiesConfigBean.getColor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRefreshScopedValueReader_whenColorChangedAndRefreshCalled_thenExpectChangeValue() throws Exception {
|
||||
Assert.assertEquals("blue", valueRefreshConfigBean.getColor());
|
||||
|
||||
createConfig("extra.properties", "application.theme.color", "red");
|
||||
Thread.sleep(refreshDelay);
|
||||
|
||||
Assert.assertEquals("blue", valueRefreshConfigBean.getColor());
|
||||
|
||||
callRefresh();
|
||||
|
||||
Assert.assertEquals("red", valueRefreshConfigBean.getColor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSingletonRefreshScopedValueReader_whenColorChangedAndRefreshCalled_thenExpectOldValue() throws Exception {
|
||||
|
||||
Assert.assertEquals("blue", singletonValueRefreshConfigBean.getColor());
|
||||
|
||||
createConfig("extra.properties", "application.theme.color", "red");
|
||||
Thread.sleep(refreshDelay);
|
||||
|
||||
Assert.assertEquals("blue", singletonValueRefreshConfigBean.getColor());
|
||||
|
||||
callRefresh();
|
||||
|
||||
Assert.assertEquals("blue", singletonValueRefreshConfigBean.getColor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRefreshScopedConfigurationPropertiesReader_whenColorChangedAndRefreshCalled_thenExpectChangeValue() throws Exception {
|
||||
|
||||
Assert.assertEquals("blue", configurationPropertiesRefreshConfigBean.getColor());
|
||||
|
||||
createConfig("extra.properties", "application.theme.color", "red");
|
||||
Thread.sleep(refreshDelay);
|
||||
|
||||
Assert.assertEquals("blue", configurationPropertiesRefreshConfigBean.getColor());
|
||||
|
||||
callRefresh();
|
||||
|
||||
Assert.assertEquals("red", configurationPropertiesRefreshConfigBean.getColor());
|
||||
}
|
||||
|
||||
public void callRefresh() throws Exception {
|
||||
MvcResult mvcResult = mvc
|
||||
.perform(MockMvcRequestBuilders
|
||||
.post("/actuator/refresh")
|
||||
.accept(MediaType.APPLICATION_JSON_VALUE))
|
||||
.andReturn();
|
||||
MockHttpServletResponse response = mvcResult.getResponse();
|
||||
Assert.assertEquals(response.getStatus(), 200);
|
||||
}
|
||||
|
||||
public void createConfig(String file, String key, String value) throws Exception {
|
||||
FileOutputStream fo = new FileOutputStream(file);
|
||||
fo.write(String
|
||||
.format("%s=%s", key, value)
|
||||
.getBytes());
|
||||
fo.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.properties.reloading;
|
||||
|
||||
import com.baeldung.properties.reloading.beans.ValueRefreshConfigBean;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@SpringBootApplication
|
||||
@WebAppConfiguration
|
||||
public class SpringBootPropertiesTestApplication {
|
||||
|
||||
@Bean("singletonValueRefreshConfigBean")
|
||||
@RefreshScope
|
||||
@Scope("singleton")
|
||||
public ValueRefreshConfigBean singletonValueRefreshConfigBean(@Value("${application.theme.color:null}") String val) {
|
||||
return new ValueRefreshConfigBean(val);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@RefreshScope
|
||||
public ValueRefreshConfigBean valueRefreshConfigBean(@Value("${application.theme.color:null}") String val) {
|
||||
return new ValueRefreshConfigBean(val);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.properties.reloading.beans;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "application.theme")
|
||||
@RefreshScope
|
||||
public class ConfigurationPropertiesRefreshConfigBean {
|
||||
private String color;
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.properties.reloading.beans;
|
||||
|
||||
import com.baeldung.properties.reloading.configs.ReloadablePropertySourceFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@PropertySource(value = "file:extra2.properties", factory = ReloadablePropertySourceFactory.class)
|
||||
public class EnvironmentConfigBean {
|
||||
|
||||
private Environment environment;
|
||||
|
||||
public EnvironmentConfigBean(@Autowired Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return environment.getProperty("application.theme.color");
|
||||
}
|
||||
|
||||
public String getBackgroundColor() {
|
||||
return environment.getProperty("application.theme.background");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.properties.reloading.beans;
|
||||
|
||||
import java.util.Properties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PropertiesConfigBean {
|
||||
|
||||
private Properties properties;
|
||||
|
||||
public PropertiesConfigBean(@Autowired Properties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return properties.getProperty("application.theme.color");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.properties.reloading.beans;
|
||||
|
||||
public class ValueRefreshConfigBean {
|
||||
private String color;
|
||||
|
||||
public ValueRefreshConfigBean(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.properties.testproperty;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@TestPropertySource("/foo.properties")
|
||||
public class FilePropertyInjectionUnitTest {
|
||||
|
||||
@Value("${foo}")
|
||||
private String foo;
|
||||
|
||||
@Test
|
||||
public void whenFilePropertyProvided_thenProperlyInjected() {
|
||||
assertThat(foo).isEqualTo("bar");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.properties.testproperty;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@TestPropertySource(properties = {"foo=bar"})
|
||||
public class PropertyInjectionUnitTest {
|
||||
|
||||
@Value("${foo}")
|
||||
private String foo;
|
||||
|
||||
@Test
|
||||
public void whenPropertyProvided_thenProperlyInjected() {
|
||||
assertThat(foo).isEqualTo("bar");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.properties.testproperty;
|
||||
|
||||
import com.baeldung.properties.reloading.SpringBootPropertiesTestApplication;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(properties = {"foo=bar"}, classes = SpringBootPropertiesTestApplication.class)
|
||||
public class SpringBootPropertyInjectionIntegrationTest {
|
||||
|
||||
@Value("${foo}")
|
||||
private String foo;
|
||||
|
||||
@Test
|
||||
public void whenSpringBootPropertyProvided_thenProperlyInjected() {
|
||||
assertThat(foo).isEqualTo("bar");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.test;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
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;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({ //@formatter:off
|
||||
PropertiesWithXmlIntegrationTest.class,
|
||||
ExternalPropertiesWithJavaIntegrationTest.class,
|
||||
ExternalPropertiesWithMultipleXmlsIntegrationTest.class,
|
||||
ExternalPropertiesWithXmlManualTest.class,
|
||||
ExtendedPropertiesWithJavaIntegrationTest.class,
|
||||
PropertiesWithMultipleXmlsIntegrationTest.class,
|
||||
})// @formatter:on
|
||||
public final class IntegrationTestSuite {
|
||||
//
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.value;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
public class ClassNotManagedBySpringIntegrationTest {
|
||||
|
||||
@MockBean
|
||||
private InitializerBean initializerBean;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
when(initializerBean.initClass())
|
||||
.thenReturn(new ClassNotManagedBySpring("This is only sample value", "Another configured value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInitializerBean_whenInvokedInitClass_shouldInitialize() throws Exception {
|
||||
|
||||
//given
|
||||
ClassNotManagedBySpring classNotManagedBySpring = initializerBean.initClass();
|
||||
|
||||
//when
|
||||
String initializedValue = classNotManagedBySpring.getCustomVariable();
|
||||
String anotherCustomVariable = classNotManagedBySpring.getAnotherCustomVariable();
|
||||
|
||||
//then
|
||||
assertEquals("This is only sample value", initializedValue);
|
||||
assertEquals("Another configured value", anotherCustomVariable);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
management.endpoints.web.exposure.include=refresh
|
||||
spring.properties.refreshDelay=1000
|
||||
spring.config.location=file:extra.properties
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
@@ -0,0 +1,28 @@
|
||||
#Simple properties
|
||||
mail.hostname=host@mail.com
|
||||
mail.port=9000
|
||||
mail.from=mailer@mail.com
|
||||
|
||||
#List properties
|
||||
mail.defaultRecipients[0]=admin@mail.com
|
||||
mail.defaultRecipients[1]=owner@mail.com
|
||||
|
||||
#Map Properties
|
||||
mail.additionalHeaders.redelivery=true
|
||||
mail.additionalHeaders.secure=true
|
||||
mail.additionalHeaders.p3=value
|
||||
|
||||
#Object properties
|
||||
mail.credentials.username=john
|
||||
mail.credentials.password=password
|
||||
mail.credentials.authMethod=SHA1
|
||||
|
||||
#Bean method properties
|
||||
item.name=Test item name
|
||||
item.size=21
|
||||
|
||||
#Additional properties
|
||||
additional.unit=km
|
||||
additional.max=100
|
||||
|
||||
key.something=val
|
||||
@@ -0,0 +1,11 @@
|
||||
###### time unit
|
||||
conversion.timeInDefaultUnit=10
|
||||
conversion.timeInNano=9ns
|
||||
conversion.timeInDays=2
|
||||
|
||||
###### data size
|
||||
conversion.sizeInDefaultUnit=300
|
||||
conversion.sizeInGB=2GB
|
||||
conversion.sizeInTB=4
|
||||
|
||||
conversion.employee=john,2000
|
||||
@@ -0,0 +1,2 @@
|
||||
foo=bar
|
||||
key.something=val
|
||||
Reference in New Issue
Block a user