• kotlin spring @value 注解


    spring boot和kotlin里静态类使用@Value注解配置解决方案
    前言
    spring boot里默认是不能给静态属性使用@Value赋值的。所以这里使用中间变量过渡绑定。

    方案
    //applicaton.yml配置
    isDebug:true
    name:cy

    //Test类保存配置信息。使用@Value取配置里的isDebug和name变量值。
    //spring boot写法
    @Component
    public class Test {
    public static Boolean isDebug = false;
    public static String name = "";

    @Value("${isDebug}")
    public void setIsDebug(Boolean isDebug) {
    Test.isDebug = isDebug;
    }

    @Value("${name}")
    public void setname(static name) {
    Test.name = name;
    }
    }


    //kotlin写法,使用伴生对象companion object语法模拟静态类
    @Component
    class Test {
    @Value("${isDebug}")
    fun setIsDebug(isDebug: Boolean?) {
    Test.isDebug = isDebug
    }

    @Value("${name}")
    fun setname() {
    Test.name = name
    }

    companion object {
    var isDebug: Boolean? = false
    var name = ""
    }
    }

    注意

    @Component注解必需要有。
    setter方法里static不要加
    isXXX变量首字母也要大写
    注意spring boot的java写法变量使用static,kotlin里使用companion object
    ---------------------
    作者:陈袁
    来源:CSDN
    原文:https://blog.csdn.net/achenyuan/article/details/81332882
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    mysql的四种隔离
    mysql-事物
    Mysql数据备份
    线程池
    springboot整合log4j2
    springboot项目部署
    数组去重
    倒叙输出算法
    使用LLDB和debugserver对ios程序进行调试
    Linux使用pyinstaller 编译py成可执行程序
  • 原文地址:https://www.cnblogs.com/whm-blog/p/9898831.html
Copyright © 2020-2023  润新知