Spring框架–应用程序上下文–到达应用程序上下文的三种方法

本文介绍了在Spring框架中获取应用程序上下文的三种方法:通过实现ApplicationContextAware接口、在Servlet中使用、以及通过静态方法。内容详细解释了各种方法的适用场景和工作原理。
摘要由CSDN通过智能技术生成

本文向您展示了三种不同的方式来获取代码中的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();

而已!!!

干杯。

如果您觉得这篇文章有用,请考虑注册我的电子邮件或将其重新发布到您喜欢的社交网站上。 请参阅右侧导航栏上的链接。

现在为今天的灵感

为了创新,我们不能期待别人做了什么。 开辟一条道路的整个想法是,以前没有道路。 今天要创新!

翻译自: https://www.javacodegeeks.com/2017/11/spring-framework-application-context-three-ways-get-application-context.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值