热部署,配置文件使用

一、热加载

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)---热部署,配置文件使用的更多相关文章

  1. Springboot静态文件不更新的解决办法,以及Springboot实现热部署

    Springboot静态文件不更新的解决办法,以及Springboot实现热部署 原文链接:https://www.cnblogs.com/blog5277/p/9271882.html 原文作者:博 ...

  2. springBoot开启热部署

    springBoot开启热部署 这里使用devtools工具开启热部署 〇.搭建springbboot基础环境 一.添加依赖 <dependency> <groupId>org ...

  3. SpringBoot SpringCloud 热部署 热加载 热调试

    疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 架构师成长+面试必备之 高并发基础书籍 [Netty Zookeeper Redis 高并发实战 ] Crazy-Sp ...

  4. idea+spring-boot+devtools热部署

    idea+spring-boot+devtools热部署 标签: spring-boot 2017-03-20 14:45 2635人阅读 评论(1) 收藏 举报  分类: spring-boot m ...

  5. SpringBoot工程+热部署进行远程调试

    本文转载自:https://blog.csdn.net/qq_31868349/article/details/78553901 SpringBoot工程+热部署进行远程调试 本地端添加配置 在pom ...

  6. spring-boot项目热部署以及spring-devtools导致同类不能转换

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  7. SpringBoot工程热部署

    SpringBoot工程热部署 1.在pom文件中添加热部署依赖 <!-- 热部署配置 --> <dependency> <groupId>org.springfr ...

  8. 从零开始学习springboot之热部署的配置

    各位看官大家好,博主之前因为毕业设计以及毕业旅游耽搁了好长一段时间没有更新博客了,从今天起又会慢慢开始学习啦. 今天主要是来学习springboot热部署的配置. 一. 热部署 我们通常在修改某些文件 ...

  9. springboot 配置热部署 及 热部署后依旧是404的坑

    springboot配置热部署的教程网上一大堆: 个人喜欢这种方式: https://www.cnblogs.com/winner-0715/p/6666579.html 本文主要强调的是,大家如果配 ...

随机推荐

  1. APP界面设计 大概总结

    APP界面设计大概总结 首先,你得有个Android Studio 其次,你得学会有耐心的对它 最后,要适应它习惯它了解它 来看看APP的基本步骤 先有资源 再是界面布局 下来承载布局Activity ...

  2. linu输出重定向

    1.tee命令 ls | tee filename #若出现Permission Denied使用下面 ls | sudo tee filename #清空filename后重写 ls | sudo ...

  3. tensorflow学习之(十一)将python代码写入文件

    #save to file import tensorflow as tf import numpy as np ##(1)Save to file 把相关变量存储到文件中 #remember to ...

  4. orm单表查询和模糊查询

    一.单表查询 1. 返回queryset对象的查询 all() 以列表形式返回全部queryset对象 filter(**kwargs) 筛选 exclude(**kwargs) 排除 reverse ...

  5. vue.js报错:Module build failed: Error: No parser and no file path given, couldn't infer a parser.

    ERROR Failed to compile with 2 errors 12:00:33 error in ./src/App.vue Module build failed: Error: No ...

  6. (PMP)解题技巧和典型题目分析(模拟一)

  7. 信号量(Semaphore)

    在python的多线程体系中,一共有4种锁: 同步锁(互斥锁):Lock: 递归锁:RLock: 信号量:Semaphore: 同步条件锁:Condition. 信号量(semaphore)是一种可以 ...

  8. 学以致用二十九-----python3连接mysql

    在前面安装好mysql后,在一个项目中需要连接mysql,python是3.6版本 python3连接mysql需要安装pymysql模块 可以通过pip安装 查看pip 版本 pip --versi ...

  9. Spring-AspectJ 配置文件

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  10. android-基础编程-ViewPager

    ViewPager android 提供的基础V4包,android studio 导入gradle compile 'com.android.support:support-v4:25.0.0' 1 ...