[BAEL-15990] - Moved properties articles
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.value;
|
||||
|
||||
public class ClassNotManagedBySpring {
|
||||
|
||||
private String customVariable;
|
||||
private String anotherCustomVariable;
|
||||
|
||||
public ClassNotManagedBySpring(String someInitialValue, String anotherManagedValue) {
|
||||
this.customVariable = someInitialValue;
|
||||
this.anotherCustomVariable = anotherManagedValue;
|
||||
}
|
||||
|
||||
public String getCustomVariable() {
|
||||
return customVariable;
|
||||
}
|
||||
|
||||
public void setCustomVariable(String customVariable) {
|
||||
this.customVariable = customVariable;
|
||||
}
|
||||
|
||||
public String getAnotherCustomVariable() {
|
||||
return anotherCustomVariable;
|
||||
}
|
||||
|
||||
public void setAnotherCustomVariable(String anotherCustomVariable) {
|
||||
this.anotherCustomVariable = anotherCustomVariable;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.value;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class InitializerBean {
|
||||
|
||||
private String someInitialValue;
|
||||
private String anotherManagedValue;
|
||||
|
||||
public InitializerBean(@Value("someInitialValue") String someInitialValue, @Value("anotherValue") String anotherManagedValue) {
|
||||
this.someInitialValue = someInitialValue;
|
||||
this.anotherManagedValue = anotherManagedValue;
|
||||
}
|
||||
|
||||
public ClassNotManagedBySpring initClass() {
|
||||
return new ClassNotManagedBySpring(this.someInitialValue, this.anotherManagedValue);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
package com.baeldung.value;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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("${listOfValues}")
|
||||
private String[] valuesArray;
|
||||
|
||||
@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;
|
||||
|
||||
@Value("#{${valuesMap}}")
|
||||
private Map<String, Integer> valuesMap;
|
||||
|
||||
@Value("#{${valuesMap}.key1}")
|
||||
private Integer valuesMapKey1;
|
||||
|
||||
@Value("#{${valuesMap}['unknownKey']}")
|
||||
private Integer unknownMapKey;
|
||||
|
||||
@Value("#{${unknownMap : {key1:'1', key2 : '2'}}}")
|
||||
private Map<String, Integer> unknownMap;
|
||||
|
||||
@Value("#{${valuesMap}['unknownKey'] ?: 5}")
|
||||
private Integer unknownMapKeyWithDefaultValue;
|
||||
|
||||
@Value("#{${valuesMap}.?[value>'1']}")
|
||||
private Map<String, Integer> valuesMapFiltered;
|
||||
|
||||
@Value("#{systemProperties}")
|
||||
private Map<String, String> systemPropertiesMap;
|
||||
|
||||
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(Arrays.toString(valuesArray));
|
||||
System.out.println(spelValue);
|
||||
System.out.println(spelSomeDefault);
|
||||
System.out.println(someBeanValue);
|
||||
System.out.println(valuesList);
|
||||
System.out.println(valuesMap);
|
||||
System.out.println(valuesMapKey1);
|
||||
System.out.println(unknownMapKey);
|
||||
System.out.println(unknownMap);
|
||||
System.out.println(unknownMapKeyWithDefaultValue);
|
||||
System.out.println(valuesMapFiltered);
|
||||
System.out.println(systemPropertiesMap);
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package com.baeldung.valuewithdefaults;
|
||||
|
||||
import java.util.Arrays;
|
||||
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.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.primitives.Ints;
|
||||
|
||||
/**
|
||||
* Demonstrates setting defaults for @Value annotation. Note that there are no properties
|
||||
* defined in the specified property source. We also assume that the user here
|
||||
* does not have a system property named some.key.
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
@PropertySource(name = "myProperties", value = "valueswithdefaults.properties")
|
||||
public class ValuesWithDefaultsApp {
|
||||
|
||||
@Value("${some.key:my default value}")
|
||||
private String stringWithDefaultValue;
|
||||
|
||||
@Value("${some.key:}")
|
||||
private String stringWithBlankDefaultValue;
|
||||
|
||||
@Value("${some.key:true}")
|
||||
private boolean booleanWithDefaultValue;
|
||||
|
||||
@Value("${some.key:42}")
|
||||
private int intWithDefaultValue;
|
||||
|
||||
@Value("${some.key:one,two,three}")
|
||||
private String[] stringArrayWithDefaults;
|
||||
|
||||
@Value("${some.key:1,2,3}")
|
||||
private int[] intArrayWithDefaults;
|
||||
|
||||
@Value("#{systemProperties['some.key'] ?: 'my default system property value'}")
|
||||
private String spelWithDefaultValue;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValuesWithDefaultsApp.class);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void afterInitialize() {
|
||||
// strings
|
||||
Assert.isTrue(stringWithDefaultValue.equals("my default value"));
|
||||
Assert.isTrue(stringWithBlankDefaultValue.equals(""));
|
||||
|
||||
// other primitives
|
||||
Assert.isTrue(booleanWithDefaultValue);
|
||||
Assert.isTrue(intWithDefaultValue == 42);
|
||||
|
||||
// arrays
|
||||
List<String> stringListValues = Lists.newArrayList("one", "two", "three");
|
||||
Assert.isTrue(Arrays.asList(stringArrayWithDefaults).containsAll(stringListValues));
|
||||
|
||||
List<Integer> intListValues = Lists.newArrayList(1, 2, 3);
|
||||
Assert.isTrue(Ints.asList(intArrayWithDefaults).containsAll(intListValues));
|
||||
|
||||
// SpEL
|
||||
Assert.isTrue(spelWithDefaultValue.equals("my default system property value"));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.baeldung.yaml;
|
||||
|
||||
import java.util.Collections;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MyApplication implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
private YAMLConfig myConfig;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication app = new SpringApplication(MyApplication.class);
|
||||
app.run();
|
||||
}
|
||||
|
||||
public void run(String... args) throws Exception {
|
||||
System.out.println("using environment:" + myConfig.getEnvironment());
|
||||
System.out.println("name:" + myConfig.getName());
|
||||
System.out.println("servers:" + myConfig.getServers());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.baeldung.yaml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
@ConfigurationProperties
|
||||
public class YAMLConfig {
|
||||
private String name;
|
||||
private String environment;
|
||||
private List<String> servers = new ArrayList<String>();
|
||||
|
||||
public List<String> getServers() {
|
||||
return servers;
|
||||
}
|
||||
|
||||
public void setServers(List<String> servers) {
|
||||
this.servers = servers;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEnvironment() {
|
||||
return environment;
|
||||
}
|
||||
|
||||
public void setEnvironment(String environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
spring:
|
||||
profiles: test
|
||||
name: test-YAML
|
||||
environment: test
|
||||
servers:
|
||||
- www.abc.test.com
|
||||
- www.xyz.test.com
|
||||
|
||||
---
|
||||
|
||||
spring:
|
||||
profiles: prod
|
||||
name: prod-YAML
|
||||
environment: production
|
||||
servers:
|
||||
- www.abc.com
|
||||
- www.xyz.com
|
||||
@@ -1,4 +0,0 @@
|
||||
value.from.file=Value got from the file
|
||||
priority=Properties file
|
||||
listOfValues=A,B,C
|
||||
valuesMap={key1:'1', key2 : '2', key3 : '3'}
|
||||
-40
@@ -1,40 +0,0 @@
|
||||
package com.baeldung.value;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
public class ClassNotManagedBySpringIntegrationTest {
|
||||
|
||||
@MockBean
|
||||
private InitializerBean initializerBean;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
when(initializerBean.initClass())
|
||||
.thenReturn(new ClassNotManagedBySpring("This is only sample value", "Another configured value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInitializerBean_whenInvokedInitClass_shouldInitialize() throws Exception {
|
||||
|
||||
//given
|
||||
ClassNotManagedBySpring classNotManagedBySpring = initializerBean.initClass();
|
||||
|
||||
//when
|
||||
String initializedValue = classNotManagedBySpring.getCustomVariable();
|
||||
String anotherCustomVariable = classNotManagedBySpring.getAnotherCustomVariable();
|
||||
|
||||
//then
|
||||
assertEquals("This is only sample value", initializedValue);
|
||||
assertEquals("Another configured value", anotherCustomVariable);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user