1.dependencyManagement 和 dependencies 区别:
dependencies:即使子项目中不写依赖,子项目仍然会从父项目中继承该依赖项(全部继承)。
dependencyManagement:里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖,如果不在子项目中声明依赖,是不会从父项目中继承下来的,只有在子项目中写了依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父项目pom,如果子项目指定了版本,就会使用子项目中指定的jar版本。
2.子项目:
zero-webL控制层
zero-service:服务层
3. 主要实现控制层访问服务层代码进行运行,达到多模块中相互协作的功能。
创建父项目:
可以根据实际选一些依赖
父项目创建完成,结构如下:
父项目只是用来管理一些jar包版本,不做什么具体功能,所以删除多余的项,如下:
创建子项目,在父项目上右键:
子项目创建完成,结构如下(另一个子项目也如上创建):
在父项目的pom.xml中统一版本号:
下载该版本的依赖(上面我改为2.1.5版本依赖了):
先下载资源,再导入到项目中。
在父项目中不做具体功能实现,所以把相关依赖删除:
在zero-web子项目的pom.xml中加入如下代码:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
在zero-web子项目中,添加启动类:
启动类SpringBootWebApplication.java,代码:
package com.zero;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class,args);
}
}
创建一个controller文件夹,创建TestController.java写控制层代码:
package com.zero.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("test")
public class TestController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
}
在SpringBootWebApplication.java中右键运行项目。
启动成功:
访问项目:http://localhost:8080/test/hello
需要实现父项目管理子项目,子项目之间相互协助,如下操作:
通过父级管理子模块,在父项目的pom.xml中添加:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>zero-web</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>zero-service</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
zero-web控制层访问zero-service服务层,在zero-web子模块的pom.xml中添加如下代码:
<dependency>
<groupId>com.zero</groupId>
<artifactId>zero-service</artifactId>
</dependency>
由于zero-web的pom.xml中引用了zero-service子模块,所以我把web启动依赖移动到了zero-service的pom.xml文件中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在zero-service中创建service包,创建一个UserService.java写方法
创建一个UserServiceImpl.java 实现UserService.java 里面的方法:
在zero-web子模块中,修改testController.java接口方法,操作zero-service子模块中userService的testService方法,达到2个模块之间相互协作。
package com.zero.controller;
import com.zero.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("test")
public class TestController {
@Autowired
private UserService userService;
/*@GetMapping("/hello")
public String hello(){
return "hello";
}*/
@GetMapping("/hello")
public String hello(){
return userService.testService();
}
}
重新启动项目,访问项目(service方法的实现类中写的返回值为test):