Interface Description: When a class implements this interface, the class can easily get all the beans in the ApplicationContext. In other words, this class can get directly from the spring configuration file, all the bean objects that are referenced to.
In a Web application, Spring containers are typically generated in a declarative manner: as long as the developer configures a listener in Web. XML, the listener will be responsible for initializing the spring container, and the MVC framework can directly invoke the beans in the spring container without having to access the spring container itself. In this case, the bean in the container is under container management without having to actively access the container, just accept the container's dependency injection.
However, in some special cases, the bean needs to implement a function, but the function must be implemented with the help of the spring container, so that the bean must get the spring container first and then implement it with the help of the spring container. In order for the bean to get the spring container it is in, you can have the bean implement the Applicationcontextaware interface.
The following example is a tool class that implements Applicationcontextaware, which can be referenced by other classes to manipulate the spring container and the bean instances in it.
Public classSpringcontextholderImplementsApplicationcontextaware {Private StaticApplicationContext ApplicationContext =NULL; /*** Gets the ApplicationContext in the static variable. */ Public StaticApplicationContext Getapplicationcontext () {assertcontextinjected (); returnApplicationContext; } /*** Get the bean from the static variable ApplicationContext and automatically transform it to the type of the assigned value object. */@SuppressWarnings ("Unchecked") Public Static<T>T Getbean (String name) {assertcontextinjected (); return(T) Applicationcontext.getbean (name); } /*** Get the bean from the static variable ApplicationContext and automatically transform it to the type of the assigned value object. */ Public Static<T> T Getbean (class<t>requiredtype) {assertcontextinjected (); returnApplicationcontext.getbean (requiredtype); } /*** Clear ApplicationContext in Springcontextholder is null. */ Public Static voidClearholder () {ApplicationContext=NULL; } /*** Implement the Applicationcontextaware interface and inject the context into the static variable. */@Override Public voidSetapplicationcontext (ApplicationContext applicationcontext) {Springcontextholder.applicationcontext=ApplicationContext; } /*** Check that the ApplicationContext is not empty. */ Private Static voidassertcontextinjected () {validate.validstate (ApplicationContext!=NULL, "The Applicaitoncontext property is not injected, define Springcontextholder in Applicationcontext.xml."); } }
The spring container detects all the beans in the container, and if a bean is found to implement the Applicationcontextaware interface, the spring container will, after the bean is created, The Bean's Setapplicationcontextaware () method is called automatically, and when the method is called, the container itself is passed as an argument to the method- The implementation part of the method assigns spring-passed parameters (the container itself) to the ApplicationContext instance variable of the class object, so the container itself can then be accessed through the ApplicationContext instance variable.
The role of the Applicationcontextaware interface