1.多模块管理
1.1.为什么要多模块管理
idea中多模块开发,必须依赖于父亲模块,但是我们项目也必须是boot的parent,所以要在项目的父模块中管理boot的版本
1.2.实现方式:
pom中导入如下:
<dependencyManagement>
<dependencies>
<!--springboot版本管理,springboot相关模块引入是就不需要制定版本了-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
pom.xml:
<?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.penny</groupId>
<artifactId>day71-springboot-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springboot01-hello</module>
<module>springboot02-config</module>
<module>springboot03-test</module>
<module>springboot04-logging</module>
</modules>
<dependencyManagement>
<dependencies>
<!--springboot版本管理,springboot相关模块引入是就不需要制定版本了-->
<!--spring-boot-dependencies是spring-boot-starter-parent的父亲-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
子模块只需要在自己的Pom里写上子模块需要的包即。只要是在父模块下创建的子模块,它就会会自动继承父模块。并且父模块的pom中会自动添加子模块
<modules>
<module>springboot01-hello</module>
<module>springboot02-config</module>
<module>springboot03-test</module>
<module>springboot04-logging</module>
</modules>
例:springboot01-hello为springboot-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">
<parent>
<artifactId>day71-springboot-parent</artifactId>
<groupId>com.penny</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot01-hello</artifactId>
<dependencies>
<!--导入相关的场景启动器,就会把相关的依赖一起导入进来-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!--多模块打包专用maven插件-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--如果依赖父亲是spring-boot-starter-parent(不是多模块),就不需要添加,在里面已经配置了-->
<configuration>
<mainClass>com.penny.springboot01.App</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
此处为自动继承的父模块
<parent>
<artifactId>day71-springboot-parent</artifactId>
<groupId>com.penny</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
2.SpringBoot配置
2.1.配置文件类型
SpringBoot使用一个全局的配置文件,配置文件名是固定的
1.application.properties -传统方式,不推荐
2.application.yml-推荐使用
2.2.使用要求
要使用springboot的配置,需要在pom中引入如下即可:
···
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
···
2.3读取配置文件
1.准备一个配置文件:application.yml
注意:!!此处每个字段名后必须有空格,它为yml基本语法!!
person:
lastName: hello
age: 21
boss: true
bitrh: 2019/3/1
maps: {k1: v1,k2: 12}
lists:
- acc
- lisi
apple:
name: Apple
num: 30
2.创建一个配置类Person.java,并在里面准备好yml中的属性:
@Component
public class Person {
//获取单个值,不支持复杂数据类型
//@Value("${person.lastName}")
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Apple apple;
//getter/setter/toString省略
3.如果想获取某个单个属性,则使用:
@Value("${yml中的bean名.字段名}")(但不支持复杂数据类型:如map,list等)
如我想获取person中的lastName字段,则为:
@Component
public class Person {
//获取单个值,不支持复杂数据类型
@Value("${person.lastName}")
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Apple apple;
//getter/setter/toString省略
写个测试controller看下是否获取到lastName:
@RestController
public class ConfigController {
@Autowired
private Person person;
@RequestMapping("/hello")
public String hello(){
System.out.println(person.getLastName());
System.out.println(person);
return "hello";
}
}
结果和yml中一致
4.如果想获取该bean下的所有字段,则使用:
@ConfigurationProperties(prefix = “bean名”)
利用指定前缀的方式读取该bean下的所有字段属性
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Apple apple;
//getter/setter/toString省略
再次进行测试得到结果与之前yml中一致
5.如果想指定配置文件则可以使用:
**@PropertySource(value = {“classpath:文件名.properties”})**指定配置文件(只支持properties文件)
此处准备配制文件 person.properties:
将内容设置为:person.lastName=lowlowlow
Person.java修改如下:
@Component
@PropertySource(value = {"classpath:person.properties"})
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Apple apple;
//getter/setter/toString省略
访问浏览器得到结果与配置中一致
2.4.Profile多环境支持
2.4.1.为什么要做多环境配置
一套代码要在多种环境运行(开发,测试,上线),所以我们的配置文件要支持多种环境
我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml
application-dev.yml
application-test.yml
默认使用application.properties的配置;
spring:
profiles:
active: dev
---
server:
port: 8080
spring:
profiles: dev
---
server:
port: 8081
spring:
profiles: test
---
server:
port: 8082
spring:
profiles: prod
active处写的哪个环境,springboot就会加载哪个环境
3.SpringBootTest整合测试
它可像不用启动tomcat一样,直接就能进行测试
3.1使用:
1.首先需要在pom中导入如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
2.创建一个SpringBoot启动类:
package com.penny.springboot03;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
2.此处做测试我创建了一个测试service,并将它交给spring管理:
package com.penny.springboot03.service;
import org.springframework.stereotype.Service;
@Service
public class TestService {
public void test(){
System.out.println("testtest...");
}
}
3.再创建一个测试类用来测试是否能够在不启动浏览器的情况下打印出结果:
注意:!!此处@RunWith()里的内容要换成SpringRunner.class,同时还要再打上@SpringBootTest(classes = App.class),类指向之前创建好的springboot启动类app.class,才能用springboot的整合测试
package com.penny.springboot03;
import com.penny.springboot03.service.TestService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class MainTest {
@Autowired
private TestService testService;
@Test
public void test(){
testService.test();
}
}
4.启用测试,查看结果:与testService中的结果一致