springboot中经常会用到properties文件中的配置,一般使用@Value注入,但是针对Utils工具类,需要注入一个静态变量有几种方法?为什么有的同学注入的值为null?

代码示例

如果直接使用@Value注入是什么结果?

/**
 * the StaticInjectionUtils
 *
 * @author Java实用技术手册
 * @date 2023-01-17
 */
@Component
public class StaticInjectionUtils {

    @Value("${normal.value}")
    private String normalValue;

    @Value("${static.value}")
    private static String staticValue;

    @PostConstruct
    public void init() {
        System.err.println("*** normalValue=" + normalValue);
        System.err.println("*** staticValue=" + staticValue);
    }
}
// 运行结果
//*** normalValue=normal  --有结果
//*** staticValue=null    --无结果

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/java-tech/p/17094350.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程

3794 178.2元 198元 9折
3777 9.8元 98元 1折