Spring Bean Management

springMVC pay attention to the following springMVC, which is the value of your servlet-name configuration, pay attention to modify it in time. Java code ServletContext sc=omitted 



















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

2、listener方式加载时:【web.xml】Xml代码

<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】可以这样取得

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

3. The general method is here, artifact, the first two methods 1 and 2 are not general and can be discarded.
Add in the configuration file: Xml code

<!-- Used to hold the ApplicationContext, you can use the static method of SpringContextHolder.getBean('xxxx') to get the spring bean object --> 
<bean class="com.xxxxx.SpringContextHolder" lazy -init="false" /> 

Java code
import org.springframework.context.ApplicationContext; 
import org.springframework.context.ApplicationContextAware; 
/**
* Save the Spring ApplicationContext as a static variable, you can take out the ApplicaitonContext anywhere in any code .

*/ 
public class SpringContextHolder implements ApplicationContextAware { 
private static ApplicationContext applicationContext; 
 
/**
* Implement the context injection function of the ApplicationContextAware interface and store it in a static variable.
*/ 
public void setApplicationContext(ApplicationContext applicationContext) { 
SpringContextHolder.applicationContext = applicationContext; // NOSONAR 

 
/**
* Get the ApplicationContext stored in a static variable.
*/ 
public static ApplicationContext getApplicationContext() { 
checkApplicationContext(); 
return applicationContext; 

 
/* *
* Get 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); 

 
/**
* Get 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); 

 
/**
* 清除applicationContext静态变量.
*/ 
public static void cleanApplicationContext() { 
applicationContext = null; 

 
private static void checkApplicationContext() { 
if (applicationContext == null) { 
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder"); 


}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326992000&siteId=291194637
Recommended