The role of the Applicationcontextaware interface

Source: Internet
Author: User

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
  1. Public class Springcontextholder implements Applicationcontextaware {
  2. private static ApplicationContext ApplicationContext = null;
  3. /** 
  4. * Gets the ApplicationContext in the static variable.
  5. */
  6. public static ApplicationContext Getapplicationcontext () {
  7. Assertcontextinjected ();
  8. return applicationcontext;
  9. }
  10. /** 
  11. * Get the bean from the static variable ApplicationContext and automatically transform it to the type of the assigned value object.
  12. */
  13. @SuppressWarnings ("unchecked")
  14. public static <T> T Getbean (String name) {
  15. Assertcontextinjected ();
  16. return (T) Applicationcontext.getbean (name);
  17. }
  18. /** 
  19. * Get the bean from the static variable ApplicationContext and automatically transform it to the type of the assigned value object.
  20. */
  21. public static <T> T Getbean (class<t> requiredtype) {
  22. Assertcontextinjected ();
  23. return Applicationcontext.getbean (requiredtype);
  24. }
  25. /** 
  26. * Clear ApplicationContext in Springcontextholder is null.
  27. */
  28. public static void Clearholder () {
  29. ApplicationContext = null;
  30. }
  31. /** 
  32. * Implement the Applicationcontextaware interface and inject the context into the static variable.
  33. */
  34. @Override
  35. public void Setapplicationcontext (ApplicationContext applicationcontext) {
  36. Springcontextholder.applicationcontext = ApplicationContext;
  37. }
  38. /** 
  39. * Check that the ApplicationContext is not empty.
  40. */
  41. private static void assertcontextinjected () {
  42. Validate.validstate (ApplicationContext! = NULL,
  43. "The Applicaitoncontext property is not injected, define Springcontextholder in Applicationcontext.xml.");
  44. }
  45. }

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
    1. Public class Cacheutils {
    2. private static CacheManager CacheManager = ((CacheManager) Springcontextholder.getbean ("CacheManager"));
    3. }

The role of the Applicationcontextaware interface

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.