zoukankan      html  css  js  c++  java
  • 【转】 springBoot(4)---热部署,配置文件使用

    【转】 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

    然后在srcmain esources目录下,添加trigger.txt文件

    version=1

    这样你每次改好代码,不会每次保存就热部署,而是改好代码后,改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】

  • 相关阅读:
    net异步编程
    二维码的生成细节和原理
    4605 Magic Ball Game
    (Java实现) 子集和问题
    (Java实现) 组合的输出
    (Java实现) 自然数的拆分
    (Java实现) 装载问题
    (Java实现) 整数区间
    (Java实现) 最佳调度问题
    (Java实现) 最佳调度问题
  • 原文地址:https://www.cnblogs.com/Javastudy-note/p/13817934.html
Copyright © 2011-2022 走看看