Java 应用上下文。这是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19615972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:47:57  来源:igfitidea点击:

application context. What is this?

javaspringspring-mvcconfigurationconfig

提问by homeAccount

My colleagues very often use word "application context". In many articles this collocation used very often too.

我的同事经常使用“应用程序上下文”这个词。在许多文章中,这种搭配也经常使用。

My current understanding: application context is single xml file.

我目前的理解:应用程序上下文是单个 xml 文件。

But I understand that if I was right, people wouldn't use "application context" instead of configuration xml file.

但我明白,如果我是对的,人们不会使用“应用程序上下文”而不是配置 xml 文件。

Can you help me to deal with this issue?

你能帮我解决这个问题吗?

采纳答案by Luiggi Mendoza

@feak gives a straight answer about the meaning of ApplicationContextin terms of Spring. In short, it is an object that loads the configuration (usually a XML fileannotation based) and then Spring will start managing the beans and its benefits:

@feak 直接回答ApplicationContext了 Spring的含义。简而言之,它是一个加载配置的对象(通常基于XML 文件的注解),然后 Spring 将开始管理 bean 及其好处:

  • Beans declared in package
  • Beans declared by annotations
  • Constructor and method autowiring
  • Bean injection
  • Configuration, .properties and .yaml file loading
  • etc
  • 在包中声明的 Bean
  • 由注解声明的 Bean
  • 构造函数和方法自动装配
  • Bean注入
  • 配置、.properties 和 .yaml 文件加载
  • 等等

To start an application context, you may use one of the following:

要启动应用程序上下文,您可以使用以下方法之一:

  • Manually load the application context at the beginning of your application. This is done for sample purposes or in standalone applications:

    public class Foo {
        public static void main(String[] args) {
            ApplicationContext context =
                new ClassPathXmlApplicationContext("path/to/applicationContext.xml");
            //use the context as you wish...
        }
    }
    
  • In case of Java web applications using Spring MVC, the DispatchServletwill load the application context for you, so you only have to create a springapp-servlet.xmlfile in WEB-INF folder of the application.

  • 在应用程序开始时手动加载应用程序上下文。这是出于示例目的或在独立应用程序中完成的:

    public class Foo {
        public static void main(String[] args) {
            ApplicationContext context =
                new ClassPathXmlApplicationContext("path/to/applicationContext.xml");
            //use the context as you wish...
        }
    }
    
  • 对于使用 Spring MVC 的 Java Web 应用程序,DispatchServlet它将为您加载应用程序上下文,因此您只需在应用程序的 WEB-INF 文件夹中创建一个springapp-servlet.xml文件。

Note that an application context is associated to a single configuration(XML based or not). Period.

请注意,应用程序上下文与单个配置(基于或不基于 XML)相关联。时期。



After understanding this, you could also understand that you can have more than a single application context per application. This is, having two or more ApplicationContexts in the same application. From the last example in the console application, this is easy to check:

理解这一点后,您还可以理解每个应用程序可以有多个应用程序上下文。ApplicationContext也就是说,在同一个应用程序中有两个或更多的s。从控制台应用程序的最后一个示例中,这很容易检查:

public class Foo {
    public static void main(String[] args) {
        ApplicationContext context =
            new ClassPathXmlApplicationContext("path/to/applicationContext.xml");
        ApplicationContext context2 =
            new ClassPathXmlApplicationContext("path/to/applicationContext.xml");
        //use the context as you wish...
    }
}

Note that we have two application contexts using the same XML configuration. Can you do this? Yes, you're actually seeing it here. What's the difference, then? The maindifference is that Spring beans singleton scopes are singleton per application context, this mean when retrieving a Barbean that's configured in applicationContext.xml file from contextwill not be the sameas retrieving it from context2, but several retrieves from contextwill return the same Barbean instance.

请注意,我们有两个使用相同 XML 配置的应用程序上下文。你能做这个吗?是的,您实际上在这里看到了它。那有什么区别呢?的主要不同之处在于春豆单范围是singleton每个应用程序的情况下,这意味着检索时Bar是在applicationContext.xml文件配置为从豆context也不会是相同的,从检索它context2,但几个检索context将返回相同的Barbean实例。

Is this considered a good or bad practice? Neither, it will depend on the problem to be solved (in case of last example, I would say it is a badpractice). Most people would recommend having all your beans configured in a single place (via XML or another) and loaded by a single application context.

这被认为是好还是坏的做法?两者都不是,这取决于要解决的问题(在最后一个示例的情况下,我会说这是一种不好的做法)。大多数人会建议将所有 bean 配置在一个位置(通过 XML 或其他)并由单个应用程序上下文加载。

回答by kemenov

I guess that you colleagues meant the loaded spring application context, which allows access to:

我猜你们同事的意思是加载的 spring 应用程序上下文,它允许访问:

  • configuration of application,
  • initialized beans,
  • application events api,
  • etc
  • 应用程序配置,
  • 初始化bean,
  • 应用程序事件 API,
  • 等等

回答by Siyaram Malav

Let's understand this in simple words.

让我们用简单的话来理解这一点。

The ApplicationContextis the central interface within a Spring application that is used for providing configuration information to the application.Its created when at the start of application running.

所述的ApplicationContext是Spring应用程序内的中心接口,它用于提供配置信息,以在应用程序运行的开始时创建的application.Its。

It provides the lot of configuration needed by our application-

它提供了我们应用程序所需的大量配置——

  1. Bean Factory - Responsible for creation of java objects called beans.One example is components in the application.
  2. Application listeners - all listeners needed for events.
  3. WebServer information.
  4. Application current environment specific information.
  5. Resource pattern resolver - resource loader with path matcher.
  6. Life cycle Processor.
  7. Class Loader.
  8. Start and shutdown monitor.
  9. Servlet Context.
  10. Reader and Scanner.
  11. Logger
  1. Bean Factory - 负责创建称为 beans 的 java 对象。一个例子是应用程序中的组件。
  2. 应用程序侦听器 - 事件所需的所有侦听器。
  3. 网络服务器信息。
  4. 应用当前环境特定信息。
  5. 资源模式解析器 - 带有路径匹配器的资源加载器。
  6. 生命周期处理器。
  7. 类加载器。
  8. 启动和关闭监视器。
  9. Servlet 上下文。
  10. 阅读器和扫描仪。
  11. 记录器

etc.

等等。

package com.srmhitter9062.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextUtils implements ApplicationContextAware {

    private static ApplicationContext ctx;

    @Override
    public void setApplicationContext(ApplicationContext appContext)
            throws BeansException {
        ctx = appContext;

    }

    public static ApplicationContext getApplicationContext() {
        return ctx;
    }
}

We can get some idea about the Application object in below snapshot.

我们可以在下面的快照中对 Application 对象有所了解。

enter image description here

在此处输入图片说明

In summarise way , we can say the Application context is and Configuration object created for application to run.

总之,我们可以说应用程序上下文是为应用程序运行而创建的配置对象。

The applicationContext.xmldefines the beans for the "root webapp context".Its web aware ApplicationContext.

applicationContext.xml中定义的“根web应用程序上下文”。其网站知道的ApplicationContext豆。

It is used to have beans that are shared between all servlets in a web application.

它用于在 Web 应用程序中的所有 servlet 之间共享 bean。

I hope this is helpful.

我希望这是有帮助的。