Applicationcontextaware Fast Fetch bean

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.