前言
发现现在项目用的spring-boot,但是单模块的,所有包和类都在同一个模块中,看一起比较混乱,于是决定自己动重新建一个分模块的项目 练练手,搭建过程中也出现了不少问题,所以记录一下,我的开发工具用的是ieda,直接建立多个model即可,下面主要说明多模块中依赖添加。
1、在主pom文件中spring boot 引入
spring boot 提供2种方法来引入spring boot版号,一是继承,二是导入。
通过<parent>
继承:
<?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.clockbone.myspringboot</groupId>
<artifactId>myspring-boot</artifactId>
<!--父模块打包类型必须为pom-->
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springboot-biz</module>
<module>springboot-model</module>
<module>springboot-dao</module>
<module>springboot-service</module>
<module>springboot-web</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!--dependencyManagement用于管理依赖版本号-->
<dependencyManagement>
</dependencyManagement>
</project>
通过导入:
直接在dependencyManagement中加入spring-boot-dependencies
模块即可
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencyManagement>
无论是通过继承还是导入,现在pom文件都继承了spring-boot parent的依赖,所以主pom不需要再添加spring-boot-starter
,spring-boot-starter-test
,
spring-boot-starter-web
,spring-boot-starter-jetty
等的依赖,因为如果添加,那么子模块中的pom在添加spring boot这些模块的时候就会报spring-boot-starter等依赖的版本号不存在
。
在本项目是通过导入的方式来引入spring boot 依赖版本的。
2、在子pom文件中spring boot依赖 引入
<?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">
<parent>
<artifactId>myspring-boot</artifactId>
<groupId>com.clockbone.myspringboot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-web</artifactId>
<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-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>
</project>
4、spring boot 启动类位置
spring boot 启动类不能直接放到java 目录下,必须要建立一个package放到package下才行,否则报错:
** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
5、spring boot启动类注解
我这里建立的spring boot启动类名叫ApplicationBoot
下面都以这个类名作为启动类,ApplicationBoot用SpringBootApplication
作为注解类,官网中有一句:
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes, as shown in the following example:
官网说明: 用@SpringBootApplication
注解和用 @Configuration
@EnableAutoConfiguration
@ComponentScan
是等效的.所以我们ApplicationBoot 中只需用@SpringBootApplication
注解即可。
一个完整的spring boot启动类
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
//官网说明: 用@SpringBootApplication 注解和用 @Configuration @EnableAutoConfiguration @ComponentScan是等效的
// The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes, as shown in the following example:
@SpringBootApplication(scanBasePackages = "com.clockbone.web") same as @Configuration @EnableAutoConfiguration @ComponentScan
public class ApplicationBoot {
public static void main(String[] args) throws Exception {
SpringApplication.run(ApplicationBoot.class, args);
}
}
6、建立controller类
如果不配置SpringBootApplication
的扫描包,如果不配置默认只扫和 ApplicationBoot
同一包下的类,如果TestController
不和SpringBootApplication
建立在同一包下的话,那么就是在SpringBootApplication
后面加上扫包的配置:
@SpringBootApplication(scanBasePackages = “com.clockbone.web”)
建立TestController 类:
@RestController
@RequestMapping(value = "/springboot/")
public class TestController {
/**
* 默认返回Json
* @return
*/
@RequestMapping("/testjson")
@ResponseBody
public MyThingResDto home() {
MyThingResDto myTingResDto = new MyThingResDto();
myTingResDto.setClose("close");
myTingResDto.setOpen("open");
return myTingResDto;
}
}
启动spring boot启动类,直接浏览器访问localhost:8080/springboot/testjson
就可以看到接口是以Josn串返回的,这是spring boot默认返回方法。如果我们需要返回html视图要怎么做呢?
7、配置视图返回
添加视图模版依赖的配置,直接在子pom中添加即可,因为主Pom这个依赖都继承了parent的依赖所以不需要添加
<!--支持返回html页面-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
用默认的视图html静态试图
资源文件的约定目录结构
Maven
的资源文件目录:/src/Java/resources
spring-boot
项目静态文件目录(比如css):/src/java/resources/static
spring-boot
项目模板文件目录(比如html):/src/java/resources/templates/index.html
最后直接返回"index"
就会直接到 templates
文件夹下访问index.html
返回视图的controller
@Controller
public class AccountController {
@RequestMapping("/list")
public String listAsHtml(Model model) {
// Duplicated logic
model.addAttribute("test");
return "list";
}
}
现在我们mvc,和rest接口都可以开发了,后面如果还遇到问题我也会一一记录。
参考官网文档:
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#_working_with_spring_boot