学习在Spring应用中使用GenericApplicationContext

288 阅读2分钟

Spring Boot GenericApplicationContext教程展示了如何在Spring应用程序中使用GenericApplicationContext 。在这个例子中,我们创建了一个Spring Boot控制台应用程序。

Spring是一个流行的Java应用框架,Spring Boot是Spring的进化版,有助于轻松创建独立的、基于生产级的Spring应用。

GenericApplicationContext 是 的实现,它不假定特定的bean定义格式;例如XML或注释。ApplicationContext

Spring Boot GenericApplicationContext示例

在下面的应用程序中,我们创建了一个GenericApplicationContext ,并通过上下文的registerBean() 方法注册了一个新的bean。之后,我们用getBean() ,从应用上下文中检索该bean。

plugins {
    id 'org.springframework.boot' version '2.6.7'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.zetcode'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

这就是Gradle的构建文件。spring-boot-starter 是一个核心启动器,包括自动配置支持、日志和YAML。spring-boot-starter-test 增加了Spring中的测试支持。

spring.main.banner-mode=off
logging.level.root=ERROR
logging.pattern.console=%d{dd-MM-yyyy HH:mm:ss} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n

application.properties 是Spring Boot的主要配置文件。我们关闭了Spring的旗帜,将日志量减少到只有错误,并设置了控制台日志模式。

com/zetcode/TimeService.java

package com.zetcode.service;

import java.time.Instant;

public class TimeService {

    public Instant getNow() {

        return Instant.now();
    }
}

TimeService 包含一个简单的方法,返回当前日期和时间。这个服务类将被注册在我们的通用应用程序上下文中。

com/zetcode/Application.java

package com.zetcode;

import com.zetcode.service.TimeService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.support.GenericApplicationContext;

@SpringBootApplication
public class Application implements CommandLineRunner {

    private final GenericApplicationContext context;

    public Application(GenericApplicationContext context) {
        this.context = context;
    }

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {

        context.registerBean("com.zetcode.Service.TimeService",
                TimeService.class, TimeService::new);

        var timeService = (TimeService) context.getBean(TimeService.class);

        System.out.println(timeService.getNow());

        context.registerShutdownHook();
    }
}

Application 是设置Spring Boot应用程序的入口点。 注解可以实现自动配置和组件扫描。它是对 、 和 注释的一种方便注解。@SpringBootApplication @Configuration @EnableAutoConfiguration @ComponentScan

private final GenericApplicationContext context;

public Application(GenericApplicationContext context) {
    this.context = context;
}

我们注入GenericApplicationContext

context.registerBean("com.zetcode.Service.TimeService",
    TimeService.class, TimeService::new);

registerBean() 方法注册一个新的TimeService bean。

var timeService = (TimeService) context.getBean(TimeService.class);

我们用getBean() 检索该bean。

System.out.println(timeService.getNow());

最后,我们调用Bean的getNow() 方法。

com/zetcode/ApplicationTests.java

package com.zetcode;

import com.zetcode.service.TimeService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.support.GenericApplicationContext;

import java.time.Instant;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
public class ApplicationTests {

    @Autowired
    private GenericApplicationContext context;

    @Test
    public void testNow() {

        var timeService = (TimeService) context.getBean("com.zetcode.service.TimeService");
        var now = timeService.getNow();

        assertThat(now.isBefore(Instant.now()));
    }
}

我们有一个简单的测试,使用TimeService's getNow() 方法。

var timeService = (TimeService) context.getBean("com.zetcode.service.TimeService");

这一次我们用给定的名字来指代Bean。

$ ./gradlew bootRun
...
22-05-19 Thu 13:30:04.523 INFO  Application /home/jano/.jdks/corretto-17.0.3
22-05-19 Thu 13:30:04.524 INFO  Application MyApp

我们运行该应用程序。

在本教程中,我们已经展示了如何在Spring应用程序中使用GenericApplicationContext