亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解Spring Boot的GenericApplicationContext使用教程

 更新時(shí)間:2018年11月25日 11:53:30   作者:解道  
這篇教程展示了如何在Spring應(yīng)用程序中使用GenericApplicationContext 。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

教程展示了如何在Spring應(yīng)用程序中使用GenericApplicationContext 。在該示例中,我們創(chuàng)建了一個(gè)Spring Boot控制臺應(yīng)用程序。

Spring是一個(gè)流行的Java應(yīng)用程序框架,Spring Boot 是Spring的演變,可以幫助您輕松創(chuàng)建獨(dú)立的,基于生產(chǎn)級別的Spring應(yīng)用程序。

GenericApplicationContext是一個(gè)實(shí)現(xiàn)ApplicationContext,它不預(yù)設(shè)指定任何bean定義格式; 例如XML或注釋。

在下面的應(yīng)用程序中,我們GenericApplicationContext 使用上下文的registerBean()方法創(chuàng)建并注冊一個(gè)新bean 。稍后我們從應(yīng)用程序上下文中檢索bean getBean()。

以下是一個(gè)標(biāo)準(zhǔn)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是一個(gè)父POM,為使用Maven構(gòu)建的應(yīng)用程序提供依賴性和插件管理。它spring-boot-starter是核心啟動(dòng)器,包括自動(dòng)配置支持,日志記錄和YAML。在spring-boot-starter-test春季增加了測試支持。將spring-boot-maven-pluginSpring應(yīng)用程序包轉(zhuǎn)換為可執(zhí)行的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

這個(gè)application.properties是Spring Boot中的主要配置文件。我們關(guān)閉Spring標(biāo)題,僅減少記錄到錯(cuò)誤的數(shù)量,并設(shè)置控制臺日志記錄模式。

TimeService.java:

public class TimeService {

  public Instant getNow() {

    return Instant.now();
  }
}

TimeService包含一個(gè)返回當(dāng)前日期和時(shí)間的簡單方法。此服務(wù)類將在我們的通用應(yīng)用程序上下文中注冊。

@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是設(shè)置Spring Boot應(yīng)用程序的入口點(diǎn)。該@SpringBootApplication注釋能夠自動(dòng)配置和組件掃描。這是一個(gè)方便的注釋,等同于@Configuration,@EnableAutoConfiguration以及@ComponentScan注釋。

這里我們注入了GenericApplicationContext。使用該registerBean()方法注冊了 一個(gè)新的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()));
  }
}

運(yùn)行:

mvn -q spring-boot:run

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • mybatis使用Integer類型查詢可能出現(xiàn)的問題

    mybatis使用Integer類型查詢可能出現(xiàn)的問題

    這篇文章主要介紹了mybatis使用Integer類型查詢可能出現(xiàn)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • maven-compiler-plugin版本指定方式

    maven-compiler-plugin版本指定方式

    這篇文章主要介紹了maven-compiler-plugin版本指定方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java 基礎(chǔ)語法讓你弄懂類和對象

    Java 基礎(chǔ)語法讓你弄懂類和對象

    C 語言是面向過程的,而 Java 是面向?qū)ο笫俏覀兂B牭降囊痪湓?,這章將帶你揭曉Java 基礎(chǔ)語法中類與對象到底是什么,需要的朋友請參考下文
    2021-08-08
  • Java多線程之線程同步

    Java多線程之線程同步

    這篇文章主要介紹了Java多線程之線程同步,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-05-05
  • MyBatis實(shí)現(xiàn)物理分頁的實(shí)例

    MyBatis實(shí)現(xiàn)物理分頁的實(shí)例

    這篇文章主要介紹了MyBatis實(shí)現(xiàn)物理分頁的實(shí)例,MyBatis使用RowBounds實(shí)現(xiàn)的分頁是邏輯分頁,有興趣的可以了解一下。
    2017-01-01
  • Eclipse 開發(fā)java 出現(xiàn)Failed to create the Java Virtual Machine錯(cuò)誤解決辦法

    Eclipse 開發(fā)java 出現(xiàn)Failed to create the Java Virtual Machine錯(cuò)誤

    這篇文章主要介紹了Eclipse 開發(fā)java 出現(xiàn)Failed to create the Java Virtual Machine錯(cuò)誤解決辦法的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • MyBatisPlus3.4.3版自動(dòng)生成代碼的使用過程

    MyBatisPlus3.4.3版自動(dòng)生成代碼的使用過程

    這篇文章主要介紹了MyBatisPlus3.4.3版自動(dòng)生成代碼的使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • 微服務(wù)eureka和nacos案例詳解

    微服務(wù)eureka和nacos案例詳解

    這篇文章主要介紹了微服務(wù)eureka和nacos,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06
  • 詳解Springboot如何通過注解實(shí)現(xiàn)接口防刷

    詳解Springboot如何通過注解實(shí)現(xiàn)接口防刷

    本文主要為大家介紹一種極簡潔、靈活通用接口防刷實(shí)現(xiàn)方式、通過在需要防刷的方法加上@Prevent?注解即可實(shí)現(xiàn)短信防刷,感興趣的可以了解一下
    2022-09-09
  • Spring Cloud Gateway替代zuul作為API網(wǎng)關(guān)的方法

    Spring Cloud Gateway替代zuul作為API網(wǎng)關(guān)的方法

    本文簡要介紹如何使用Spring Cloud Gateway 作為API 網(wǎng)關(guān)(不是使用zuul作為網(wǎng)關(guān)),結(jié)合實(shí)例代碼給大家詳細(xì)講解,感興趣的朋友跟隨小編一起看看吧
    2023-02-02

最新評論