麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > Java > 正文

詳解Spring Boot的GenericApplicationContext使用教程

2024-07-14 08:42:56
字體:
來源:轉載
供稿:網友

教程展示了如何在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包含一個返回當前日期和時間的簡單方法。此服務類將在我們的通用應用程序上下文中注冊。

@SpringBootApplicationpublic 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)@SpringBootTestpublic 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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 一级免费大片 | 双性帝王调教跪撅打屁股 | 精品国产一区二区三区成人影院 | 粉嫩蜜桃麻豆免费大片 | 黄色特级一级片 | 日本欧美国产 | 久久国产乱子伦精品 | 免费一级肉体全黄毛片 | 成人三级黄色片 | 欧美电影在线观看 | 中文字幕在线免费 | 欧美日韩亚洲成人 | 一区二区三区欧美在线 | 久久久久久久亚洲精品 | 久久久免费观看完整版 | 色婷婷一区二区三区 | 亚洲成人免费视频在线 | 精品国产一级毛片 | a一级黄色大片 | 亚洲最黄视频 | 国产噜噜噜 | 色播视频在线播放 | 在线观看网址av | 九九热免费精品视频 | 美国黄色毛片女人性生活片 | 免费观看高清视频网站 | 特级黄色小说 | 九色免费视频 | 久久亚洲精品久久国产一区二区 | av免播放| 性爱视频免费 | 日本成年免费网站 | pornoⅹxxxxhd麻豆| 叉逼视频| 国产免费资源 | 国产精品刺激对白麻豆99 | 亚洲一区二区三区精品在线观看 | 91精品国产乱码久久桃 | 国产电影av在线 | 龙的两根好大拔不出去h | 4399一级成人毛片 |