ApplicationContext's beanfactory subclass, with more powerful features, ApplicationContext can automatically instantiate all the beans when the server is started, and Beanfactory only when calling Getbean () to instantiate that Bean, which is why we have to get a ApplicationContext object, In fact, the SPRING2 related web app uses the ApplicationContext object to instantiate the bean by default, in other words, when the server starts, the spring container has instantiated a ApplicationContext object, So we're going to try to get this object in the old code. But how do you get a ApplicationContext object? Many methods, the most common way is to use Classpathxmlapplicationcontext, Filesystemclasspathxmlapplicationcontext, Filesystemxmlapplicationcontext and other objects to load the spring configuration file, this is also possible, but when the spring configuration file is loaded, It generates a new Applicaitoncontext object instead of which one the spring container will generate for us, which creates redundancy, so we don't take this load file here, We use Applicationcontextaware to let the spring container pass its generated applicationcontext to us, and then we set this applicationcontext as a static variable of a class, This way we can always get the application object in the old code. (This section describes the transfer from: http://blog.csdn.net/kouwoo/article/details/43405109)
@Componentpublic class Springutils implements Applicationcontextaware {private static ApplicationContext ApplicationContext; @Overridepublic void Setapplicationcontext (ApplicationContext arg0) throws Beansexception { ApplicationContext = arg0;} public static object GetObject (String ID) {Object object = null; Object = Applicationcontext.getbean (ID); return object;}}
Applicationcontextaware Quick Fetch bean