使用@Value注解给静态变量赋值

第一步:在yml文件中配置需要的参数

clickhouse:
  address: jdbc:clickhouse://172.20.xxx.xxx:8123
  username: default
  password: xxxxx
  db: marketing
  socketTimeout: 60000

第二步:

类上一定要先注明@Component注解,在程序启动是自动加载,否则静态参数仍旧获取不到配置文件的值

创建静态变量

给静态变量set方法上添加@Value()

第三步:

@Value("${clickhouse.address}")
    public  void setClickhouseAddress(String address) {
        ClickHouseUtil.clickhouseAddress = address;
    }

完整的demo代码:

package com.qingmu.annotation.value;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lida
 * @version 1.0
 * @create 2022/9/17 17:31
 * @description 注解value
 * <p>
 * 为常量注入配置信息
 */
@Slf4j
@Component
@RestController
public class ConstValue {

    public static String db;
    public static String address;
    public static String username;
    public static String password;
    public static String socketTimeout;

    @Value("${clickhouse.address}")
    public void setAddress(String address) {
        ConstValue.address = address;
    }

    @Value("${clickhouse.username}")
    public void setUsername(String username) {
        ConstValue.username = username;
    }

    @Value("${clickhouse.password}")
    public void setPassword(String password) {
        ConstValue.password = password;
    }

    @Value("${clickhouse.db}")
    public void setDb(String db) {
        ConstValue.db = db;
    }

    @Value("${clickhouse.socketTimeout}")
    public void setSocketTimeout(String socketTimeout) {
        ConstValue.socketTimeout = socketTimeout;
    }


    @RequestMapping("/test")
    public String value() {
        log.info("ConstValue.db:{}", ConstValue.db);
        log.info("ConstValue.address:{}", ConstValue.address);
        log.info("ConstValue.username:{}", ConstValue.username);
        log.info("ConstValue.password:{}", ConstValue.password);
        log.info("ConstValue.socketTimeout:{}", ConstValue.socketTimeout);
        return "success";
    }
}

或者demo信息

package com.qingmu.annotation.value;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author lida
 * @version 1.0
 * @create 2022/9/18 10:25
 * @description
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class ConstValueTest {

    public static String db;
    public static String address;
    public static String username;
    public static String password;
    public static String socketTimeout;

    @Value("${clickhouse.address}")
    public void setAddress(String address) {
        ConstValue.address = address;
    }

    @Value("${clickhouse.username}")
    public void setUsername(String username) {
        ConstValue.username = username;
    }

    @Value("${clickhouse.password}")
    public void setPassword(String password) {
        ConstValue.password = password;
    }

    @Value("${clickhouse.db}")
    public void setDb(String db) {
        ConstValue.db = db;
    }

    @Value("${clickhouse.socketTimeout}")
    public void setSocketTimeout(String socketTimeout) {
        ConstValue.socketTimeout = socketTimeout;
    }

    @Test
    public void value() {
        log.info("ConstValue.db:{}", ConstValue.db);
        log.info("ConstValue.address:{}", ConstValue.address);
        log.info("ConstValue.username:{}", ConstValue.username);
        log.info("ConstValue.password:{}", ConstValue.password);
        log.info("ConstValue.socketTimeout:{}", ConstValue.socketTimeout);
    }

}
posted @ 2020-02-20 16:33  King-DA  阅读(2182)  评论(0编辑  收藏  举报