-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSpringContextLoader.java
More file actions
97 lines (77 loc) · 4.22 KB
/
SpringContextLoader.java
File metadata and controls
97 lines (77 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package net.dempsy.util.spring;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.PropertySource;
public class SpringContextLoader {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringContextLoader.class);
public static ClassPathXmlApplicationContext load(final String[] propertyCtx, final String[] appCtx, final Object... dependencies) throws IOException {
return load(propertyCtx, appCtx, false, dependencies);
}
public static ClassPathXmlApplicationContext load(final PropertiesReader reader, final String[] appCtx, final Object... dependencies) throws IOException {
return load(reader, appCtx, false, dependencies);
}
public static ClassPathXmlApplicationContext load(final PropertiesReader reader, final String[] appCtx, final boolean skipRefresh,
final Object... dependencies) throws IOException {
final String[] toUse = appCtx == null ? new String[] {"spring/default-property-placeholder.xml"} : appCtx;
// load the main application context without refreshing it.
final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(toUse, false);
if(dependencies != null)
Arrays.stream(dependencies)
.map(o -> new CustomBeanFactoryPostProcessor(o))
.forEach(pp -> ctx.addBeanFactoryPostProcessor(pp));
// Add a Spring PropertySource to the main application context's environment
ctx.getEnvironment().getPropertySources().addFirst(
new PropertySource<Properties>(reader.getClass().getName()) {
Properties props = reader.read();
@Override
public Object getProperty(final String name) {
final String ret = props.getProperty(name);
if(LOGGER.isDebugEnabled())
LOGGER.debug("Property requested: {} has value of {}", name, ret);
return ret;
}
});
// refresh the context
if(skipRefresh)
return ctx;
ctx.refresh();
// double check to make sure that there's a PropertySourcesPlaceholderConfigurer
final Map<String, PropertySourcesPlaceholderConfigurer> pspcmap = ctx.getBeansOfType(PropertySourcesPlaceholderConfigurer.class);
if(pspcmap == null || pspcmap.size() == 0) {
throw new IllegalArgumentException("There was an attempt to use the property reader ["
+ reader.getClass().getName()
+ "] with the context defined by "
+ Arrays.toString(appCtx)
+ " but there is no PropertySourcesPlaceholderConfigurer in the context");
}
return ctx;
}
public static ClassPathXmlApplicationContext load(final String[] propertyCtx, final String[] appCtx, final boolean skipRefresh,
final Object... dependencies) throws IOException {
// Load the properties-source context.
try(final ClassPathXmlApplicationContext propsCtx = new ClassPathXmlApplicationContext(propertyCtx);) {
// Retrieve the reader.
final PropertiesReader reader = propsCtx.getBean(PropertiesReader.class);
return load(reader, appCtx, skipRefresh, dependencies);
}
}
private static class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
private final Object dependency;
public CustomBeanFactoryPostProcessor(final Object dependency) {
this.dependency = dependency;
}
@Override
public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
beanFactory.registerResolvableDependency(dependency.getClass(), dependency);
}
}
}