Skip to content

Commit 8cfd764

Browse files
committed
spring Inject a Property Value Into a Class Not Managed by Spring
1 parent 4a1dbba commit 8cfd764

File tree

6 files changed

+141
-0
lines changed

6 files changed

+141
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.ripjava.spring.properties.properties;
2+
3+
public class ClassNotManagedBySpring {
4+
5+
private String customVariable;
6+
private String anotherCustomVariable;
7+
8+
public ClassNotManagedBySpring(String someInitialValue, String anotherManagedValue) {
9+
this.customVariable = someInitialValue;
10+
this.anotherCustomVariable = anotherManagedValue;
11+
}
12+
13+
public String getCustomVariable() {
14+
return customVariable;
15+
}
16+
17+
public void setCustomVariable(String customVariable) {
18+
this.customVariable = customVariable;
19+
}
20+
21+
public String getAnotherCustomVariable() {
22+
return anotherCustomVariable;
23+
}
24+
25+
public void setAnotherCustomVariable(String anotherCustomVariable) {
26+
this.anotherCustomVariable = anotherCustomVariable;
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.ripjava.spring.properties.properties;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class InitializerBean {
8+
9+
private String someInitialValue;
10+
private String anotherManagedValue;
11+
12+
public InitializerBean(@Value("someInitialValue") String someInitialValue, @Value("anotherValue") String anotherManagedValue) {
13+
this.someInitialValue = someInitialValue;
14+
this.anotherManagedValue = anotherManagedValue;
15+
}
16+
17+
public ClassNotManagedBySpring initClass() {
18+
return new ClassNotManagedBySpring(this.someInitialValue, this.anotherManagedValue);
19+
}
20+
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.ripjava.spring.properties.properties;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.Properties;
6+
7+
public class PropertiesLoader {
8+
9+
public static Properties loadProperties(String resourceFileName) throws IOException {
10+
Properties configuration = new Properties();
11+
InputStream inputStream = PropertiesLoader.class
12+
.getClassLoader()
13+
.getResourceAsStream(resourceFileName);
14+
configuration.load(inputStream);
15+
inputStream.close();
16+
return configuration;
17+
}
18+
19+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# this is sample property file for PropertiesLoaderTest configuration needs
2+
! this is also a comment
3+
sampleConfEntry = sample String value
4+
colonSeparatedEntry : colon separated entry value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.ripjava.spring.properties.properties;
2+
3+
4+
import com.ripjava.spring.properties.ConfigPropertiesDemoApplication;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.extension.ExtendWith;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.boot.test.mock.mockito.MockBean;
11+
import org.springframework.test.context.junit.jupiter.SpringExtension;
12+
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.mockito.Mockito.when;
15+
16+
@ExtendWith(SpringExtension.class)
17+
public class ClassNotManagedBySpringIntegrationTest {
18+
19+
20+
@MockBean
21+
private InitializerBean initializerBean;
22+
23+
@BeforeEach
24+
public void init() {
25+
when(initializerBean.initClass())
26+
.thenReturn(new ClassNotManagedBySpring("This is only sample value", "Another configured value"));
27+
}
28+
@Test
29+
public void test_ClassNotManagedBySpring(){
30+
ClassNotManagedBySpring classNotManagedBySpring = initializerBean.initClass();
31+
32+
String initializedValue = classNotManagedBySpring.getCustomVariable();
33+
String anotherCustomVariable = classNotManagedBySpring.getAnotherCustomVariable();
34+
35+
assertEquals("This is only sample value", initializedValue);
36+
assertEquals("Another configured value", anotherCustomVariable);
37+
}
38+
39+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.ripjava.spring.properties.properties;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.IOException;
6+
import java.util.Properties;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
public class PropertiesLoaderUnitTest {
11+
12+
@Test
13+
public void test_loadProperties() throws IOException {
14+
final String RESOURCE_FILE_NAME = "configuration.properties";
15+
16+
final String SAMPLE_CONF_ENTRY = "sampleConfEntry";
17+
final String COLON_SEPARATED_CONF_ENTRY = "colonSeparatedEntry";
18+
19+
final String GIVEN_CONF_ENTRY_VALUE = "sample String value";
20+
final String COLON_SEPARATED_CONF_ENTRY_VALUE = "colon separated entry value";
21+
22+
Properties config = PropertiesLoader.loadProperties(RESOURCE_FILE_NAME);
23+
String sampleConfEntryValue = config.getProperty(SAMPLE_CONF_ENTRY);
24+
String colonSeparatedConfEntryValue = config.getProperty(COLON_SEPARATED_CONF_ENTRY);
25+
26+
assertEquals(GIVEN_CONF_ENTRY_VALUE, sampleConfEntryValue);
27+
assertEquals(COLON_SEPARATED_CONF_ENTRY_VALUE, colonSeparatedConfEntryValue);
28+
29+
}
30+
}

0 commit comments

Comments
 (0)