Go: How to get spring-managed beans

Source: Internet
Author: User

Original link:http://blog.csdn.net/a9529lty/article/details/42145545

1. When the servlet mode is loaded,
"Web. Xml"

XML code
  1. <servlet>
  2. <servlet-name>springmvc</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</ Servlet-class>
  4. <init-param>
  5. <param-name>contextconfiglocation</param-name>
  6. <param-value>classpath*:/springmvc.xml</param-value>
  7. </init-param>
  8. <load-on-startup>1</load-on-startup>
  9. </servlet>

The key of the spring container placed in the ServletContext is Org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC
Note the following SPRINGMVC, which is the value of your servlet-name configuration, be aware of changes in time.

Java code
    1. ServletContext sc=
    2. Webapplicationcontext attr = (webapplicationcontext) sc.getattribute ("  Org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC ");

2, Listener mode load:
"Web. Xml"

XML code
  1. <context-param>
  2. <param-name>contextconfiglocation</param-name>
  3. <param-value>/web-inf/applicationcontext</param-value >
  4. </context-param>
  5. <listener>
  6. <listener-class>org.springframework.web.context.ContextLoaderListener< /Listener-class>
  7. </Listener>

"Jsp/servlet" can be achieved in this way

Java code
    1. ServletContext context = Getservletcontext ();
    2. Webapplicationcontext ApplicationContext = webapplicationcontextutils. Getwebapplicationcontext (context);

3, the general method to, artifact Ah, the former 1, 22 methods are not universal, can be discarded.
In the configuration file, add:

XML code
    1. <!--for holding ApplicationContext, you can use the static method of Springcontextholder.getbean (' xxxx ') to get the Spring bean object--
    2. <beans class="Com.xxxxx.SpringContextHolder" lazy-init="false" />

Java code
    1. Import Org.springframework.context.ApplicationContext;
    2. Import Org.springframework.context.ApplicationContextAware;
    3. /**
    4. * Save the spring applicationcontext with a static variable to remove the Applicaitoncontext from any code anywhere.
    5. *
    6. */
    7. Public class Springcontextholder implements Applicationcontextaware {
    8. Private static ApplicationContext ApplicationContext;
    9. /**
    10. * Implement the context injection function of the Applicationcontextaware interface and store it in a static variable.
    11. */
    12. Public void Setapplicationcontext (ApplicationContext applicationcontext) {
    13. Springcontextholder.applicationcontext = ApplicationContext; //Nosonar
    14. }
    15. /**
    16. * Gets the applicationcontext stored in the static variable.
    17. */
    18. Public static ApplicationContext Getapplicationcontext () {
    19. Checkapplicationcontext ();
    20. return applicationcontext;
    21. }
    22. /**
    23. * Gets the bean from static variable ApplicationContext, automatically transforms to the type of the assigned value object.
    24. */
    25. @SuppressWarnings ("unchecked")
    26. Public static <T> T Getbean (String name) {
    27. Checkapplicationcontext ();
    28. Return (T) Applicationcontext.getbean (name);
    29. }
    30. /**
    31. * Gets the bean from static variable ApplicationContext, automatically transforms to the type of the assigned value object.
    32. */
    33. @SuppressWarnings ("unchecked")
    34. Public static <T> T Getbean (class<t> clazz) {
    35. Checkapplicationcontext ();
    36. Return (T) Applicationcontext.getbeansoftype (clazz);
    37. }
    38. /**
    39. * Clear ApplicationContext static variables.
    40. */
    41. Public static void Cleanapplicationcontext () {
    42. ApplicationContext = null;
    43. }
    44. Private static void Checkapplicationcontext () {
    45. if (ApplicationContext = = null) {
    46. Throw New IllegalStateException ("Applicaitoncontext not injected, please define Springcontextholder in Applicationcontext.xml"  );
    47. }
    48. }
    49. }

Go: How to get spring-managed beans

Related Article

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.