好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

详解Spring Boot的GenericApplicationContext使用教程

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

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

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

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

以下是一个标准spring boot的pom.xml:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

<?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:

?

1

2

3

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:

?

1

2

3

4

5

6

7

public class timeservice {

 

   public instant getnow() {

 

     return instant.now();

   }

}

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

@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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

@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

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

原文链接:https://www.jdon.com/50838

查看更多关于详解Spring Boot的GenericApplicationContext使用教程的详细内容...

  阅读:20次