How the Bean in spring gets the IOC container service

Source: Internet
Author: User

Spring Dependency Injection allows all beans to be unconscious of the presence of their IOC containers, and can even be replaced by other containers. But in practice, if a bean object is to use the function resources of the spring container itself, it needs to be aware of the presence of the IOC container to invoke what the resources provided by spring should handle.

First, using @autowired dependency Injection

Only in the same IOC container can the corresponding Bean object be obtained through the @autowired dependency injection, as follows:

@Autowired Private messagesource Messagesource; @Autowired Private resourceloader Resourceloader; @Autowired Private ApplicationContext ApplicationContext;

Second, the realization of the corresponding aware interface

The purpose of the Spring aware interface is to get beans to the various services of the IOC container, such as Beanfactoryaware, Beannameaware, Applicationcontextaware, Resourceloaderaware, Servletcontextaware and so on. The bean that implements these aware interfaces can get the corresponding resources after being instantiated, for example, when the bean that implements Beanfactoryaware is instantiated , the spring container will inject beanfactory instance. The bean that implements Applicationcontextaware will be injected into the ApplicationContext instance and so on after the bean is instantiated .

If you want to use the tool class static method, the way to implement the interface is generally used. As follows:

@Service () @Lazy (false) Public classSpringcontextholderImplementsApplicationcontextaware, Disposablebean {Private StaticApplicationContext ApplicationContext =NULL; /*** Implement the Applicationcontextaware interface and inject the context into the static variable. */@Override Public voidSetapplicationcontext (ApplicationContext applicationcontext) {if(Springcontextholder.applicationcontext! =NULL) {System.out.println ("The ApplicationContext in Springcontextholder is covered, the original ApplicationContext is:" +springcontextholder.applicationcontext); } System.out.println ("Spring container launches, injecting container instances into the Springcontextholder instance bean"); Springcontextholder.applicationcontext=ApplicationContext; }/*** Gets the applicationcontext stored in the static variable. */     Public StaticApplicationContext Getapplicationcontext () {returnApplicationContext; }    /*** Gets the bean from static variable ApplicationContext, automatically transforms to the type of the assigned value object. */@SuppressWarnings ("Unchecked")     Public Static<T>T Getbean (String name) {return(T) Applicationcontext.getbean (name); }    /*** Gets the bean from static variable ApplicationContext, automatically transforms to the type of the assigned value object. */     Public Static<T> T Getbean (class<t>requiredtype) {        returnApplicationcontext.getbean (requiredtype); }}

How the Bean in spring gets the IOC container service

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.