How to get Spring-managed bean

This article is mainly about 3 implementation, please use the first three methods (Universal)

1, servlet configuration loading mode as follows

<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>

spring containers are placed in the ServletContext key org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC

Note that the latter springMVC, is the value of your servlet-name configuration, pay attention to timely change.

ServletContext sc=略 
WebApplicationContext attr = (WebApplicationContext)sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC");

2, listener way to load

web.xml as follows:

<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 get so

ServletContext context = getServletContext(); 
WebApplicationContext applicationContext = WebApplicationContextUtils .getWebApplicationContext(context);

3, a general method, the first two methods 1 and 2 are not common, can be abandoned.

Join in the configuration file:

<! - for holding ApplicationContext, may be used SpringContextHolder.getBean ( 'xxxx') obtained Static method spring bean objects -> 
<the bean class = "com.xxxxx.SpringContextHolder" the lazy-the init = "to false" />

Acquisition are as follows:

org.springframework.context.ApplicationContext Import; 
Import org.springframework.context.ApplicationContextAware;
/ **
* in a static variable that holds Spring ApplicationContext, ApplicaitonContext may be removed at any time in any place at any codes.
*
* /
public class SpringContextHolder the implements the ApplicationContextAware {
static applicationContext the applicationContext Private;

/ **
* ApplicationContextAware implement context injection interface function, which is stored in a static variable.
* /
public void setApplicationContext (the applicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationContext; // NOSONAR
}

/ **
* acquisition memory ApplicationContext in a static variable.
* /
ApplicationContext getApplicationContext static public () {
checkApplicationContext ();
return applicationContext;
}

/ **
* Bean acquired from the ApplicationContext static variable, automatically transition to the type of the assigned object.
* /
@SuppressWarnings ( "an unchecked")
public static <T> the getBean T (String name) {
checkApplicationContext ();
return (T) to applicationContext.getBean (name);
}

/ **
* acquired from the static variable ApplicationContext Bean, an automatic transition to the type of the assigned object.
* /
@SuppressWarnings ( " an unchecked ")
public static <T> T the getBean (Class <T> clazz) {
checkApplicationContext ();
return (T) applicationContext.getBeansOfType (clazz);
}

/ **
* Clear applicationContext static variable.
*/
public static void cleanApplicationContext() {
applicationContext = null;
}

private static void checkApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
}
}
}
How to get Spring-managed bean

Guess you like

Origin www.cnblogs.com/mengtaoadmin/p/11184002.html