溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

GenericApplicationContext怎么在Spring Boot中使用

發(fā)布時間:2021-03-25 17:42:59 來源:億速云 閱讀:254 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關(guān)GenericApplicationContext怎么在Spring Boot中使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

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構(gòu)建的應(yīng)用程序提供依賴性和插件管理。它spring-boot-starter是核心啟動器,包括自動配置支持,日志記錄和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

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

TimeService.java:

public class TimeService {

  public Instant getNow() {

    return Instant.now();
  }
}

TimeService包含一個返回當(dāng)前日期和時間的簡單方法。此服務(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)用程序的入口點。該@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

看完上述內(nèi)容,你們對GenericApplicationContext怎么在Spring Boot中使用有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI