springboot创建parent_使用Maven创建Springboot的父子工程

1、在eclipse开发工具中创建一个新的Maven项目,项目类型为quickstart,如下所示:

然后项目类型为quickstart,如下所示:

然后设置Maven项目的信息(Group Id、Artifact Id、Version等),如下所示:

修改pom.xml配置文件,添加SpringBoot的依赖配置与相关插件,如下所示:

1

2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

4 http://maven.apache.org/xsd/maven-4.0.0.xsd">

5 4.0.0

6

7

8

9

10 org.springframework.boot

11 spring-boot-starter-parent

12 2.3.4.RELEASE

13

14

15

16 com.bie

17 springboot-parent

18 0.0.1-SNAPSHOT

19 jar

20

21 springboot-parent

22 http://maven.apache.org

23

24

25 1.8

26 UTF-8

27 3.1.1

28

29

30

31

32 org.springframework.boot

33 spring-boot-starter-web

34

35

36

37

38 springboot-parent

39

40

42

43 org.apache.maven.plugins

44 maven-compiler-plugin

45

46

47 ${jdk.version}

48

49 ${jdk.version}

50

51 ${project.build.sourceEncoding}

52

53

54

55

56

57

本案例,使用的spring-boot-starter-parent就是官方给出的快速构建SpringBoot项目的公共父pom.xml配置文件支持。如果你的项目开发是基于eclipse开发工具的,修改完pom.xml配置文件之后,一定要更新项目(快捷键为Alt + F5)。

2、在项目中使用SpringBoot,往往会需要引入一个标准的父pom配置(spring-boot-starter-parent),利用这个父pom文件,可以方便地进行核心依赖库的导入,并且由父pom统一管理所有的开发版本。但在实际的Maven项目开发中,往往会根据自己的需要来自定义属于自己的父pom,这样就会造成冲突。为了解决这样的问题,在SpringBoot里面,用户也可以直接以依赖管理的形式使用SpringBoot。

3、创建一个用于管理父pom的Maven项目springboot-base,如下所示:

注意:这里定义为pom类型的,如下所示:

修改springboot-base项目pom.xml配置文件,如下所示:

1

2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

4 http://maven.apache.org/xsd/maven-4.0.0.xsd">

5

6 4.0.0

7 com.bie

8 springboot-base

9 0.0.1-SNAPSHOT

10

11 pom

12 springboot-base

13 http://maven.apache.org

14

15

16 1.8

17 UTF-8

18 3.1.1

19 2.3.4.RELEASE

20

21

22

23

24

25

26 org.springframework.boot

27 spring-boot-dependencies

28 ${spring-boot-dependencies.version}

29 pom

30 import

31

32

33

34

35

36 springboot-parent

37

38

39

40 org.apache.maven.plugins

41 maven-compiler-plugin

42

43

44 ${jdk.version}

45

46 ${jdk.version}

47

48 ${project.build.sourceEncoding}

49

50

51

52

53

54

在父项目springboot-base之中建立一个新的Maven模块springboot-tentent,如下所示:

修改springboot-tentent项目的pom.xml配置文件,追加要引入的SpringBoot依赖配置,如下所示:

1 <?xml version="1.0"?>

2

5 xmlns="http://maven.apache.org/POM/4.0.0"

6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

7 4.0.0

8

9

10 com.bie

11 springboot-base

12 0.0.1-SNAPSHOT

13

14

15

16

17 springboot-tentent

18

19 springboot-tentent

20 http://maven.apache.org

21

22

23 UTF-8

24

25

26

27

28 org.springframework.boot

29 spring-boot-starter-web

30

31

32

33

如果要修改application.properties配置文件,可以创建一个src/main/resources目录,如下所示:

此时的Maven创建的springboot的父子工程的项目结构,如下所示:

4、SpringBoot程序开发完成之后,需要对程序的功能进行测试,这时需要启动Spring容器。开发者可以直接利用SpringBoot提供的依赖包来实现控制层方法测试。

在子模块springboot-tentent新增测试的依赖,如下所示:

1 <?xml version="1.0"?>

2

5 xmlns="http://maven.apache.org/POM/4.0.0"

6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

7 4.0.0

8

9

10 com.bie

11 springboot-base

12 0.0.1-SNAPSHOT

13

14

15

16

17 springboot-tentent

18

19 springboot-tentent

20 http://maven.apache.org

21

22

23 UTF-8

24

25

26

27

28 org.springframework.boot

29 spring-boot-starter-web

30

31

32 org.springframework.boot

33 spring-boot-starter-test

34 test

35

36

37 junit

38 junit

39 test

40

41

42

43

编写一个测试程序类,如下所示:

1 package org.springboot.tentent.test;2

3 import org.junit.Test;4 import org.junit.runner.RunWith;5 import org.springboot.tentent.controller.SampleController;6 import org.springframework.beans.factory.annotation.Autowired;7 import org.springframework.boot.test.context.SpringBootTest;8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;9 import org.springframework.test.context.web.WebAppConfiguration;10

11 @SpringBootTest(classes = SampleController.class) //定义要测试的Springboot类

12 @RunWith(SpringJUnit4ClassRunner.class) //使用Junit进行测试

13 @WebAppConfiguration //进行web应用配置

14 public classSampleControllerTest {15

16 @Autowired17 privateSampleController sampleController;18

19 @Test //使用Junit进行测试

20 public voidtestPrint() {21 System.out.println(this.sampleController.hello());22 }23

24 }

当右击方法,run as -> Junit Test的时候,报如下所示的错误:

1 java.lang.NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory2 at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.(JUnit5TestLoader.java:31)3 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)4 at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)5 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)6 at java.lang.reflect.Constructor.newInstance(Unknown Source)7 at java.lang.Class.newInstance(Unknown Source)8 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:367)9 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:362)10 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:306)11 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:221)12 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:205)13 Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.core.LauncherFactory14 at java.net.URLClassLoader.findClass(Unknown Source)15 at java.lang.ClassLoader.loadClass(Unknown Source)16 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)17 at java.lang.ClassLoader.loadClass(Unknown Source)18 ... 11 more

为项目添加eclipse对应的Junit库即可,具体步骤如下:在包资源管理器中右键单击您的项目build path,configure build path,libraries-->add libraries-->junit-->添加就好了。

注意:@EnableAutoConfiguration为整个SpringBoot的启动注解配置,也就是说,这个注解应该随着程序的主类一起进行定义。但是该注解有一个前提,就是只能够扫描在同一程序类包中的配置程序,很明显其功能是不足的。

对于控制器程序类,由于在项目中有许多的控制器,那么最好将这些类统一保存在一个包中(如将所有的控制器程序类保存在org.springboot.tentent.controller中,这是org.springboot.tentent的子包),在这样的环境下建议开发者使用@SpringBootApplication注解实现启动配置。

请严格遵守SpringBoot的自动配置约束,在SpringBoot开发过程中,为了简化开发配置,往往会在SpringBoot启动类下创建若干个子包,这样子包中的注解就都可以自动扫描到(@EnableAutoConfiguration注解不支持此功能),并且可以实现依赖关系的自动配置。如果不在指定的子包中,程序启动类就需要配置@ComponentScan注解设置扫描包。

JUnit Platform是提供了运行(测试框架)环境的平台,JUnit Jupiter 是新的Junit5(子项目提供了一个基于平台测试运行Jupiter的测试引擎),JUnit Vintage提供了Junit3/4的测试引擎(应该是向前兼容的意思)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值