ABOUT ME

For organizing technical knowledge and experience. demyank88@gmail.com

Today
Yesterday
Total
  • Application Context
    Framework/SPRING 2020. 4. 10. 23:38

    1. Overview

    Application-context in spring means nothing but it is the core component of spring container in spring framework. Ideally, we can say application-context one of the Spring Containers in Spring Framework and another container is bean-factory. The configuration for application-context is loaded by one of the concrete implementations 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.

    2. Description

    2.1 Responsibility

    • It provides bean factory methods for accessing application components.
    • Bean factory methods for accessing application components
    • The ability to load file resources in a generic way
    • The ability to publish events to registered listeners
    • The ability to resolve messages, supporting internationalization

    2.2 Creating one application-context

    2.2.1 AnnotationConfigApplicationContext

    Loads a Spring application context from one or more 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);

    2.2.3 ClassPathXMLApplicationContext

    Loads a context definition from one or more XML files located in the classpath

    ApplicationContext context = new ClassPathXMLApplicationContext("spingConfig.xml");

    2.2.4 FileSystemXMLApplicationContext

    Loads a context definition from one or more XML files located in the filesystem.

    ApplicationContext context = new FileSystemXMLApplicationContext("c:/spingConfig.xml");

    2.2.5 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");

    2.3 Accessing Application Context

    2.3.1 Application Structure

    build.gradle
    gradlew
    gradlew.bat
    settings.gradle
    src
    ├───main
    │   ├───java
    │   │   └───com
    │   │       └───zetcode
    │   │           │   Application.java
    │   │           └───bean
    │   │                   MyBean.java
    │   └───resources
    │           application.yml
    └───test
        ├───java
        └───resources

    2.3.2 build.gradle

    plugins {
        id 'java'
        id 'org.springframework.boot' version '2.2.2.RELEASE'
        id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    }
    
    group 'com.zetcode'
    version '1.0-SNAPSHOT'
    
    sourceCompatibility = 11
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter'
    }

    2.3.3 resources/application.yml

    spring:
      main:
        banner-mode: "off"
        log-startup-info: "false"
      
      application:
          name: "My application"

    2.3.4 com/zetcode/bean/MyBean.java

    package com.zetcode.bean;
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyBean implements ApplicationContextAware {
    
        private String applicationId;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) 
                throws BeansException {
    
            applicationId = applicationContext.getId();
        }
    
        public String getApplicationId() {
    
            return applicationId;
        }
    }

    2.3.5 com/zetcode/Application.java

    package com.zetcode;
    
    import com.zetcode.bean.MyBean;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ApplicationContext;
    
    @SpringBootApplication
    public class Application implements CommandLineRunner {
    
        @Autowired
        private ApplicationContext applicationContext;
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @Override
        public void run(String... args) throws Exception {
    
            System.out.println(applicationContext.getDisplayName());
            System.out.println(applicationContext.getId());
    
            MyBean myBean = applicationContext.getBean(MyBean.class);
            System.out.println(myBean.getApplicationId());
        }
    }

    The @SpringBootApplication annotation enables auto-configuration and component scanning. Spring Boot finds the MyBean annotation and loads it into the application context bean factory.

    @SpringBootApplication
    public class Application implements CommandLineRunner {

    With the @Autowired annotation we inject our ApplicationContext bean into the field. Now we can access the methods of the context.

    @Autowired
    private ApplicationContext applicationContext;

    We print the application context's display name and Id.

    MyBean myBean = applicationContext.getBean(MyBean.class);
    System.out.println(myBean.getApplicationId());

    We run the application with gradlew -q bootRun. From the output we can see, that we use the AnnotationConfigApplicationContext.

    3. Reference

    http://zetcode.com/springboot/applicationcontext/

    http://zetcode.com/springboot/applicationcontext/

    https://stackoverflow.com/questions/19615972/application-context-what-is-this

    https://www.dineshonjava.com/what-is-meant-by-application-context-and-how-do-you-create-one/

    'Framework > SPRING' 카테고리의 다른 글

Designed by Tistory.