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 context files: " + SpringContextLocator.SPRING_CONFIG_CONTEXT);
29 factoryFiles = new String[] { SPRING_CONFIG_CONTEXT };
31 SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles);
33 System.out.println("The context has been loaded successfully!! ");
37 * Singleton pattern not thread safety. To use SingletoHolder pattern as the best approximation
38 * otherwise to use an Enum class (see Effective Java Second Edition and ) if we need serialization.
40 public static SpringContextLocator getInstance() {
41 if (SpringContextLocator.instance == null) {
42 SpringContextLocator.instance = new SpringContextLocator();
44 return SpringContextLocator.instance;
48 * Return bean from application context.
50 public Object getBean(final String name) {
51 return SpringContextLocator.context.getBean(name);