<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Spring Boot `GenericApplicationContext` > 原文: [http://zetcode.com/springboot/genericapplicationcontext/](http://zetcode.com/springboot/genericapplicationcontext/) Spring Boot `GenericApplicationContext`教程展示了如何在 Spring 應用中使用`GenericApplicationContext`。 在示例中,我們創建一個 Spring Boot 控制臺應用。 Spring 是流行的 Java 應用框架,而 Spring Boot 是 Spring 的演進,可以幫助輕松地創建獨立的,生產級的基于 Spring 的應用。 ## `GenericApplicationContext` `GenericApplicationContext`是`ApplicationContext`的實現,它不采用特定的 bean 定義格式; 例如 XML 或注解。 ## Spring Boot `GenericApplicationContext`示例 在以下應用中,我們創建一個`GenericApplicationContext`,并使用上下文的`registerBean()`方法注冊一個新 bean。 稍后,我們使用`getBean()`從應用上下文中檢索 bean。 `pom.xml` ```java <?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 中增加了測試支持。 `spring-boot-maven-plugin`將 Spring 應用打包到可執行的 JAR 或 WAR 歸檔文件中。 `application.properties` ```java 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` ```java package com.zetcode.service; import java.time.Instant; public class TimeService { public Instant getNow() { return Instant.now(); } } ``` `TimeService`包含一個返回當前日期和時間的簡單方法。 該服務類將在我們的通用應用上下文中注冊。 `MyApplication.java` ```java package com.zetcode; import com.zetcode.service.TimeService; import org.springframework.beans.factory.annotation.Autowired; 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 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`注解的便捷注解。 ```java @Autowired private GenericApplicationContext context; ``` 我們注入`GenericApplicationContext`。 ```java context.registerBean("com.zetcode.Service.TimeService", TimeService.class, () -> new TimeService()); ``` 使用`registerBean()`方法注冊了一個新的`TimeService` bean。 ```java var timeService = (TimeService) context.getBean(TimeService.class); ``` 我們使用`getBean()`檢索 bean。 ```java System.out.println(timeService.getNow()); ``` 最后,我們調用 bean 的`getNow()`方法。 `MyApplicationTests.java` ```java package com.zetcode; import com.zetcode.service.TimeService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.support.GenericApplicationContext; import org.springframework.test.context.junit4.SpringRunner; import java.time.Instant; import static org.assertj.core.api.Assertions.assertThat; @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())); } } ``` 我們有一個使用`TimeService`的`getNow()`方法的簡單測試。 ```java var timeService = (TimeService) context.getBean("com.zetcode.Service.TimeService"); ``` 這次,我們通過給定名稱引用 Bean。 ```java $ mvn -q spring-boot:run 2018-11-24T16:31:32.146393700Z ``` 我們運行該應用。 在本教程中,我們展示了如何在 Spring 應用中使用`GenericApplicationContext`。 您可能也對相關教程感興趣: [Spring Boot `@PostConstruct`教程](/springboot/postconstruct/), [Spring Boot `@Controller`教程](/springboot/controller/), [Spring Boot `@ExceptionHandler`教程](/springboot/exceptionhandler/), [Spring Boot 上傳文件](/springboot/uploadfile/), [Spring Boot `@PathVariable`教程](/springboot/pathvariable/), [Spring Boot `@RequestParam`教程](/springboot/requestparam/), [Spring Boot `@ResponseBody`教程](/springboot/responsebody/), [Java 教程](/lang/java/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看