Close

Spring Framework - @PropertySource Examples

Spring Framework 

@Configuration
@PropertySource("classpath:app.properties")
public class PropertySourceExample {

public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(PropertySourceExample.class);

ConfigurableEnvironment env = context.getEnvironment();
String property = env.getProperty("some-strProp");
System.out.println("some-strProp value is " + property);
//printing all sources
System.out.println(env.getPropertySources());
}
}
Original Post




@Configuration
@PropertySource("classpath:app.properties")
public class PropertySourceBeanExample {
@Bean
public MyBean myBean () {
return new MyBean();
}

public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(PropertySourceBeanExample.class);
context.getBean(MyBean.class)
.showProp();
}


public static class MyBean {
@Autowired
Environment env;


public void showProp () {
System.out.println(env.getProperty("some-strProp"));
}
}
}
Original Post




This annotation provides a way to add org.springframework.core.env.PropertySource to Spring's Environment. It is used along with @Configuration annotation (Javaconfig).

@Configuration
@PropertySource("classpath:app.properties")
public class BeanValueExample {
@Bean
public MyBean myBean () {
return new MyBean();
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev () {
return new PropertySourcesPlaceholderConfigurer();
}

public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(BeanValueExample.class);
context.getBean(MyBean.class)
.showProp();
}


public static class MyBean {
@Value("${some-prop:defaultStr}")
private String str;

public void showProp () {
System.out.println(str);
}
}

}
Original Post




@PropetySource provides a class path based property source. These properties are actually registered with spring Environment as PropertySource.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@PropertySource("classpath:app-props.properties")
@Configuration
@ComponentScan(basePackageClasses = ArrayInjectionScanRefExample.class, useDefaultFilters = false,
includeFilters = @ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
value = ArrayInjectionScanRefExample.class))

public class ArrayInjectionScanRefExample {

public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
ArrayInjectionScanRefExample.class);

TestBean bean = context.getBean(TestBean.class);
System.out.println(Arrays.toString(bean.getRefBeanServices()));
}

@Component
static class TestBean {
private final RefBeanService[] refBeanServices;

@Autowired // this annotation not needed starting spring 4.3
private TestBean (RefBeanService[] refBean) {
this.refBeanServices = refBean;
}

public RefBeanService[] getRefBeanServices () {
return refBeanServices;
}
}

private static interface RefBeanService {
String getStr ();
}

@Component
private static class RefBean1 implements RefBeanService {
private String str;

@Override
public String getStr () {
return str;
}

@Value("${some-prop1:defaultStr}")
public void setStr (String str) {
this.str = str;
}

@Override
public String toString () {
return "RefBean{" +
"str='" + str + '\'' +
'}';
}
}

@Component
private static class RefBean2 implements RefBeanService {
private String str;

@Override
public String getStr () {
return str;
}

@Value("${some-prop2:defaultStr}")
public void setStr (String str) {
this.str = str;
}

@Override
public String toString () {
return "RefBean{" +
"str='" + str + '\'' +
'}';
}
}
}
Original Post




See Also