详解Spring Boot的GenericApplicationContext使用教程

作者:解道 时间:2021-06-09 14:05:40 

教程展示了如何在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

来源:https://www.jdon.com/50838

标签:Spring,Boot,GenericApplicationContext
0
投稿

猜你喜欢

  • spring缓存cache的使用详解

    2023-03-28 11:36:14
  • 使用ViewPager实现左右循环滑动及滑动跳转

    2023-04-12 20:42:43
  • Java数组的定义与使用

    2023-08-12 11:44:38
  • Android仿QQ微信侧滑删除效果

    2023-07-08 11:22:15
  • Android 实现当下最流行的吸顶效果

    2021-12-30 18:46:33
  • Java卡片布局管理器解释及实例

    2022-03-12 18:26:19
  • c# 基于任务的异步编程模式(TAP)的异常处理

    2023-08-03 15:44:06
  • 基于C#调用OCX控件的常用方法(推荐)

    2021-06-24 11:56:29
  • Java异步处理机制实例详解

    2022-05-12 01:39:37
  • Android开发之WebView组件的使用解析

    2022-07-22 21:52:15
  • Maven打包没有指定主类问题(xxx.jar中没有主清单属性)

    2023-11-26 21:34:44
  • Java 从Set里面取出有序的记录详解及实例

    2021-07-08 09:07:33
  • Springboot通过谷歌Kaptcha 组件生成图形验证码功能

    2021-07-24 23:42:47
  • IDEA查看Scala的源码的教程图解

    2023-12-13 22:50:45
  • java中使用xls格式化xml的实例

    2023-06-13 09:43:07
  • dubbo入门指南及demo实例详解

    2023-08-24 04:49:07
  • Java中Map的遍历方法及性能测试

    2023-07-14 08:54:15
  • C#如何利用反射将枚举绑定到下拉框详解

    2022-09-12 01:48:09
  • android RecyclerView的一些优化点介绍

    2021-08-21 06:27:08
  • JAVAsynchronized原理详解

    2023-05-17 12:18:32
  • asp之家 软件编程 m.aspxhome.com