(1)项目的结构:搭建spring-boot多模块时,需要有一个父模块作为所有子模块的统一依赖,父模块有时只是一个文件夹,文件夹中放着父类的pom.xml文件,子模块的文件夹。比如我将建立3个子模块,我的目录结构是这样的:
在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的相关依赖:
4.0.0
cdmvs-dataColl
dataCollection
0.0.1-SNAPSHOT
pom
dataCollection
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
1.3.0.RELEASE
UTF-8
4.2.3.RELEASE
1.3.0.RELEASE
collection1
collection2
collection3
org.apache.maven.plugins
maven-compiler-plugin
3.6.0
1.8
1.8
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如下:
4.0.0
cdmvs-dataColl
collection2
0.0.1-SNAPSHOT
collection2
http://maven.apache.org
cdmvs-dataColl
dataCollection
0.0.1-SNAPSHOT
UTF-8
比如本文我是collection1作为spring boot启动点,collection2做为controller 层,collection3暂时没想到,那我collection1则应该依赖springboot的相关配置依赖,ok,引入maven如下:
4.0.0
cdmvs-dataColl
collection1
0.0.1-SNAPSHOT
jar
collection1
http://maven.apache.org
cdmvs-dataColl
dataCollection
0.0.1-SNAPSHOT
UTF-8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
true
org.springframework.boot
spring-boot-dependencies
1.3.5.RELEASE
pom
import
collecicton2 层是controller,它的pom.xml是:
4.0.0
cdmvs-dataColl
collection2
0.0.1-SNAPSHOT
collection2
http://maven.apache.org
cdmvs-dataColl
dataCollection
0.0.1-SNAPSHOT
UTF-8
org.springframework
spring-webmvc
${spring.version}
(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扫盲)
cdmvs-dataColl
collection1
${project.version}
(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)