Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions spring-core/src/main/java/com/baeldung/value/SomeBean.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
68 changes: 68 additions & 0 deletions spring-core/src/main/java/com/baeldung/value/ValuesApp.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 3 additions & 0 deletions spring-core/src/main/resources/values.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
value.from.file=Value got from the file
priority=Properties file
listOfValues=A,B,C