/**
* Load Spring configuration file, provide Getbean interface.
* @author Xiongzy
*
*/
Public final class Beanlocator {
private static final Logger Logger = Logger.getlogger (Beanlocator.class);
/**
* Single instance.
*/
private static Beanlocator instance = NULL;
/**
* Default configuration file name.
*/
private static final String Default_configuration_filename = "Spring/applicationcontext.xml";
/**
* Load the profile name.
*/
private static String configurationfilename = null;
/**
* Spring environment.
*/
Private ApplicationContext applicationcontext = null;
/**
* Singleton mode.
* @return Interface
*/
public static Beanlocator getinstance () {
if (instance = = null) {
Synchronization control code to prevent initialization multiple times.
Synchronized (logger) {
if (instance = = null) {
Instance = new Beanlocator ();
}
}
}
return instance;
}
/**
* Set the profile name.
* @param filename configuration file name
*/
public static void Setconfigurationfilename (String fileName) {
Configurationfilename = FileName;
}
/**
* Private construction.
*/
Private Beanlocator () {
if (Configurationfilename = = NULL | | configurationfilename.length () = = 0) {
Configurationfilename = Default_configuration_filename;
}
Get the Spring Framework bean environment
try{
ApplicationContext = new Classpathxmlapplicationcontext (configurationfilename);
}catch (Exception e) {
Logger.error ("Exception occurred while initializing spring configuration file:" + e.getmessage (), E);
throw new RuntimeException ("Exception occurred while initializing spring configuration file:" + e.getmessage (), E);
}
}
/**
* Spring Getbean interface.
* @param beanname Interface Name
* @return Interface
*/
Public Object Getbean (String beanname) {
Return Applicationcontext.getbean (Beanname);
}
public static void Main (string[] args) {
Cityinfoservice Cityinfoservice = (cityinfoservice) beanlocator.getinstance (). Getbean ("CityInfoService");
System.out.println (Cityinfoservice);
}
}