ZetCode

Spring Boot ApplicationContext

最后修改于 2023 年 7 月 16 日

Spring Boot ApplicationContext 教程展示了如何在 Spring Boot 应用程序中使用 ApplicationContext。

Spring Boot 是一个用于构建 Java、Kotlin 或 Groovy 企业级应用程序的流行框架。

Spring ApplicationContext

ApplicationContext 是 Spring Boot 应用程序的基石。 它代表 Spring IoC 容器,负责实例化、配置和组装 Bean。 容器通过读取配置元数据来获取有关要实例化、配置和组装哪些对象的指令。 配置元数据以 XML、Java 注解或 Java 代码表示。

ApplicationContext 提供以下功能:

ApplicationContext 有多种实现。 例如,ClassPathXmlApplicationContext 从类路径上的 XML 文件获取配置,或者 AnnotationConfigApplicationContext 使用注解读取配置,尤其是 @Configuration

获取 ApplicationContext

要访问应用程序上下文,我们可以自动装配 ApplicationContext 接口或实现 ApplicationContextAware

Spring Boot ApplicationContext 示例

在下面的应用程序中,我们访问应用程序上下文并使用其方法。

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

这是 Spring Boot 应用程序的项目结构。

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

这是 Gradle 构建文件。spring-boot-starter 是核心启动器,包括自动配置支持、日志记录和 YAML。应用程序被打包成一个 JAR 文件。

resources/application.yml
spring:
  main:
    banner-mode: "off"
    log-startup-info: "false"
  
  application:
      name: "My application"

application.yml 文件包含应用程序配置设置。 有一些内置的应用程序属性,我们可以创建自定义属性。 banner-mode 属性是 Spring 内置属性; 我们关闭 Spring 的横幅。 使用 log-startup-info 属性,我们可以关闭启动日志记录信息。 name 是一个用于设置应用程序名称的属性。

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

MyBean 实现了 ApplicationContextAware。 Spring Boot 将应用程序上下文注入到 setApplicationContext 方法的参数中,在那里我们获取 Spring 应用程序的 Id。 (这里的 Id 是应用程序的名称。)

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

Application 中,我们创建一个 bean,调用它的方法并设置 Spring Boot 应用程序。 CommandLineRunner 接口表明一个 bean 应该在它包含在 SpringApplication 中时运行。 它可以用于在 Spring Boot 中创建命令行应用程序。

@SpringBootApplication
public class Application implements CommandLineRunner {

@SpringBootApplication 注解启用自动配置和组件扫描。 Spring Boot 找到 MyBean 注解并将其加载到应用程序上下文 bean 工厂中。

@Autowired
private ApplicationContext applicationContext;

使用 @Autowired 注解,我们将 ApplicationContext bean 注入到字段中。 现在我们可以访问上下文的方法了。

System.out.println(applicationContext.getDisplayName());
System.out.println(applicationContext.getId());

我们打印应用程序上下文的显示名称和 Id。

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

我们使用 getBean 方法从 bean 工厂获取 MyBean。 然后我们调用它的 getApplicationId 方法。

$ gradlew -q bootRun
org.springframework.context.annotation.AnnotationConfigApplicationContext@51a9ad5e
My application
My application

我们使用 gradlew -q bootRun 运行应用程序。 从输出中我们可以看到,我们使用 AnnotationConfigApplicationContext

在本文中,我们介绍了 Spring Boot ApplicationContext

作者

我叫 Jan Bodnar,是一位充满激情的程序员,拥有丰富的编程经验。 自 2007 年以来,我一直在撰写编程文章。 迄今为止,我已经撰写了 1,400 多篇文章和 8 本电子书。 我拥有超过十年的编程教学经验。

列出 所有 Spring Boot 教程