java Spring中获取ApplicationContext对象的方式有哪些?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28767479/
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-11-02 14:09:28  来源:igfitidea点击:

What are the ways to get ApplicationContext object in Spring?

javaspring

提问by Gnanadurai A

Hi i want to know what are the different ways to get ApplicationContext Object in Spring? I know only one way that is,

嗨,我想知道在 Spring 中获取 ApplicationContext 对象的不同方法是什么?我只知道一种方式,那就是

ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

is there any other ways? if it is please let me know.

还有其他方法吗?如果是,请告诉我。

Thanks.

谢谢。

回答by Evgeniy Dorofeev

You can also use annotation based configuration

您还可以使用基于注释的配置

@Configuration
public class Config {

    @Bean
    public Bean1 bean1() {
        return new Bean1();
    }

    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
    }
}

回答by Tunaki

You can implement the interface ApplicationContextAware, like this :

您可以实现该接口ApplicationContextAware,如下所示:

public class MyClass implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

}

If you are using annotation, you could also autowire it

如果您正在使用注释,您也可以自动装配它

@Autowired
private ApplicationContext applicationContext;

Also, the code you wrote does not getan existing application context, it createsone.

此外,您编写的代码没有获得现有的应用程序上下文,而是创建了一个。

回答by Martin Baumgartner

Well, there are a lot of ways out there, I wonder whom would know them all...

嗯,外面有很多方法,我想知道谁会知道所有这些......

But first, we need to make a difference between instanting a new context, or getting a running and existing application-context.

但首先,我们需要区分实例化一个新的上下文,或者获取一个正在运行的和现有的应用程序上下文。

By new ***ApplicationContexta new context will be created. Therefore all Subclasses of org.springframework.context.ApplicationContextcan be used to create a new ApplicationContext. You can find all implementing classes here. The new way to instantiate a spring-context is through AnnotationConfigApplicationContext.

通过new ***ApplicationContext一个新的上下文将被创建。因此所有的子类of org.springframework.context.ApplicationContext都可以用来创建一个新的 ApplicationContext。您可以在此处找到所有实现类。实例化 spring-context 的新方法是通过AnnotationConfigApplicationContext

Also, you can add a displatcher-servlet or an servlet-listener in your web.xml. Or use a framework like gemini-blueprint in an osgi-environment which starts all xml-files in meta-inf/spring. (e.g. eclipse virgo)

此外,您可以在 web.xml 中添加 displatcher-servlet 或 servlet-listener。或者在 osgi-environment 中使用像 gemini-blueprint 这样的框架,它启动 meta-inf/spring 中的所有 xml 文件。(例如日食处女座)

On the other hand, you can get an existing context (which means not a new one) through different ways:

另一方面,您可以通过不同的方式获取现有的上下文(这意味着不是新的):

  1. ApplicationContextAware
  1. 应用上下文感知

Implement the ApplicationContextAwareinterface and you will get the context via setApplicationContext(ApplicationContext applicationContext)method.

实现ApplicationContextAware接口,您将通过setApplicationContext(ApplicationContext applicationContext)方法获取上下文。

  1. Just add @Autowired private ApplicationContext applicationContext;to your spring bean. But make sure it is a spring bean.

  2. In your web-application, you can get the context of your listener-context via ApplicationContextUtils.getWebApplicationContext( servletcontext)

  1. 只需添加@Autowired private ApplicationContext applicationContext;到您的春豆中即可。但请确保它是春豆。

  2. 在您的 Web 应用程序中,您可以通过以下方式获取侦听器上下文的上下文 ApplicationContextUtils.getWebApplicationContext( servletcontext)

There would a lot of more ways, but these are those which popped up in my mind quickly.

会有更多的方法,但这些是我很快想到的方法。

回答by GabiM

If you are referring to the possible way you can create an ApplicationContext and not to the ways such an instance can be passed through your code then I suggest taking a look at the Spring javadoc for ApplicationContext. So based on this the concrete implementations of this interface are:

如果您指的是创建 ApplicationContext 的可能方式,而不是通过代码传递此类实例的方式,那么我建议您查看Spring javadoc for ApplicationContext。所以基于此,该接口的具体实现是:

org.springframework.context.annotation.AnnotationConfigApplicationContext

org.springframework.context.annotation.AnnotationConfigApplicationContext

Standalone application context, accepting annotated classes as input - in particular @Configuration-annotated classes, but also plain @Component types and JSR-330 compliant classes using javax.inject annotations. Allows for registering classes one by one using register(Class...) as well as for classpath scanning using scan(String...).

独立的应用程序上下文,接受带注释的类作为输入 - 特别是 @Configuration-annotated 类,还有普通的 @Component 类型和使用 javax.inject 注释的 JSR-330 兼容类。允许使用 register(Class...) 一一注册类,以及使用 scan(String...) 进行类路径扫描。

org.springframework.web.context.support.AnnotationConfigWebApplicationContext

org.springframework.web.context.support.AnnotationConfigWebApplicationContext

This is essentially the equivalent of AnnotationConfigApplicationContext for a web environment.

这本质上等同于 Web 环境的 AnnotationConfigApplicationContext。

org.springframework.context.support.ClassPathXmlApplicationContext

org.springframework.context.support.ClassPathXmlApplicationContext

Standalone XML application context, taking the context definition files from the class path, interpreting plain paths as class path resource names that include the package path (e.g. "mypackage/myresource.txt"). Useful for test harnesses as well as for application contexts embedded within JARs.

独立的 XML 应用程序上下文,从类路径中获取上下文定义文件,将普通路径解释为包含包路径的类路径资源名称(例如“mypackage/myresource.txt”)。对于测试工具以及嵌入在 JAR 中的应用程序上下文很有用。

org.springframework.context.support.FileSystemXmlApplicationContext

org.springframework.context.support.FileSystemXmlApplicationContext

Standalone XML application context, taking the context definition files from the file system or from URLs, interpreting plain paths as relative file system locations (e.g. "mydir/myfile.txt"). Useful for test harnesses as well as for standalone environments.

独立的 XML 应用程序上下文,从文件系统或 URL 获取上下文定义文件,将普通路径解释为相对文件系统位置(例如“mydir/myfile.txt”)。适用于测试工具以及独立环境。

org.springframework.context.support.GenericApplicationContext

org.springframework.context.support.GenericApplicationContext

Generic ApplicationContext implementation that [...] does not assume a specific bean definition format

[...] 不采用特定 bean 定义格式的通用 ApplicationContext 实现

org.springframework.context.support.GenericXmlApplicationContext

org.springframework.context.support.GenericXmlApplicationContext

Convenient application context with built-in XML support. This is a flexible alternative to ClassPathXmlApplicationContext and FileSystemXmlApplicationContext, to be configured via setters, with an eventual AbstractApplicationContext.refresh() call activating the context.

具有内置 XML 支持的便捷应用程序上下文。这是 ClassPathXmlApplicationContext 和 FileSystemXmlApplicationContext 的灵活替代方案,可通过 setter 进行配置,最终通过 AbstractApplicationContext.refresh() 调用激活上下文。

org.springframework.context.support.GenericGroovyApplicationContext

org.springframework.context.support.GenericGroovyApplicationContext

An ApplicationContext implementation that extends GenericApplicationContext. [...] Consider this as the equivalent of GenericXmlApplicationContext for Groovy bean definitions, or even an upgrade thereof since it seamlessly understands XML bean definition files as well.

扩展 GenericApplicationContext 的 ApplicationContext 实现。[...] 将其视为 Groovy bean 定义的 GenericXmlApplicationContext 等价物,或者甚至是其升级,因为它也无缝理解 XML bean 定义文件。

org.springframework.web.context.support.GenericWebApplicationContext

org.springframework.web.context.support.GenericWebApplicationContext

Subclass of GenericApplicationContext, suitable for web environments.

GenericApplicationContext 的子类,适用于 Web 环境。

org.springframework.web.context.support.GroovyWebApplicationContext

org.springframework.web.context.support.GroovyWebApplicationContext

WebApplicationContext implementation which takes its configuration from Groovy bean definition scripts and/or XML files, as understood by an GroovyBeanDefinitionReader. This is essentially the equivalent of GenericGroovyApplicationContext for a web environment.

正如 GroovyBeanDefinitionReader 所理解的那样,WebApplicationContext 实现从 Groovy bean 定义脚本和/或 XML 文件中获取其配置。这本质上等同于 Web 环境的 GenericGroovyApplicationContext。

org.springframework.jca.context.ResourceAdapterApplicationContext

org.springframework.jca.context.ResourceAdapterApplicationContext

ApplicationContext implementation for a JCA ResourceAdapter. Needs to be initialized with the JCA BootstrapContext, passing it on to Spring-managed beans that implement BootstrapContextAware.

JCA ResourceAdapter 的 ApplicationContext 实现。需要使用 JCA BootstrapContext 进行初始化,将其传递给实现 BootstrapContextAware 的 Spring 管理的 bean。

org.springframework.context.support.StaticApplicationContext

org.springframework.context.support.StaticApplicationContext

ApplicationContext implementation which supports programmatic registration of beans and messages, rather than reading bean definitions from external configuration sources. Mainly useful for testing.

ApplicationContext 实现支持 bean 和消息的编程注册,而不是从外部配置源读取 bean 定义。主要用于测试。

org.springframework.web.portlet.context.StaticPortletApplicationContext

org.springframework.web.portlet.context.StaticPortletApplicationContext

Static Portlet-based ApplicationContext implementation for testing. Not intended for use in production applications.

用于测试的基于静态 Portlet 的 ApplicationContext 实现。不适用于生产应用。

org.springframework.web.context.support.StaticWebApplicationContext

org.springframework.web.context.support.StaticWebApplicationContext

Static WebApplicationContext implementation for testing. Not intended for use in production applications.

用于测试的静态 WebApplicationContext 实现。不适用于生产应用。

org.springframework.web.portlet.context.XmlPortletApplicationContext

org.springframework.web.portlet.context.XmlPortletApplicationContext

Portlet-based WebApplicationContext implementation which takes its configuration from XML documents, understood by an XmlBeanDefinitionReader.

基于 Portlet 的 WebApplicationContext 实现从 XML 文档获取其配置,由 XmlBeanDefinitionReader 理解。

org.springframework.web.context.support.XmlWebApplicationContext

org.springframework.web.context.support.XmlWebApplicationContext

WebApplicationContext implementation which takes its configuration from XML documents, understood by an XmlBeanDefinitionReader. This is essentially the equivalent of GenericXmlApplicationContext for a web environment.

WebApplicationContext 实现,它从 XML 文档获取其配置,由 XmlBeanDefinitionReader 理解。这本质上等同于 Web 环境的 GenericXmlApplicationContext。