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.
[Java]View PlainCopy
- Public class Springcontextholder implements Applicationcontextaware {
- private static ApplicationContext ApplicationContext = null;
- /**
- * Gets the ApplicationContext in the static variable.
- */
- public static ApplicationContext Getapplicationcontext () {
- Assertcontextinjected ();
- return applicationcontext;
- }
- /**
- * 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 ();
- return Applicationcontext.getbean (requiredtype);
- }
- /**
- * Clear ApplicationContext in Springcontextholder is null.
- */
- public static void Clearholder () {
- ApplicationContext = null;
- }
- /**
- * Implement the Applicationcontextaware interface and inject the context into the static variable.
- */
- @Override
- public void Setapplicationcontext (ApplicationContext applicationcontext) {
- Springcontextholder.applicationcontext = ApplicationContext;
- }
- /**
- * Check that the ApplicationContext is not empty.
- */
- private static void assertcontextinjected () {
- 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.
For example:
Creating CacheManager instances in Cacheutil with the spring container
[Java]View PlainCopy
- Public class Cacheutils {
- private static CacheManager CacheManager = ((CacheManager) Springcontextholder.getbean ("CacheManager"));
- }
The role of the Applicationcontextaware interface