详解Spring Boot的GenericApplicationContext使用教程

 更新时间:2018年11月25日 11:53:30   作者:解道  
这篇教程展示了如何在Spring应用程序中使用GenericApplicationContext 。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

Spring是一个流行的Java应用程序框架,Spring Boot 是Spring的演变,可以帮助您轻松创建独立的,基于生产级别的Spring应用程序。

GenericApplicationContext是一个实现ApplicationContext,它不预设指定任何bean定义格式; 例如XML或注释。

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

以下是一个标准Spring Boot的POM.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
      http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.zetcode</groupId>
  <artifactId>genappctx</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>genappctx</name>
  <description>Using GenericApplicationContext</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>11</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

这是Maven pom.xml文件。这spring-boot-starter-parent是一个父POM,为使用Maven构建的应用程序提供依赖性和插件管理。它spring-boot-starter是核心启动器,包括自动配置支持,日志记录和YAML。在spring-boot-starter-test春季增加了测试支持。将spring-boot-maven-pluginSpring应用程序包转换为可执行的JAR或WAR归档文件。

application.properties:

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标题,仅减少记录到错误的数量,并设置控制台日志记录模式。

TimeService.java:

public class TimeService {

  public Instant getNow() {

    return Instant.now();
  }
}

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

@SpringBootApplication
public class MyApplication implements CommandLineRunner {

  @Autowired
  private GenericApplicationContext context;

  public static void main(String[] args) {

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

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

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

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

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

    context.registerShutdownHook();
  }
}

MyApplication是设置Spring Boot应用程序的入口点。该@SpringBootApplication注释能够自动配置和组件扫描。这是一个方便的注释,等同于@Configuration,@EnableAutoConfiguration以及@ComponentScan注释。

这里我们注入了GenericApplicationContext。使用该registerBean()方法注册了 一个新的TimeService bean 。

下面是测试MyApplicationTests.java:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyApplicationTests {

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

运行:

mvn -q spring-boot:run

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • mybatis使用Integer类型查询可能出现的问题

    mybatis使用Integer类型查询可能出现的问题

    这篇文章主要介绍了mybatis使用Integer类型查询可能出现的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • maven-compiler-plugin版本指定方式

    maven-compiler-plugin版本指定方式

    这篇文章主要介绍了maven-compiler-plugin版本指定方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Java 基础语法让你弄懂类和对象

    Java 基础语法让你弄懂类和对象

    C 语言是面向过程的,而 Java 是面向对象是我们常听到的一句话,这章将带你揭晓Java 基础语法中类与对象到底是什么,需要的朋友请参考下文
    2021-08-08
  • Java多线程之线程同步

    Java多线程之线程同步

    这篇文章主要介绍了Java多线程之线程同步,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-05-05
  • MyBatis实现物理分页的实例

    MyBatis实现物理分页的实例

    这篇文章主要介绍了MyBatis实现物理分页的实例,MyBatis使用RowBounds实现的分页是逻辑分页,有兴趣的可以了解一下。
    2017-01-01
  • Eclipse 开发java 出现Failed to create the Java Virtual Machine错误解决办法

    Eclipse 开发java 出现Failed to create the Java Virtual Machine错误

    这篇文章主要介绍了Eclipse 开发java 出现Failed to create the Java Virtual Machine错误解决办法的相关资料,需要的朋友可以参考下
    2017-04-04
  • MyBatisPlus3.4.3版自动生成代码的使用过程

    MyBatisPlus3.4.3版自动生成代码的使用过程

    这篇文章主要介绍了MyBatisPlus3.4.3版自动生成代码的使用,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • 微服务eureka和nacos案例详解

    微服务eureka和nacos案例详解

    这篇文章主要介绍了微服务eureka和nacos,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06
  • 详解Springboot如何通过注解实现接口防刷

    详解Springboot如何通过注解实现接口防刷

    本文主要为大家介绍一种极简洁、灵活通用接口防刷实现方式、通过在需要防刷的方法加上@Prevent 注解即可实现短信防刷,感兴趣的可以了解一下
    2022-09-09
  • Spring Cloud Gateway替代zuul作为API网关的方法

    Spring Cloud Gateway替代zuul作为API网关的方法

    本文简要介绍如何使用Spring Cloud Gateway 作为API 网关(不是使用zuul作为网关),结合实例代码给大家详细讲解,感兴趣的朋友跟随小编一起看看吧
    2023-02-02

最新评论