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
- <Servlet>
- <Servlet-name>SpringMVC</Servlet-name>
- <Servlet-class>Org. springframework. web. servlet. DispatcherServlet</Servlet-class>
- <Init-param>
- <Param-name>ContExtConfigLocation</Param-name>
- <Param-value>Classpath *:/springMVC. xml</Param-value>
- </Init-param>
- <Load-on-startup>1</Load-on-startup>
- </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
- ServletContext SC = omitted
- WebApplicationContext attr = (WebApplicationContext) SC. getAttribute ("org. springframework. web. servlet. FrameworkServlet. CONTEXT. springMVC ");
2. listener loading:
[Web. xml]
Xml Code
- <Context-param>
- <Param-name>ContextConfigLocation</Param-name>
- <Param-value>/WEB-INF/applicationContext</Param-value>
- </Context-param>
- <Listener>
- <Listener-class>Org. springframework. web. context. ContextLoaderListener</Listener-class>
- </Listener>
[Jsp/servlet] can be obtained in this way
Java code
- ServletContext context = getServletContext ();
- 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
- <! -- Used to hold ApplicationContext. You can use the static method of SpringContextHolder. getBean ('xxxx') to obtain the spring bean object -->
- <BeanClass = "com. xxxxx. SpringContextHolder" lazy-init = "false"/>
Java code
- Import org. springframework. context. ApplicationContext;
- Import org. springframework. context. ApplicationContextAware;
- /**
- * Save Spring ApplicationContext as a static variable. You can retrieve ApplicaitonContext from any code anywhere.
- *
- */
- Public class SpringContextHolder implements ApplicationContextAware {
- Private static ApplicationContext applicationContext;
- /**
- * Implements the context injection function of the ApplicationContextAware interface and saves it to static variables.
- */
- Public void setApplicationContext (ApplicationContext applicationContext ){
- SpringContextHolder. applicationContext = applicationContext; // NOSONAR
- }
- /**
- * Obtain ApplicationContext stored in static variables.
- */
- Public static ApplicationContext getApplicationContext (){
- CheckApplicationContext ();
- Return applicationContext;
- }
- /**
- * Obtain the Bean from the static variable ApplicationContext and automatically convert it to the type of the assigned object.
- */
- @ SuppressWarnings ("unchecked ")
- Public static <T> T getBean (String name ){
- CheckApplicationContext ();
- Return (T) applicationContext. getBean (name );
- }
- /**
- * Obtain the Bean from the static variable ApplicationContext and automatically convert it to the type of the assigned object.
- */
- @ SuppressWarnings ("unchecked ")
- Public static <T> T getBean (Class <T> clazz ){
- CheckApplicationContext ();
- Return (T) applicationContext. getBeansOfType (clazz );
- }
- /**
- * Clear static applicationContext variables.
- */
- Public static void cleanApplicationContext (){
- ApplicationContext = null;
- }
- Private static void checkApplicationContext (){
- If (applicationContext = null ){
- Throw new IllegalStateException ("applicaitonContext is not injected. Define SpringContextHolder in applicationContext. xml ");
- }
- }
- }