From 425586cc08bedd08c387ae43b310e22eb0fc4772 Mon Sep 17 00:00:00 2001 From: Anton Sipachev Date: Sun, 2 Apr 2017 23:43:58 +0300 Subject: [PATCH 1/2] BAEL-748 quick guide to @Value --- .../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 000000000000..39d5245049e9 --- /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 000000000000..4a90a7d85a72 --- /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 value1; + + @Value("${value.from.file}") + private String value2; + + @Value("${systemValue}") + private String value3; + + @Value("${unknown_param:some default}") + private String value4; + + @Value("${priority}") + private String value5; + + @Value("#{systemProperties['priority']}") + private String value6; + + @Value("#{systemEnvironment['priority'] ?: 'default env'}") + private String value7; + + @Value("#{someBean.someValue}") + private Integer value8; + + @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(42); + } + + @PostConstruct + public void afterInitialize() { + System.out.println(value1); + System.out.println(value2); + System.out.println(value3); + System.out.println(value4); + System.out.println(value5); + System.out.println(value6); + System.out.println(value7); + System.out.println(value8); + 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 000000000000..8c406cda9077 --- /dev/null +++ b/spring-core/src/main/resources/values.properties @@ -0,0 +1,3 @@ +value.from.file=Value got from file +priority=Properties file +listOfValues=A, B, C \ No newline at end of file From f56a201948f996f41906880ec520cf3f2d689ac7 Mon Sep 17 00:00:00 2001 From: Anton Sipachev Date: Fri, 14 Apr 2017 00:36:57 +0300 Subject: [PATCH 2/2] BAEL-748 changes from review --- .../java/com/baeldung/value/ValuesApp.java | 36 +++++++++---------- .../src/main/resources/values.properties | 4 +-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/spring-core/src/main/java/com/baeldung/value/ValuesApp.java b/spring-core/src/main/java/com/baeldung/value/ValuesApp.java index 4a90a7d85a72..25f4b9fb9c63 100644 --- a/spring-core/src/main/java/com/baeldung/value/ValuesApp.java +++ b/spring-core/src/main/java/com/baeldung/value/ValuesApp.java @@ -16,28 +16,28 @@ public class ValuesApp { @Value("string value") - private String value1; + private String stringValue; @Value("${value.from.file}") - private String value2; + private String valueFromFile; @Value("${systemValue}") - private String value3; + private String systemValue; @Value("${unknown_param:some default}") - private String value4; + private String someDefault; @Value("${priority}") - private String value5; + private String prioritySystemProperty; @Value("#{systemProperties['priority']}") - private String value6; + private String spelValue; - @Value("#{systemEnvironment['priority'] ?: 'default env'}") - private String value7; + @Value("#{systemProperties['unknown'] ?: 'some default'}") + private String spelSomeDefault; @Value("#{someBean.someValue}") - private Integer value8; + private Integer someBeanValue; @Value("#{'${listOfValues}'.split(',')}") private List valuesList; @@ -50,19 +50,19 @@ public static void main(String[] args) { @Bean public SomeBean someBean() { - return new SomeBean(42); + return new SomeBean(10); } @PostConstruct public void afterInitialize() { - System.out.println(value1); - System.out.println(value2); - System.out.println(value3); - System.out.println(value4); - System.out.println(value5); - System.out.println(value6); - System.out.println(value7); - System.out.println(value8); + 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 index 8c406cda9077..d7d61b8ee8e1 100644 --- a/spring-core/src/main/resources/values.properties +++ b/spring-core/src/main/resources/values.properties @@ -1,3 +1,3 @@ -value.from.file=Value got from file +value.from.file=Value got from the file priority=Properties file -listOfValues=A, B, C \ No newline at end of file +listOfValues=A,B,C \ No newline at end of file