当前位置:首页 » 《随便一记》 » 正文

踩坑:Spring静态变量/构造函数注入失败(注入为null)问题的解决方案_兴趣使然的草帽路飞

1 人参与  2022年05月08日 12:17  分类 : 《随便一记》  评论

点击全文阅读


1、案例1:Spring对静态变量的注入为空

案例代码如下:

@Component
public class HelloWorld {
   /**
    * 错误案例:这种方式是不能给静态变量注入属性值的
    */
    @Value("${hello.world}")
    public static String HELLO_WORLD;
}

解决方案一:@Value注解加在setter方法上面

@Component
public class HelloWorld {
    public static String HELLO_WORLD;
    
    @Value("${hello.world}")
    public void setHELLO_WORLD(String HELLO_WORLD) {
        this.HELLO_WORLD = HELLO_WORLD;
    } 
}

解决方案二:@PostConstruct注解

因为@PostConstruct注解修饰的方法加在顺序在构造方法之后静态变量赋值之前,所以可以通过该注解解决静态变量属性值注入失败问题:

@Component
public class HelloWorld {
    public static String HELLO_WORLD;
  
    @Value("${hello.world}")
    public static String helloWorld;
    
    @PostConstruct
    public void init(){
        // 为静态变量赋值(值为从Spring IOC容器中获取的hello.world字段值)
        HELLO_WORLD = this.helloWorld;
    } 
}

2、案例2:在构造函数中使用Spring容器中的Bean对象,得到的结果为空

业务场景假设:

eg:我需要在一个类(HelloWorld)被加载的时候,调用service层的接口(UserService)去执行一个方法(sayHello),有些同学可能会在构造函数中通过调用UserService的sayHello()去实现这个需求,但是这会导致一些错误异常,请看下面的示例。

错误演示代码如下:

@Component
public class HelloWorld {
     
   /**
    * UserService注入
    */
    @Autowired
    private UserService userService;

    public HelloWorld(){
       // 这里会报空指针异常:因为 userService 的属性注入是在无参数构造函数之后,如果这里直接使用 userService ,此时该属性值为null,一个为null的成员变量调用sayHello()方法,NullPointException 异常是情理之中呀!
       userService.sayHello("hello tiandai!");
    }
}

解决方案:@PostConstruct注解

由于@PostConstruct注解修饰的方法其生命周期位于构造方法调用之后,在Spring属性值注入之前,所以,该注解可以很好的解决这个业务需求,代码如下:

@Component
public class HelloWorld {
     
   /**
    * UserService注入
    */
    @Autowired
    private UserService userService;

    public HelloWorld(){
    }
  
    @PostConstruct
    public void init(){
       userService.sayHello("hello tiandai!");
    } 
}

关于这一部分问题,还有一些奇奇怪怪的用法,参考文章:https://blog.csdn.net/dream19990329/article/details/106274283


点击全文阅读


本文链接:http://zhangshiyu.com/post/39725.html

注解  注入  变量  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1