If you can add the spring-boot
jar as a dependency you can use @ConfigurationProperties
with core spring.
package com.stackoverflow.q54119803;
import static java.util.stream.Collectors.*;
import static org.junit.Assert.*;
import java.util.List;
import java.util.stream.Stream;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;
@SuppressWarnings("javadoc")
public class So54119803 {
/** The Constant SPRING_CLASS_RULE. */
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
final String anotherPropValue = "anotherPropValue";
final List<String> expected = Stream.of("string1", this.anotherPropValue)
.collect(toList());
/** The spring method rule. */
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Autowired
Props props;
@Test
public void test() {
System.out.println(this.props);
assertEquals(this.anotherPropValue, this.props.getAnotherProp());
assertEquals(String.class, this.props.getClazz());
assertEquals(this.expected, this.props.getStrings());
}
@Configuration
@EnableConfigurationProperties
static class Config {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
final ClassPathResource location = new ClassPathResource("props.properties");
final PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setLocation(location);
return propertySourcesPlaceholderConfigurer;
}
@Bean
@ConfigurationProperties("test")
Props props() {
return new Props();
}
}
static class Props {
String anotherProp;
List<String> strings;
Class<?> clazz;
public String getAnotherProp() {
return this.anotherProp;
}
public void setAnotherProp(String anotherProp) {
this.anotherProp = anotherProp;
}
public List<String> getStrings() {
return this.strings;
}
public void setStrings(List<String> strings) {
this.strings = strings;
}
public Class<?> getClazz() {
return this.clazz;
}
public void setClazz(Class<?> clazz) {
this.clazz = clazz;
}
@Override
public String toString() {
return "Props [anotherProp=" + this.anotherProp + ", strings=" + this.strings + ", clazz=" + this.clazz
+ "]";
}
}
}
Sample props file: props.properties
#Conversions and references work fine
test.anotherProp=anotherPropValue
test.strings=string1,${test.anotherProp}
test.clazz=java.lang.String
Dependencies in example:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>