1 package de.spring.example;
3 import org.springframework.context.ApplicationContext;
4 import org.springframework.context.support.ClassPathXmlApplicationContext;
10 public final class SpringContextLocator {
13 private static SpringContextLocator instance;
15 // Spring ApplicationContext
16 private static ApplicationContext context;
19 private static final String SPRING_CONFIG_CONTEXT="spring-config.xml";
23 * Private constructor. Singleton pattern.
25 private SpringContextLocator() {
26 String[] factoryFiles = null;
27 System.out.println("Loading files context " +
28 SpringContextLocator.SPRING_CONFIG_CONTEXT);
30 factoryFiles = new String[] { SPRING_CONFIG_CONTEXT };
32 SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles);
34 System.out.println("The context has been loaded successfully!! ");
38 * Singleton pattern not thread safety. To use SingletoHolder pattern as the best approximation
39 * otherwise to use an Enum class (see Effective Java Second Edition and ) if we need serialization.
41 public synchronized static SpringContextLocator getInstance() {
42 if (SpringContextLocator.instance == null) {
43 SpringContextLocator.instance = new SpringContextLocator();
45 return SpringContextLocator.instance;
49 * Return bean from application context.
51 public Object getBean(final String name) {
52 return SpringContextLocator.context.getBean(name);