文章目录
1 热部署(热启动)
SpringBoot提供热部署的功能(重新启动项目时,只加载改变过的类文件,是一种比较快的启动方式)
1.1 导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
1.2 设置IDEA
首先设置自动构建项目(发生改变时):
然后设置在项目运行时也重新构建项目(发生改变时)
ctrl+shift+alt+/
选择Registry,找到compiler.automake.allow.when.app.running并选择。
1.3 热部署时机
IDEA并不会实时将项目重新启动,而是会选择项目有改动并且焦点移出编译器时将项目重新启动。
1.4 排除文件
在yaml中配置需要排除自动部署的文件夹
#热部署排除(自动排除config下的文件)
#spring.devtools.restart.exclude=config/**
同时,springboot默认一下文件夹下的文件都不会自动部署(因为是跟web相关的文件,没有必要自动部署),而是配置了ReLoad,也就是浏览器的刷新。
/META-INF/maven
,/META-INF/resources
,/resources
,/static
,/public
,/templates
2 配置
SpringBoot的全局配置文件,默认为:
- Application.properties
- Application.yaml
以上两种文件皆可。
2.1 配置文件位置
对于maven项目,配置文件的位置默认在resources目录下,也就是class根目录下,当然也可以在其他目录下:
- file:./config/
- file:./(项目的根路径,如果有父工程,则需要放到父工程的根路径下)
- classpath:/config/
- classpath:/
优先级由高到低,高优先级的配置会覆盖低优先级的配置;
SpringBoot会从这四个位置全部加载主配置文件;
互补配置;
2.2 配置文件名字
配置文件的名字默认是Application,如果要用其他的名字,需要通过参数指定配置文件(当项目myproject的配置文件名字为myproject时):
$java -jar myproject.jar --spring.config.name=myproject
项目开发时在如下地方配置:
配置文件在特定位置
$java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
2.3 yaml具体配置
2.4 属性绑定
/**
* 将配置文件中的属性值,映射到这个组件中
* @ConfigurationProperties: 告诉SpringBoot将本类中所有属性和配置文件中相关的配置进行绑定
* prefix = "song":配置文件中哪个下面的所有属性进行一一映射
*/
@ConfigurationProperties(prefix = "song")
@Component("s")
public class song implements Serializable {
private String name;
private Singer singer;
private Long time;
private Date publishTime;
/*
* setter and getter
*/
}
构造器绑定
使用@ConstructorBinding
进行构造器绑定
@ConstructorBinding
@ConfigurationProperties(prefix = "song")
public class song implements Serializable {
private String name;
private Singer singer;
private Long time;
private Date publishTime;
private Map<String,String> maps;
private List<comment> comments;
public song(String name, Singer singer, Long time, Date publishTime, Map<String, String> maps, List<comment> comments) {
this.name = name;
this.singer = singer;
this.time = time;
this.publishTime = publishTime;
this.maps = maps;
this.comments = comments;
}
/*
* setter and getter
*/
}
在需要使用该配置的类上加上@EnableConfigurationProperties(song.class)
开启配置
@RestController
@RequestMapping("/demo")
@EnableConfigurationProperties(song.class)
public class DemoController {
@Autowired
private song s;
@GetMapping("/echo")
public song echo(){
return s;
}
}
第三方组件注入
除了使用@ConfigurationProperties
来让类绑定配置文件中的值,还可以增加@Bean
来给方法返回的类绑定值,特别是要将属性绑定到第三方组件中时。
@Component
public class MyService{
@ConfigurationProperties("acme")
@Bean
public AnotherComponent anotherComponent(){
return new AnotherComponent();
}
}
松散绑定
Springboot使用一些宽松的规则将环境属性绑定到@ConfigurationProperties
,因此,环境属性名和bean属性名之间不需要完全匹配。
@Data
@Component
//在这,不能用驼峰式,只能用羊肉串模式
@ConfigurationProperties("acme.my-person.person")
public class OwnerProperties{
private String firstName;
}
acme:
my-person:
person:
first-name: Meteor
属性文件中配置 | 说明 |
---|---|
acme.my-project.person.first-name | 羊肉串模式,常用 |
acme.myProject.person.firstName | 标准驼峰模式 |
acme.my_project.person.first_name | 下划线模式 |
ACME_MYPROJECT_PERSON_FIRSTNAME | 大写下划线,系统环境推荐使用 |
2.5 属性绑定校验
检验配置文件中传过来的值是否有效。
需要的依赖:
<!-- 配置绑定检验规范-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.0.Final</version>
</dependency>
使用:
@ConstructorBinding
@ConfigurationProperties(prefix = "song")
@Validated //在类上加这个注解
@Data
public class song implements Serializable {
@NotNull //不能为空
private String name;
@Max(35) //最大值为35
private int age;
@Email //格式为邮箱格式
private String email;
}
如果出错,控制台报错:
当类的属性是一个对象时,为了能让该对象对应的类的属性检验生效,需要给对象类型的属性加@valid
注解:
@ConstructorBinding
@ConfigurationProperties(prefix = "song")
@Validated //在类上加这个注解
@Data
public class song implements Serializable {
@NotNull //不能为空
private String name;
@Max(35) //最大值为35
private int age;
@Email //格式为邮箱格式
private String email;
@valid
private Singer singer;
class Singer{
@NotNull
private String name;
}
}
2.6 两个注解的区别
@ConfigurationProperties
和@Value
两个注解都可以用于配置文件属性的绑定,但二者有一定的区别:
特点 | @ConfigurationProperties | @Value |
---|---|---|
松散绑定 | Yes | Limit |
元数据支持 | Yes | No |
SpEL表达式{} | No | Yes |
复杂类型绑定 | Yes | No |
校验 | Yes | No |
应用场景 | Boot里面多个属性的绑定 | 单个属性的绑定 |
2.7 java spi
java spi 是“基于接口的编程+策略模式+配置文件”组合实现的动态加载机制。
例如,mysql的jdbc(实现类A)和orcle的jdbc(实现类B)都实现了连接数据库的接口功能(标准服务接口),我们(调用方)可以通过依赖jdbc来连接数据库,这样就实现了可插拔的模块调用。
详细见下面的文章:
java spi