Foreword
spring context is an implementation of the abstract spring containers. You will need to spring to help you manage objects placed in a container objects, ApplicationContext Bean definition and maintenance is a collaboration between objects off the first high-level interface.
Gets spring of context ApplicationContext way
A), obtained by WebApplicationUtils tools.
WebApplicationUtils class is the Spring framework based packet spring-web-3.2.0. RELEASE.jar classes. Using this method must rely Servlet container. Use as follows:
// the Spring acquired ServletContext object, this general category can be acquired
ServletContext SC = ContextLoader.getCurrentWebApplicationContext () GetServletContext ();.
// the servlet may be obtained in this manner, the method more
ServletContext SC = request.getServletContext ():
SC the ServletContext = servletConfig.getServletContext (); // ServletConfig can be acquired in the servlet init (ServletConfig config) obtained in the method
/ * Need to pass a parameter ServletContext object method for obtaining the above * /
// This method throws an exception acquisition failure
ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext (SC);
// returns null acquisition failure this method
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext (sc);
B) by loading configuration files or annotations configuration class acquired
① ClassPathXmlApplicationContext: loaded from the one or more context definition xml configuration file in the class path, applicable to the embodiment xml configuration; such conventional test
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
② FileSystemXmlApplicationContext: loaded from the one or more context definition xml configuration file in the file system, that is loaded in the xml configuration file system drive;
③ XmlWebApplicationContext: a load context definitions from the web application or more xml configuration file for xml configuration.
④AnnotationConfigApplicationContext: load context defined java class based on the configuration of one or more suitable manner java annotations;
⑤ AnnotationConfigWebApplicationContext: specifically for web application prepared for annotation mode;
Three), create your own tools (SpringContextHolder) to achieve the Spring ApplicationContextAware interface.
① registration tools in the configuration file
<bean id="springContextHolder" class="com.fubo.utils.spring.SpringContextHolder" lazy-init="false"/>
② Tools
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* To achieve spring context management
*/
@Component("applicationContextHelper")
public class ApplicationContextHelper implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
applicationContext = context;
}
// Get the type of bean
public static <T> T popBean (Class <T> clazz) {
checkApplicationContext ();
return applicationContext.getBean(clazz);
}
// Get the name of The bean
public static <T> T popBean (String name) {
checkApplicationContext ();
return applicationContext.getBean(clazz);
}
The bean // Get the name and type of
public static <T> T popBean (String name, Class <T> clazz) {
checkApplicationContext ();
return applicationContext.getBean(name, clazz);
}
// Check whether the empty applicationContext
Private static void checkApplicationContext () {
IF (applicationContext == null ) {
the throw new new IllegalStateException ( "applicaitonContext not injected, define in the applicationContext.xml SpringContextHolder" );
}
}
}