zoukankan      html  css  js  c++  java
  • SpringBoot使用@Value注入静态属性

    说明:SpringBoot中使用yml文件配置自定义属性,读取配置文件属性注入到实体类中,属性值都为静态属性

    配置文件:yml

    #用户信息
    userMag:
      userName: "王小波"
      userAge: "21"

    工具实体类:UserConstant

    @Component
    public class UserConstant {
    
        private static String userName;
    
        @Value("${userMag.userName}")
        private void setUserName(String userName) {//注入值
            UserConstant.userName = userName;
        }
    
        public static String getUserName() {//获取值
            return userName;
        }
    
        private static String userAge;
    
        @Value("${userMag.userAge}")
        private void setUserAge(String userAge) {//注入值
            UserConstant.userAge = userAge;
        }
    
        public static String getUserAge() {//获取值
            return userAge;
        }
    }

    项目启动,属性调用

    控制台结果输出

  • 相关阅读:
    POJ1045 Bode Plot
    POJ1044 Date bugs
    POJ1043 What's In A Name?
    POJ1042 Gone Fishing
    POJ1041 John's trip
    POJ1040 Transportation
    POJ1039 Pipe
    background-size属性
    一些CSS的备忘
    only-child选择器
  • 原文地址:https://www.cnblogs.com/bgyb/p/14945489.html
Copyright © 2011-2022 走看看