• Spring @Autowired 注解静态变量


    最近应该项目的需要,需要使用一个工具类来访问数据库。

    但是这个工具类又被定义成静态访问了。

    我们也需要设置一个静态变量来访问数据库。

    1. @Autowired
    2. private static VisaRepository visaRepository;
    3. private static VisaCheckeeRepository visaCheckeeRepository;

    上面的代码在编译的时候是没有问题的。

    但是在程序运行的时候提示空对象异常。

    类加载后静态成员是在内存的共享区,静态方法里面的变量必然要使用静态成员变量。

    通过日志我们可以非常明确的知道上面异常的主要原因就是因为 VisaRepository 这个变量没有初始化,简单来说就是没有被 @Autowired 上去。

    问题和解决

    在 Spring 框架中,不能 @Autowired一个静态变量,使之成为一个Spring bean。

    这是因为当类加载器加载静态变量时,Spring上下文尚未加载。所以类加载器不会在bean中正确注入静态类。

    这个和静态变量这个属性有关的,因为静态变量总是先于 Spring 的 上下文加载。

    使用构造函数

    其实 IDEA 已经非常明确的建议我们不要使用变量 @Autowired 的方式。

    而建议使用构造方法或者 Setter 的方式。

    1. Marks a constructor, field, setter method, or config method as to be autowired by Spring's dependency injection facilities. This is an alternative to the JSR-330 javax.inject.Inject annotation, adding required-vs-optional semantics.

    在这个时候,我们只需要简单的将 @Autowired 放到构造方法上。

    按照下面这样写就可以了。

    1. private static VisaRepository visaRepository;
    2. private static VisaCheckeeRepository visaCheckeeRepository;
    3. @Autowired
    4. public CheckeeUtils(VisaCheckeeRepository visaCheckeeRepository, VisaRepository visaRepository) {
    5. this.visaCheckeeRepository = visaCheckeeRepository;
    6. this.visaRepository = visaRepository;
    7. }

    setter

    给静态变量加一个 setter 方法,并在这个方法上加上@Autowired。

    Spring 就能扫描到AutowiredTypeComponent 的 bean,然后通过setter方法注入。

    1. @Autowired
    2. public static void setVisaRepository(VisaRepository visaRepository) {
    3. CheckeeUtils.visaRepository = visaRepository;
    4. }

    定义 2 个变量

    可以定义一个静态变量,一个非静态变量。

    然后使用 @PostConstruct 注解。

    这个注解是 JavaEE 使用的,我们通过注解就知道,这个注解就是在构造方法被执行后下一个执行的方法。

    我们可以在这里对我们的静态变量初始化。

    1. @Component
    2. public class TestClass {
    3. private static AutowiredTypeComponent component;
    4. @Autowired
    5. private AutowiredTypeComponent autowiredComponent;
    6. @PostConstruct
    7. private void beforeInit() {
    8. component = this.autowiredComponent;
    9. }
    10. // 调用静态组件的方法
    11. public static void testMethod() {
    12. component.callTestMethod();
    13. }
    14. }

    使用 Spring 的工具类获取 Bean

    这个方法就是直接调用 Spring 的上下文工具来获得组件。

    1. AutowiredTypeComponent component = SpringApplicationContextUtil.getBean("component");
    2. component.callTestMethod();

    这个方法实在太麻烦,每一次要用的时候都要调用一次,增加不少容易的代码。

    总结

    这个问题就是 Spring 的 Bean 在什么时候初始化的问题。

    如果没有初始化的话,是没有办法直接调用和自动加载的。

    根据官方的提示,不要使用变量上的自动加载,使用构造方法的自动加载就可以了,这个也是官方推荐的方式。

    https://www.ossez.com/t/spring-autowired/14074icon-default.png?t=M7J4https://www.ossez.com/t/spring-autowired/14074

  • 相关阅读:
    logstash/filebeat只接收最近一段时间的数据
    AI智能伪原创工具:原创文章自动生成的革新
    Qt raise()问题
    web前端面试题附答案008-你项目中的TDK是怎么赋值的?(这道题很容易暴露项目经验)
    Hypervisor Platform无法执行xmm寄存器fpu指令sse解决方法
    常用的git命令(实用)
    R 和 Python用于统计学分析,哪个更好?
    【3D目标检测】PointPillars: Fast Encoders for Object Detection from Point Clouds
    windows c++ 输出所有服务列表,并判断是否是开机启动项
    用git上传文件 / 文件夹到GitHub仓库(完整步骤)
  • 原文地址:https://blog.csdn.net/huyuchengus/article/details/126562838