本文向您展示了三种不同的方式来获取代码中的Spring Framework Application Context。
摘要
(这是我在2010年撰写的旧文章的转贴)。 在Google中搜索“ Spring ApplicationContextAware ”时,您会遇到很多建议,而且我也看到很多人继续抱怨说他们的setApplicationContext方法没有被调用。 因此,为澄清起见,我在博客中写了一些说明,希望它有助于澄清上下文的工作方式。
方法1
在您的类中,可以实现ApplicationContextAware类,如下所示:
public class MyClass implements ApplicationContextAware {
static final long serialVersionUID = 02L;
ApplicationContext applicationContext = null;
public void doSomething(){
if (applicationContext != null && applicationContext.containsBean("accessKeys")){
MyBean beanA = (MyBean) applicationContext.getBean("mybean");
//Do something with this AccessBean
}
return null;
}
@Override
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
System.out.println("setting context");
this.applicationContext = applicationContext;
}
}
方法2
如果您使用的是Java Servlet,则可以执行以下操作:
public class gzservlet extends HttpServlet {
static final long serialVersionUID = 02L;
ApplicationContext applicationContext = null;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (applicationContext == null){
System.out.println("setting context in get");
applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
}
if (applicationContext != null && applicationContext.containsBean("accessKeys")){
AccessBean thisAccessBean = (AccessBean) applicationContext.getBean("accessKeys");
req.setAttribute("keys", thisAccessBean.toString());
System.out.println("setting keys");
}
req.getRequestDispatcher("/index2.jsp").include(req,resp);
}
}
因此,人们会问的问题是什么时候使用什么? 答案是。 取决于您如何调用Spring。
方法1的工作原理:调用Spring时,您正在使用DispatcherServlet将此链接。 然后,方法1将解析ApplicationContextAware的实现,并调用setApplicationContext()方法来设置上下文。
在web.xml中:
<servlet>
<servlet-name>dispatchservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatchservlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
如果您没有使用DispatcherServlet,并且正在使用侦听器初始化Spring,并且您有自己的Servlet正在驱动Request \ Response范围,则请使用方法#2。 下面是这种情况下web.xml外观的示例。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>MyOwnServlet</servlet-name>
<servlet-class>com.something.myservlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyOwnServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
我希望这可以阐明为什么有时即使实现了ApplicationContextAware接口,也不会调用您的setter。
[09/12/2010]这是获取上下文的第三种方法:
使用静态方法创建以下类以获取上下文:
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContextProvider implements ApplicationContextAware{
private static ApplicationContext ctx = null;
public static ApplicationContext getApplicationContext() {
return ctx;
}
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
this.ctx = ctx;
}
}
并在您的spring bean配置xml文件中添加以下内容:
<bean id="applicationContextProvider" class="ApplicationContextProvider"></bean>
现在,在您的课程中,您可以执行以下操作:
ApplicationContext ctx = ApplicationContextProvider.getApplicationContext();
而已!!!
干杯。
如果您觉得这篇文章有用,请考虑注册我的电子邮件或将其重新发布到您喜欢的社交网站上。 请参阅右侧导航栏上的链接。
现在为今天的灵感
为了创新,我们不能期待别人做了什么。 开辟一条道路的整个想法是,以前没有道路。 今天要创新!