Go to: how to obtain the bean managed by Spring and the bean managed by spring

Source: Internet
Author: User

Go to: how to obtain the bean managed by Spring and the bean managed by spring

Original article:Http://blog.csdn.net/a9529lty/article/details/42145545

1. When loading in servlet mode,
[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 in ServletContext is org. springframework. web. servlet. FrameworkServlet. CONTEXT. springMVC.
Note that springMVC is the configuration value of your servlet-name and must be modified in a timely manner.

Java code
  1. ServletContext SC = omitted
  2. WebApplicationContext attr = (WebApplicationContext) SC. getAttribute ("org. springframework. web. servlet. FrameworkServlet. CONTEXT. springMVC ");

 

2. listener loading:
[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 obtained in this way

Java code
  1. ServletContext context = getServletContext ();
  2. WebApplicationContext applicationContext = WebApplicationContextUtils. getWebApplicationContext (context );

 

3. A general method is provided. The first two methods are not common and can be discarded.
Add the following to the configuration file:

Xml Code
  1. <! -- Used to hold ApplicationContext. You can use the static method of SpringContextHolder. getBean ('xxxx') to obtain the spring bean object -->
  2. <BeanClass = "com. xxxxx. SpringContextHolder" lazy-init = "false"/>

 

Java code
    1. Import org. springframework. context. ApplicationContext;
    2. Import org. springframework. context. ApplicationContextAware;
    3. /**
    4. * Save Spring ApplicationContext as a static variable. You can retrieve ApplicaitonContext from any code anywhere.
    5. *
    6. */
    7. Public class SpringContextHolder implements ApplicationContextAware {
    8. Private static ApplicationContext applicationContext;
    9. /**
    10. * Implements the context injection function of the ApplicationContextAware interface and saves it to static variables.
    11. */
    12. Public void setApplicationContext (ApplicationContext applicationContext ){
    13. SpringContextHolder. applicationContext = applicationContext; // NOSONAR
    14. }
    15. /**
    16. * Obtain ApplicationContext stored in static variables.
    17. */
    18. Public static ApplicationContext getApplicationContext (){
    19. CheckApplicationContext ();
    20. Return applicationContext;
    21. }
    22. /**
    23. * Obtain the Bean from the static variable ApplicationContext and automatically convert it to the type of the assigned object.
    24. */
    25. @ SuppressWarnings ("unchecked ")
    26. Public static <T> T getBean (String name ){
    27. CheckApplicationContext ();
    28. Return (T) applicationContext. getBean (name );
    29. }
    30. /**
    31. * Obtain the Bean from the static variable ApplicationContext and automatically convert it to the type of the assigned 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 static applicationContext 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 is not injected. Define SpringContextHolder in applicationContext. xml ");
    47. }
    48. }
    49. }

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.