今天就跟大家聊聊有关GenericApplicationContext怎么在Spring Boot中使用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
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
看完上述内容,你们对GenericApplicationContext怎么在Spring Boot中使用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。