侧边栏壁纸
博主头像
lai博主等级

  • 累计撰写 53 篇文章
  • 累计创建 19 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

如何使用Eclipse构建一个Spring Boot复合工程

lai
lai
2021-06-16 / 0 评论 / 0 点赞 / 1,820 阅读 / 11,443 字
温馨提示:
本文最后更新于 2021-06-23,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

本文将通过Eclipse构建一个基于Spring Boot 的复合项目工程。项目工程将包含一个library jar和一个使用该libraryapplication web应用。
链接:https://spring.io/guides/gs/multi-module/

工程目的

创建一个jar提供服务简单“hello,world”的公开服务,然后该库将作为web工程的依赖引用

创建根项目

使用Eclipse创建根项目laijava-mutil-module,该项目将包含libraryapplication两个子项目

使用Eclipse快捷创建Maven项目,选中Maven Project点击next

Maven Project.png

跳过模板创建Maven项目,点击next

New Maven Project.png

输入项目信息
Group Id :laijava.com
Artifact Id : laijava-mutil-module
Packaging : pom
Finish完成创建项目

Config Maven Project.png

删除自动生成的src目录,并新增library,application目录,项目目录如下

Project Path.png

修改pom.xml文件,添加子模块配置

<modules>
        <module>library</module>
        <module>application</module>
</modules>

最终pom.xml文件如下

<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>laijava.com</groupId>
  <artifactId>laijava-mutil-module</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  
    <modules>
        <module>library</module>
        <module>application</module>
    </modules>
</project>

创建library项目

选中library目录,创建maven项目

new library project.png

Image 8.png

输入项目信息
Group Id: laijava.com
Artifact Id: library
Packaging: jar
Name: laijava-library-module
Description:A library might merely provide pure Java APIs and no Spring features
Parent Project
Group Id: org.springframework.boot
Artifact Id: spring-boot-starter-parent
Version: 2.4.6
Finish完成创建项目

Image 9.png

系统生成的pom.xml如下

<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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.6</version>
  </parent>
  <groupId>laijava.com</groupId>
  <artifactId>library</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>laijava-library-module</name>
  <description>A library might merely provide pure Java APIs and no Spring features</description>
</project>

配置pom.xml,添加依赖

  	<properties>
		<java.version>1.8</java.version>
		<!-- 配置之后,Maven->Update Project -> force Update of Snapshots/Releases 解决Eclipse下Maven 
			unknown问题 -->
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.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>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>

最后的到的pom.xml如下

<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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.6</version>
  </parent>
  <groupId>laijava.com</groupId>
  <artifactId>library</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>laijava-library-module</name>
  <description>A library might merely provide pure Java APIs and no Spring features</description>
  
   <properties>
		<java.version>1.8</java.version>
		<!-- 配置之后,Maven->Update Project -> force Update of Snapshots/Releases 解决Eclipse下Maven 
			unknown问题 -->
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.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>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>
</project>

创建服务组件

创建文件目录

└── src
    └── main
        └── java
            └── com
                └── example
                    └── multimodule
                        └── service

library将提供MyService(library/src/main/java/com/example/multimodule/service/MyService.java)服务组件供application使用,MyService类如下:

```java
package com.example.multimodule.service;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

@Service
@EnableConfigurationProperties(ServiceProperties.class)
public class MyService {
	private final ServiceProperties serviceProperties;

	public MyService(ServiceProperties serviceProperties) {
		this.serviceProperties = serviceProperties;
	}

	public String message() {
		return this.serviceProperties.getMessage();
	}
}

使用注解@ConfigurationProperties使得spring boot配置独立于application.properties,通过ServicePropertieslibrary/src/main/java/com/example/multimodule/service/ServiceProperties.java)类提供配置信息

package com.example.multimodule.service;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("service")
public class ServiceProperties {

	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

通常library库只提供java API,不具备Spring特性,这种情况下需要引用library库的应用独自配置

组件测试

使用JUnit@SpringBootTest注解对组件进行测试和验证,创建MyServiceTest.java(library/src/test/java/com/example/multimodule/service/MyServiceTest.java):

package com.example.multimodule.service;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest("service.message=Hello")
public class MyServiceTest {

  @Autowired
  private MyService myService;

  @Test
  public void contextLoads() {
    assertThat(myService.message()).isNotNull();
  }

  @SpringBootApplication
  static class TestConfiguration {
  }

}

选中library项目,右键->Debug As -> Maven test,测试项目

Test Library.png

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.685 s - in com.example.multimodule.service.MyServiceTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.226 s
[INFO] Finished at: 2021-06-23T19:58:20+08:00
[INFO] Final Memory: 16M/245M
[INFO] ------------------------------------------------------------------------

创建Application项目

参照 library maven项目,创建application maven项目,输入项目信息
Group Id: laijava.com
Artifact Id: application
Packaging: jar
Name: laijava-application-module
Description:Application project for Spring Boot
Parent Project
Group Id: org.springframework.boot
Artifact Id: spring-boot-starter-parent
Version: 2.4.6
Finish完成创建项目

application project.png

修改pom.xml并添加library依赖

    <properties>
		<java.version>1.8</java.version>
		<!-- 配置之后,Maven->Update Project -> force Update of Snapshots/Releases 解决Eclipse下Maven 
			unknown问题 -->
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
			<groupId>laijava.com</groupId>
			<artifactId>library</artifactId>
			<version>${project.version}</version>
		</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>

最终pom.xml入下所示:

<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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.6</version>
  </parent>
  <groupId>laijava.com</groupId>
  <artifactId>application</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>laijava-application-module</name>
  <description>Application project for Spring Boot</description>
  
    <properties>
		<java.version>1.8</java.version>
		<!-- 配置之后,Maven->Update Project -> force Update of Snapshots/Releases 解决Eclipse下Maven 
			unknown问题 -->
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
			<groupId>laijava.com</groupId>
			<artifactId>library</artifactId>
			<version>${project.version}</version>
		</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>

编写application应用

在application下创建文件目录

└── src
    └── main
        └── java
            └── com
                └── example
                    └── multimodule
                        └── application

主类使用@RestController调用library中的service展示信息,主类如下(application/src/main/java/com/example/multimodule/application/DemoApplication.java):

package com.example.multimodule.application;

import com.example.multimodule.service.MyService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication(scanBasePackages = "com.example.multimodule")
@RestController
public class DemoApplication {

  private final MyService myService;

  public DemoApplication(MyService myService) {
    this.myService = myService;
  }

  @GetMapping("/")
  public String home() {
    return myService.message();
  }

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

@SpringBootApplication包含了如下内容:

@Configuration:将类标记未应用上下文Bean
@EnableAutoConfiguration: 告诉Spring Boot根据类路径设置、其他 bean 和各种属性设置开始添加 bean。例如,如果spring-webmvc在类路径上,此注释将应用程序标记为 Web 应用程序并激活关键行为,例如设置DispatcherServlet.
@ComponentScan: 告诉 Spring 在包中查找其他组件、配置和服务com/example,让它找到控制器。
该main()方法使用 Spring Boot 的SpringApplication.run()方法来启动应用程序。

创建application.properties文件

需要未library服务提供配置(src/main/resources/application.properties)

service.message=Hello, World

测试应用

Test Library.png

应用正常启动


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.6)

2021-06-23 20:46:53.903  INFO 19836 --- [           main] c.e.m.application.DemoApplication        : Starting DemoApplication using Java 1.8.0_131 on LAPTOP-MFE46FH4 with PID 19836 (D:\allWorkplace\oxygenEclipse\laijava-mutil-module\application\target\classes started by 30456 in D:\allWorkplace\oxygenEclipse\taotao\laijava-mutil-module\application)
2021-06-23 20:46:53.909  INFO 19836 --- [           main] c.e.m.application.DemoApplication        : No active profile set, falling back to default profiles: default
2021-06-23 20:46:56.537  INFO 19836 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-06-23 20:46:56.554  INFO 19836 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-06-23 20:46:56.555  INFO 19836 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.46]
2021-06-23 20:46:56.688  INFO 19836 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-06-23 20:46:56.688  INFO 19836 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2682 ms
2021-06-23 20:46:57.490  INFO 19836 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2021-06-23 20:46:57.554  INFO 19836 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-06-23 20:46:57.567  INFO 19836 --- [           main] c.e.m.application.DemoApplication        : Started DemoApplication in 4.207 seconds (JVM running for 4.862)

浏览器输入:http://127.0.0.1:8080/,将会看到 Hello, World
通过Eclipse构建一个基于Spring Boot 的复合项目工程到此结束
gitee:https://gitee.com/sevenbigbee/laijava-multi-module

0

评论区