简体   繁体   中英

What are the ways to get ApplicationContext object in Spring?

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

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

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

Thanks.

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);
    }
}

You can implement the interface ApplicationContextAware , like this :

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 get an existing application context, it creates one.

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 ***ApplicationContext a new context will be created. Therefore all Subclasses of org.springframework.context.ApplicationContext can 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 .

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. (eg eclipse virgo)

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

  1. ApplicationContextAware

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

  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)

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

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:

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

org.springframework.web.context.support.AnnotationConfigWebApplicationContext

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

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 (eg "mypackage/myresource.txt"). Useful for test harnesses as well as for application contexts embedded within JARs.

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 (eg "mydir/myfile.txt"). Useful for test harnesses as well as for standalone environments.

org.springframework.context.support.GenericApplicationContext

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

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.

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.

org.springframework.web.context.support.GenericWebApplicationContext

Subclass of GenericApplicationContext, suitable for web environments.

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.

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.

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.

org.springframework.web.portlet.context.StaticPortletApplicationContext

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

org.springframework.web.context.support.StaticWebApplicationContext

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

org.springframework.web.portlet.context.XmlPortletApplicationContext

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM