What is meant by “application-context” in spring and how do you create one?

"application-context" in spring means nothing but it is core component of spring container in spring framework. Ideally we can say "application-context" one of the Spring Container in Spring Framework and other container is "bean-factory". The configuration for "application-context" is loaded by the one of concrete implementation of ApplicationContext interface.

The ApplicationContext is the central interface within a Spring Application for providing configuration information to the application. It is read-only at run time , but can be reloaded if necessary and supported by the application.
Popular Spring Tutorials
  1. Spring Tutorial
  2. Spring MVC Web Tutorial
  3. Spring Boot Tutorial
  4. Spring JDBC Tutorial
  5. Spring AOP Tutorial
  6. Spring Security Tutorial
Major Responsibilities of "application-context" container
application-context in Spring
  • It provides bean factory methods for accessing application components.
  • It provides the ability to load file resources in a generic fashion.
  • It provides the ability to publish event to register listeners
  • It provide the ability to resolve to support internationalization.


Creating one "application-context"

  • AnnotationConfigApplicationContext. Loads a Spring application context from one or moro Java-Based configuration classes.
    ApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class;
    
  • AnnotationConfigWebApplicationContext. Loads a Spring web application context from one or more Java-Based configuration classes.
    ApplicationContext context = new AnnotationConfigWebApplicationContext(JavaWebConfig.class;
    
  • ClassPathXMLApplicationContext. Loads a context definition from one or more XML files located in the classpath
    ApplicationContext context = new ClassPathXMLApplicationContext("spingConfig.xml");
    
  • FileSystemXMLApplicationContext. Loads a context definition from one or more XML files located in the filesystem.
    ApplicationContext context = new FileSystemXMLApplicationContext("c:/spingConfig.xml");
    
  • XMLWebApplicationContext. Loads a context definition from one or more XML files contained in the web application.
    ApplicationContext context = new XMLWebApplicationContext("/WEB-INF/config/spingConfig.xml");
    

Accessing Application Context in the other classes:
You could also access the "application-context" container into other classes. You can implement ApplicationContextAware as in the following example:
public class AnotherClass implements ApplicationContextAware {

    private ApplicationContext applicationContext;

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

}


No comments:

Post a Comment