zoukankan      html  css  js  c++  java
  • Springboot: 配置传参

    因为对springboot知识点掌握的不太牢靠,仅仅处于会用的状态,本着拒绝搬钻的想法,整一本书,快速过一遍基础

    配置文件:  /resources/application.properties

    server.address=192.168.251.219
    server.port=8888
    
    #https://www.bejson.com/convert/unicode_chinese/  汉字需转码
    
    #第一种演示方法,一对一传参,需要在controller方法前调用Value注解调用参数
    #传参不支持中文,可以使用在线unicode转汉字网站进行重编译
    #unicode转中文网站 https://www.bejson.com/convert/unicode_chinese/ Today.name=u4ebau751fu5f97u610fu9700u5c3du6b22uff0cu83abu4f7fu91d1u6a3du7a7au5bf9u6708u3002 Today.value=u5c11u5987u767du6d01 #第二种传参方法:
    # 第一步:需要在 dao 层定义一个实体类,并在类前添加配置文件注解 @ConfiguretionProperties(prefix="可声明前缀")
    # 第二步 :在启动类上添加 EnableConf....注解,激活
    # 第三步: 在控制器 添加自动注入注解
    # need to hava some Rand count !
    #随机字符串
    book.value=${random.value}
    #随机Int值
    book.intValue=${random.int}
    #随机Long值
    book.longValue=${random.long(200)}
    #随机UUID
    book.uuid=${random.uuid}
    #自定义属性之间的调用
    book.title=Book's name is ${Today.value}

    dao层实体类

    package com.example.demo.dao;
    
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    @ConfigurationProperties(prefix = "book")      //配置文件相关的注解
    public class BookConfigBean {
        public int getIntValue() {
            return intValue;
        }
    
        public void setIntValue(int intValue) {
            this.intValue = intValue;
        }
    
        public String getUuid() {
            return uuid;
        }
    
        public void setUuid(String uuid) {
            this.uuid = uuid;
        }
    
        private int intValue;
        private String uuid;
    }

    控制器层

    package com.example.demo.controller;
    
    import com.example.demo.dao.BookConfigBean;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class helloController {
        @Value("${Today.name}")
        // Value 注释从配置文件传值
        private  String name;
    
        @Value("${Today.value}")
        private  String value ;
    
        @Value("${book.uuid}")
        private String uuid;
    
    @GetMapping("/test")   //对应紧邻着相邻的文件
    public String hello(){
        return uuid+  "______________"+"这里的汉字可以显示————————>"  +name+  value   +"__________________-" ;
    }
    
    @Autowired   //自动导入
        private BookConfigBean bookConfigBean;
    @GetMapping("/test01")
        public BookConfigBean test01(){
        return bookConfigBean;
    }
    
    }

    启动器层

    package com.example.demo;
    
    import com.example.demo.dao.BookConfigBean;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    
    @SpringBootApplication
    @EnableConfigurationProperties(BookConfigBean.class)   //引用配置文件的实体类生效
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }

     源码见:

    链接:https://pan.baidu.com/s/1u5m-5C6QEuNw_v_vf2Tg2g 
    提取码:5mme
    

      

    当参数使用value调用的时候,就不能使用javaBean对其重复调用
    RUSH B
  • 相关阅读:
    【ICLR2018】Mixup 解读
    MobileNetV2 解读
    MobileNetV1 解读
    AdaIN 解读
    心灵奇旅
    SA-Net: Shuffle Attention for Deep Convolutional Neural Networks
    ShuffleNet V1 解读
    情人节看《唐探3》
    拆弹人
    内卷常态下的就业烦恼
  • 原文地址:https://www.cnblogs.com/tangsonghuai/p/12971275.html
Copyright © 2011-2022 走看看