目录
5.2 SpringBoot2.X结合dev-tool实现IDEA项目热部署
2.Springboot里面常用xx.properties(推荐)
5.1 什么是热部署
- 应用正在运行的时候进行升级功能,不需要重新启动应用;
- 对于Java应用程序来说,热部署就是在运行时更新Java类文件;
- 好处:不需要重新手工启动应用,提高本地开发效率(线上尽量关闭热部署);
- 常见实现热部署的方式
1.Jrebel
2.Spring Loaded
3.spring-boot-devtools
5.2 SpringBoot2.X结合dev-tool实现IDEA项目热部署
1.pom文件添加依赖包
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
2.IDEA配置
3.快捷键打开Registry
window快捷键(Shift+Ctrl+Alt+/)之后选择compiler.automake.allow.when.app.running ,重启 idea就⾏!!!
4.问题记录
找不到“compiler.automake.allow.when.app.running”,使用以下方法:
5.3 常见配置文件
常见的配置文件格式:xml、properties、json、yaml
1.Springboot里面常用的xx.yml
- YAML(Yet Another Markup Language)
- 写 YAML 要⽐写 XML 快得多(⽆需关注标签或引号) 使⽤空格 Space 缩进表示分层,不同层 次 之间的缩进可以使⽤不同的空格数⽬
- 注意:key后⾯的冒号,后⾯⼀定要跟⼀个空格,树状结
2.Springboot里面常用xx.properties(推荐)
- key=value格式
- 语法简单,不容易出错
5.4 SpringBoot注解配置文件映射属性和实体类
测试配置文件(仿微信支付)
1.方式一
- Controller上面配置@PropertySource({"classpath:resource.properties"});
- 增加属性@Value(“${test.name}”)private String name;
控制类:
2.方式二:实体类配置文件
- 添加@Component注解;
- 使用@PropertySource注解制定配置文件位置;
- 使用@ConfigurationProperties注解,设置相关属性;
- 必须通过注入IOC对象Resource进来,才能再累中使用获取的配置文件值
新增配置类
/**
* 微信支付配置类
* */
@Configuration
@PropertySource(value = "classpath:pay.properties")
public class WXConfig {
@Value("wxpay.appid")
private String payAppid;
@Value("wxpay.sercret")
private String paySecret;
@Value("wx.mechid")
private String payMechId;
public String getPayAppid() {
return payAppid;
}
public void setPayAppid(String payAppid) {
this.payAppid = payAppid;
}
public String getPaySecret() {
return paySecret;
}
public void setPaySecret(String paySecret) {
this.paySecret = paySecret;
}
public String getPayMechId() {
return payMechId;
}
public void setPayMechId(String payMechId) {
this.payMechId = payMechId;
}
}
控制类:
@RestController
@RequestMapping("api/v1/test")
@PropertySource("classpath:pay.properties")
public class TestController {
@Autowired
private WXConfig wxConfig;
@GetMapping("get_config")
public JsonData testConfig(){
Map<String,String> map = new HashMap<>();
map.put("appid",wxConfig.getPayAppid());
map.put("secret",wxConfig.getPaySecret());
map.put("mechID",wxConfig.getPayMechId());
return JsonData.buildSuccess(map);
}
}
运行结果