From 389927d901394d13cb6e1db3d14fd3a50206b99b Mon Sep 17 00:00:00 2001 From: Anton Date: Sat, 15 Apr 2017 04:40:39 +0300 Subject: [PATCH] BAEL-748 quick guide to @Value (#1577) * BAEL-748 quick guide to @Value * BAEL-748 changes from review --- .../java/com/baeldung/value/SomeBean.java | 17 +++++ .../java/com/baeldung/value/ValuesApp.java | 68 +++++++++++++++++++ .../src/main/resources/values.properties | 3 + 3 files changed, 88 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/value/SomeBean.java create mode 100644 spring-core/src/main/java/com/baeldung/value/ValuesApp.java create mode 100644 spring-core/src/main/resources/values.properties diff --git a/spring-core/src/main/java/com/baeldung/value/SomeBean.java b/spring-core/src/main/java/com/baeldung/value/SomeBean.java new file mode 100644 index 0000000000..39d5245049 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/value/SomeBean.java @@ -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; + } +} diff --git a/spring-core/src/main/java/com/baeldung/value/ValuesApp.java b/spring-core/src/main/java/com/baeldung/value/ValuesApp.java new file mode 100644 index 0000000000..25f4b9fb9c --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/value/ValuesApp.java @@ -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 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); + } +} diff --git a/spring-core/src/main/resources/values.properties b/spring-core/src/main/resources/values.properties new file mode 100644 index 0000000000..d7d61b8ee8 --- /dev/null +++ b/spring-core/src/main/resources/values.properties @@ -0,0 +1,3 @@ +value.from.file=Value got from the file +priority=Properties file +listOfValues=A,B,C \ No newline at end of file