springBoot(4)---热部署,配置文件使用
热部署,配置文件使用
一、热加载
spring为开发者提供了一个名为spring-boot-devtools的模块来使Spring Boot应用支持热部署,提高开发者的开发效率,无需手动重启Spring Boot应用。
devtools的原理
深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为restart ClassLoader,这样在有代码更改的时候,原来的restart ClassLoader 被丢弃,重新创建一个restart ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间。
官方地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-devtools
实现热部署,首先要引入:spring-boot-devtools.jar包
核心依赖包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
添加依赖后,在ide里面重启应用,后续修改后马上可以生效
默认不被热部署的文件
1、/META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates
2、指定文件不进行热部署 spring.devtools.restart.exclude=static/**,public/**
在开发中,我们会思考一个问题?
如果你写一个逻辑代码,需要好几个文件,总不能你每保存一次就进行一次热部署,这里有个解决方法。
在application.properties添加手工触发重启
#指定某些文件不进行监听,即不会进行热加载
#spring.devtools.restart.exclude=application.properties #通过触发器,去控制什么时候进行热加载部署新的文件
spring.devtools.restart.trigger-file=trigger.txt
然后在src\main\resources目录下,添加trigger.txt文件
version=
这样你每次改好代码,不会每次保存就热部署,而是改好代码后,改version=2就会进行热部署。
注意点:生产环境不要开启这个功能,如果用java -jar启动,springBoot是不会进行热部署的
二、SpringBoot注解把配置文件自动映射到属性和实体类实战
方式一、Controller上面配置
简介:讲解使用@value注解配置文件自动映射到属性和实体类
1、配置文件加载
方式一
1、Controller上面配置
@PropertySource({"classpath:resource.properties"})
2、增加属性
@Value("${test.name}")
private String name;
举例
上篇的文件上传的地址我是写死的。
这样显然不科学,这里改成写在1.application.properties配置文件里。
#文件上传路径配置
web.file.path=C:/Users/chenww/Desktop/springbootstudy/springbootstudy/src/main/resources/static/images/
2在FileController 类中
@Controller
@PropertySource({"classpath:application.properties"})
public class FileController { //文件放在项目的images下
// private String filePath = "C:\\Users\\chenww\\Desktop\\springbootstudy\\springbootstudy\\src\\main\\resources\\static\\images\\";
@Value("${web.file.path}")
private String filePath;
总结:
1:@PropertySource代表读取哪个文件
2:@Value通过key得到value值
方式二:实体类配置文件
步骤:
1、添加 @Component 注解;
2、使用 @PropertySource 注解指定配置文件位置;
3、使用 @ConfigurationProperties 注解,设置相关属性;
4、必须 通过注入IOC对象Resource 进来 , 才能在类中使用获取的配置文件值。
@Autowired
private ServerSettings serverSettings;
案例:
1.在application.properties
#测试配置文件路径
test.domain=www.jincou.com
test.name=springboot
2.创建ServerSettings实体
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; //服务器配置 @Component
@PropertySource({"classpath:application.properties"})
@ConfigurationProperties public class ServerSettings { //名称test.domain是key值
@Value("${test.domain}")
private String name;
//域名地址
@Value("${test.name}")
private String domain; //提供set和get方法
}
三创建GetController
import com.jincou.model.ServerSettings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class GetController { //需要注入
@Autowired
private ServerSettings serverSettings; @GetMapping("/v1/test_properties")
public Object testPeroperties(){ return serverSettings;
}
}
页面效果

其实上面还可以做个优化:
创建ServerSettings实体
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; //服务器配置 @Component
@PropertySource({"classpath:application.properties"})
@ConfigurationProperties(prefix="test")
//这里加了个test前缀
public class ServerSettings { //这是不需要写vlaue标签,只要text.name去掉前缀test后的name和这里name相同,就会自动赋值
private String name; //域名地址
private String domain; //提供set和get方法
}
想太多,做太少,中间的落差就是烦恼。想没有烦恼,要么别想,要么多做。上尉【6】
springBoot(4)---热部署,配置文件使用的更多相关文章
- Springboot静态文件不更新的解决办法,以及Springboot实现热部署
Springboot静态文件不更新的解决办法,以及Springboot实现热部署 原文链接:https://www.cnblogs.com/blog5277/p/9271882.html 原文作者:博 ...
- springBoot开启热部署
springBoot开启热部署 这里使用devtools工具开启热部署 〇.搭建springbboot基础环境 一.添加依赖 <dependency> <groupId>org ...
- SpringBoot SpringCloud 热部署 热加载 热调试
疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 架构师成长+面试必备之 高并发基础书籍 [Netty Zookeeper Redis 高并发实战 ] Crazy-Sp ...
- idea+spring-boot+devtools热部署
idea+spring-boot+devtools热部署 标签: spring-boot 2017-03-20 14:45 2635人阅读 评论(1) 收藏 举报 分类: spring-boot m ...
- SpringBoot工程+热部署进行远程调试
本文转载自:https://blog.csdn.net/qq_31868349/article/details/78553901 SpringBoot工程+热部署进行远程调试 本地端添加配置 在pom ...
- spring-boot项目热部署以及spring-devtools导致同类不能转换
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- SpringBoot工程热部署
SpringBoot工程热部署 1.在pom文件中添加热部署依赖 <!-- 热部署配置 --> <dependency> <groupId>org.springfr ...
- 从零开始学习springboot之热部署的配置
各位看官大家好,博主之前因为毕业设计以及毕业旅游耽搁了好长一段时间没有更新博客了,从今天起又会慢慢开始学习啦. 今天主要是来学习springboot热部署的配置. 一. 热部署 我们通常在修改某些文件 ...
- springboot 配置热部署 及 热部署后依旧是404的坑
springboot配置热部署的教程网上一大堆: 个人喜欢这种方式: https://www.cnblogs.com/winner-0715/p/6666579.html 本文主要强调的是,大家如果配 ...
随机推荐
- pypi batch download
https://wiki.archlinux.org/index.php/Python_package_guidelines_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87 ...
- PCL安装
本文是在Ubuntu16.04下安装PCL. 按照官网的教程,有两种方法可以安装: 1.直接安装预先编译好的二进制库文件 sudo add-apt-repository ppa:v-launchpad ...
- tensorflow学习之(八)使用dropout解决overfitting(过拟合)问题
#使用dropout解决overfitting(过拟合)问题 #如果有dropout,在feed_dict的参数中一定要加入dropout的值 import tensorflow as tf from ...
- Promise注意点
一. Promise API 概述 var p = new Promise( function(resolve,reject){ // resolve(..) 用于决议 / 完成这个 promise ...
- mac os ssh远程链接centos提示证书错误解决方法
下面是错误提示 IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you ...
- Solr Cloud安装
1. zookeeper-3.4.10安装: zoo.cfg配置文件: dataDir=/usr/share/zookeeper/data/ clientPort=2181 server.1=10.0 ...
- vue图片上传
之前花了两个月用vue做了一个建筑照片我的webApp,前端就我一人,负责用vue写页面对接接口,后台一个程序员负责写接口,嵌套个安卓ios的壳.搞的是风风火火,过程也是很心累,大多不在技术,在于所谓 ...
- 补发————DOM与BOM
什么是Dom? DOM是w3c(万维网联盟)的标准. DOM定义了HTML与ML文档的标准: w3c文档对象模型(DOM)是中立于平台与语言的接口,他允许程序和脚本动态访问和更新文档的内容.结构和样式 ...
- Settings 参数记录
DOWNLOAD_FAIL_ON_DATALOSS : 参数:TRUE.FALSE 如果设置为 True : scrapy.Request 有一个 errback 参数, 当 Request 请求出错 ...
- Chapter 8 The Simplest Plug-in Solution
This chapter introduces the simplest plug-in solution that are applicable to the four major componen ...