经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
详解Spring Boot的GenericApplicationContext使用教程
来源:jb51  时间:2018/11/27 9:57:34  对本文有异议

教程展示了如何在Spring应用程序中使用GenericApplicationContext 。在该示例中,我们创建了一个Spring Boot控制台应用程序。

Spring是一个流行的Java应用程序框架,Spring Boot 是Spring的演变,可以帮助您轻松创建独立的,基于生产级别的Spring应用程序。

GenericApplicationContext是一个实现ApplicationContext,它不预设指定任何bean定义格式; 例如XML或注释。

在下面的应用程序中,我们GenericApplicationContext 使用上下文的registerBean()方法创建并注册一个新bean 。稍后我们从应用程序上下文中检索bean getBean()。

以下是一个标准Spring Boot的POM.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <modelVersion>4.0.0</modelVersion>
  7.  
  8. <groupId>com.zetcode</groupId>
  9. <artifactId>genappctx</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. <packaging>jar</packaging>
  12.  
  13. <name>genappctx</name>
  14. <description>Using GenericApplicationContext</description>
  15.  
  16. <parent>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-parent</artifactId>
  19. <version>2.1.0.RELEASE</version>
  20. <relativePath/> <!-- lookup parent from repository -->
  21. </parent>
  22.  
  23. <properties>
  24. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  25. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  26. <java.version>11</java.version>
  27. </properties>
  28.  
  29. <dependencies>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter</artifactId>
  33. </dependency>
  34.  
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-test</artifactId>
  38. <scope>test</scope>
  39. </dependency>
  40. </dependencies>
  41.  
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-maven-plugin</artifactId>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </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:

  1. spring.main.banner-mode = off
  2. logging.level.root = ERROR
  3. logging.pattern.console =%d {dd-MM-yyyy HHmmss}%magenta([%thread])%highlight(% - 5level )%logger。%M - msgn

这个application.properties是Spring Boot中的主要配置文件。我们关闭Spring标题,仅减少记录到错误的数量,并设置控制台日志记录模式。

TimeService.java:

  1. public class TimeService {
  2.  
  3. public Instant getNow() {
  4.  
  5. return Instant.now();
  6. }
  7. }

TimeService包含一个返回当前日期和时间的简单方法。此服务类将在我们的通用应用程序上下文中注册。

  1. @SpringBootApplication
  2. public class MyApplication implements CommandLineRunner {
  3.  
  4. @Autowired
  5. private GenericApplicationContext context;
  6.  
  7. public static void main(String[] args) {
  8.  
  9. SpringApplication.run(MyApplication.class, args);
  10. }
  11.  
  12. @Override
  13. public void run(String... args) throws Exception {
  14.  
  15. context.registerBean("com.zetcode.Service.TimeService",
  16. TimeService.class, () -> new TimeService());
  17.  
  18. var timeService = (TimeService) context.getBean(TimeService.class);
  19.  
  20. System.out.println(timeService.getNow());
  21.  
  22. context.registerShutdownHook();
  23. }
  24. }

MyApplication是设置Spring Boot应用程序的入口点。该@SpringBootApplication注释能够自动配置和组件扫描。这是一个方便的注释,等同于@Configuration,@EnableAutoConfiguration以及@ComponentScan注释。

这里我们注入了GenericApplicationContext。使用该registerBean()方法注册了 一个新的TimeService bean 。

下面是测试MyApplicationTests.java:

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class MyApplicationTests {
  4.  
  5. @Autowired
  6. private GenericApplicationContext context;
  7.  
  8. @Test
  9. public void testNow() {
  10.  
  11. var timeService = (TimeService) context.getBean("com.zetcode.Service.TimeService");
  12. var now = timeService.getNow();
  13.  
  14. assertThat(now.isBefore(Instant.now()));
  15. }
  16. }

运行:

mvn -q spring-boot:run

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号