BAEL-748 quick guide to @Value (#1577)

* BAEL-748 quick guide to @Value

* BAEL-748 changes from review
This commit is contained in:
Anton 2017-04-15 04:40:39 +03:00 committed by yetanotherallisonf
parent 14909f6c2f
commit 389927d901
3 changed files with 88 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -0,0 +1,68 @@
package com.baeldung.value;
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.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("#{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;
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(spelValue);
System.out.println(spelSomeDefault);
System.out.println(someBeanValue);
System.out.println(valuesList);
}
}

View File

@ -0,0 +1,3 @@
value.from.file=Value got from the file
priority=Properties file
listOfValues=A,B,C