Spring obtains ApplicationContext and applicationcontext.
In Spring + Struts + Hibernate, Spring context is sometimes used. When the project is started, the context is automatically initialized Based on the applicationContext configuration file. You can use the ApplicationContextAware interface to obtain the Spring context. Create the following classes:
package com.school.tool;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component;@Componentpublic class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtil.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { return applicationContext; } @SuppressWarnings("unchecked") public static <T> T getBean(String name) throws BeansException { return (T) applicationContext.getBean(name); }}
Configure this class in the applicationContext configuration file:
<bean id="springContextUtil" class="com.school.tool.SpringContextUtil" />
In this way, when the context is initialized Based on applicationContext, The setApplicationContext () method is automatically called to obtain ApplicationContext.
You can also use
ClassPathXmlApplicationContext("applicationContext.xml")
Obtain ApplicationContext. However, this is equivalent to re-initializing the context. The speed is very slow and the Force Grid is not high. It is not recommended.