spring-boot 多模块的搭建

时间:2022-01-11 17:01:36

(1)项目的结构:搭建spring-boot多模块时,需要有一个父模块作为所有子模块的统一依赖,父模块有时只是一个文件夹,文件夹中放着父类的pom.xml文件,子模块的文件夹。比如我将建立3个子模块,我的目录结构是这样的:spring-boot 多模块的搭建

 在dataCollection文件夹下:有collection1,collection2,collection3和pom.xml.其中pom.xml是父类的pom.xml.

(2)创建项目:以eclipse为例,打开file->new->maven project->next->next->maven-archetype-quickstart->next->填写groupId和artifactId(项目名,这里dataCollection)->finish;

删除除pom.xml外的其他文件,在pom.xml中配置maven的相关依赖:

<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>cdmvs-dataColl</groupId>
<artifactId>dataCollection</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>dataCollection</name>
<url>http://maven.apache.org</url>
<!-- 指向spring的依赖库 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
 		<!-- 设置版本的相关属性-->	<properties>		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>		<spring.version>4.2.3.RELEASE</spring.version>		<spring.boot.version>1.3.0.RELEASE</spring.boot.version>	</properties>
		<!--这里指明有3个子模块 -->	<modules>		<module>collection1</module>		<module>collection2</module>		<module>collection3</module>	</modules>
		<!-- 插件配置,标明jdk版本1.8 -->	<build>		<plugins>			<plugin>				<groupId>org.apache.maven.plugins</groupId>				<artifactId>maven-compiler-plugin</artifactId>				<version>3.6.0</version>				<configuration>					<source>1.8</source>					<target>1.8</target>				</configuration>			</plugin>		</plugins>	</build></project>
ok,一个父类的pom.xml就完成了。


接下来创建子项目collection1,collection2,collection3:

以colleciton1为例,collection2和collection3同理:file->new->maven project->next->next->maven-archetype-quickstart->next->填写groupId和artifactId(项目名,这里collection1)->finish;


colleciton1,2,3的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>cdmvs-dataColl</groupId>
<artifactId>collection2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>collection2</name>
<url>http://maven.apache.org</url>
	<!-- 指明父类的依赖时dataCollection -->	<parent>		<groupId>cdmvs-dataColl</groupId>		<artifactId>dataCollection</artifactId>		<version>0.0.1-SNAPSHOT</version>	</parent>	<properties>		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>	</properties>	<dependencies>			</dependencies></project>


(3)接下来根据各模块的特定来配置各模块的pom.xml

比如本文我是collection1作为spring boot启动点,collection2做为controller 层,collection3暂时没想到,那我collection1则应该依赖springboot的相关配置依赖,ok,引入maven如下:

<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>cdmvs-dataColl</groupId>
<artifactId>collection1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>collection1</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>cdmvs-dataColl</groupId>
<artifactId>dataCollection</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- automatically restart whenever files on the classpath change -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.3.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
collecicton2 层是controller,它的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>cdmvs-dataColl</groupId>
<artifactId>collection2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>collection2</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>cdmvs-dataColl</groupId>
<artifactId>dataCollection</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
(4)jar包的maven依赖弄好了,该配置启动springboot类了,在collection1->src->main->java文件夹下新建App.java如下:

package cdmvs_dataColl_collection3;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
* Hello world!
*
*/
@Configuration
@SpringBootApplication
@ComponentScan(basePackages = "cdmvs_dataColl_collection2")//这里是什么鬼?
public class App
{
private static Logger logger= LoggerFactory.getLogger(App.class);
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
在app.java 文件中有一个这样的注解:@ComponentScan(basePackages = "cdmvs_dataColl_collection2")
刚才不是说collection2做为我们的controller吗,那coolection1我怎么能找到colleciton2?那就通过 basePackages指向我们要扫描的文件包,同时将collection1通过依赖进来就行了。在collection2 src/main/java 下新建包 basePackages,同时写controller文件类AppController.java:

package cdmvs_dataColl_collection2;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* Hello world!
*
*/
@Controller
public class AppController
{
@RequestMapping(value="/hello", method=RequestMethod.GET)
@ResponseBody
public String hello(){
return "hello world";
}
}
(不知道 @Controller和 @RequestMapping(value="/hello", method=RequestMethod.GET) , @ResponseBody的,自己去http://www.javaworld.com/article/2078034/spring-framework/mastering-spring-mvc.html扫盲)

然后在collection1的pom.xml中将collection1引入:

		<dependency>
<groupId>cdmvs-dataColl</groupId>
<artifactId>collection1</artifactId>
<version>${project.version}</version>
</dependency>
(5)启动

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.0.RELEASE)

2017-03-22 20:21:38.594 DEBUG 16575 --- [ main] o.s.w.c.s.StandardServletEnvironment : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2017-03-22 20:21:39.842 DEBUG 16575 --- [ main] o.s.w.s.resource.ResourceUrlProvider : Looking for resource handler mappings
2017-03-22 20:21:39.843 DEBUG 16575 --- [ main] o.s.w.s.resource.ResourceUrlProvider : Found resource handler mapping: URL pattern="/**/favicon.ico", locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], class path resource []], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@1051817b]
2017-03-22 20:21:39.843 DEBUG 16575 --- [ main] o.s.w.s.resource.ResourceUrlProvider : Found resource handler mapping: URL pattern="/webjars/**", locations=[class path resource [META-INF/resources/webjars/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@35293c05]
2017-03-22 20:21:39.843 DEBUG 16575 --- [ main] o.s.w.s.resource.ResourceUrlProvider : Found resource handler mapping: URL pattern="/**", locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@620aa4ea]
2017-03-22 20:21:39.875 INFO 16575 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-03-22 20:21:39.876 DEBUG 16575 --- [ main] o.s.w.c.s.StandardServletEnvironment : Adding [server.ports] PropertySource with highest search precedence
2017-03-22 20:21:39.877 INFO 16575 --- [ main] cdmvs_dataColl_collection3.App : Started App in 6.579 seconds (JVM running for 6.782)

localhost:8080/hello看下。。。。