应用上下文.这是什么? [英] application context. What is this?

查看:22
本文介绍了应用上下文.这是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的同事经常使用应用程序上下文"这个词.在很多文章中,这种搭配也经常使用.

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

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

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.

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

Can you help me to deal with this issue?

推荐答案

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

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

  • 在包中声明的 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...
    }
}

  • 对于使用 Spring MVC 的 Java Web 应用程序,DispatchServlet 将为您加载应用程序上下文,因此您只需创建一个 springapp-servlet.xmlem> 应用程序的 WEB-INF 文件夹中的文件.

  • In case of Java web applications using Spring MVC, the DispatchServlet will load the application context for you, so you only have to create a springapp-servlet.xml file in WEB-INF folder of the application.

    请注意,应用程序上下文与单个配置相关联(无论是否基于 XML).期间.

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

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

    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:

    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...
        }
    }
    

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

    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 main difference is that Spring beans singleton scopes are singleton per application context, this mean when retrieving a Bar bean that's configured in applicationContext.xml file from context will not be the same as retrieving it from context2, but several retrieves from context will return the same Bar bean instance.

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

    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 bad practice). Most people would recommend having all your beans configured in a single place (via XML or another) and loaded by a single application context.

    这篇关于应用上下文.这是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆