java 依赖注入 静态_Spring 依赖注入 static 静态变量相关问题

Spring 依赖注入 static 静态变量相关问题

1.Spring 不支持依赖注入 static 静态变量

在 springframework 里, 我们不能 @Autowired 一个静态变量, 使之成为一个 spring bean, 例如下面这样:

@Autowired

private static YourClass yourClass;

可以试一下, yourClass 在这种状态下不能够被依赖注入, 会抛出运行时异常 java.lang.NullPointerException, 为什么呢? 静态变量 / 类变量不是对象的属性, 而是一个类的属性, spring 则是基于对象层面上的依赖注入.

而使用静态变量 / 类变量扩大了静态方法的使用范围. 静态方法在 spring 是不推荐使用的. 依赖注入的主要目的, 是让容器去产生一个对象的实例, 然后在整个生命周期中使用他们, 同时也让 testing 工作更加容易.

一旦你使用静态方法, 就不再需要去产生这个类的实例, 这会让 testing 变得更加困难, 同时你也不能为一个给定的类, 依靠注入方式去产生多个具有不同的依赖环境的实例. 这种 static field 是隐含共享的, 并且是一种 global 全局状态, spring 同样不推荐这样去做.

2.Spring 如何给静态变量注入值

spring 不允许 / 不支持把值注入到静态变量中,如:

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

@Component

public class GlobalValue {

@Value("${mongodb.db}")

public static String DATABASE;

}

如果你获取 GlobalValue.DATABASE,会得到 null

GlobalValue.DATABASE = null

那我们如何解决这个问题呢。

好在 spring 支持 set 方法注入,我们可以利用非静态 setter 方法注入静态变量。如:

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

@Component

public class GlobalValue {

public static String DATABASE;

@Value("${mongodb.db}")

public void setDatabase(String db) {

DATABASE = db;

}

}

输出:

GlobalValue.DATABASE = "mongodb database name"

3.Spring 静态注入的三种方式

Spring 静态注入的三种方式:

(说明:MongoFileOperationUtil 是自己封装的一个 Mongodb 文件读写工具类,里面需要依赖 AdvancedDatastore 对象实例,dsForRW 用来获取 Mongodb 数据源)

在 springframework 里,我们不能 @Autowired 一个静态变量, 使之成为一个 spring bean,例如下面这种方式:

@Autowired

private static AdvancedDatastore dsForRW;

可以试一下,dsForRW 在这种状态下不能够被依赖注入,会抛出运行时异常 java.lang.NullPointerException,为什么呢? 静态变量 / 类变量不是对象的属性, 而是一个类的属性, spring 则是基于对象层面上的依赖注入。

但是自己比较喜欢封装工具类,并通过 @Component 注解成功能组件,但是功能组件中的方法一般都是静态方法,静态方法只能调用静态成员变量,于是就有了下面的问题。封有的时候封装功能组件会需要底层的 service 注入,怎么办呢?

去网上搜了下解决办法,简单总结一下几种实现方式;

1.xml 方式实现:

public class MongoFileOperationUtil {

private static AdvancedDatastore dsForRW;

private static MongoFileOperationUtil mongoFileOperationUtil;

public void init() {

mongoFileOperationUtil = this;

mongoFileOperationUtil.dsForRW = this.dsForRW;

}

}

这种方式适合基于 XML 配置的 WEB 项目;

2.@PostConstruct 方式实现;

import org.mongodb.morphia.AdvancedDatastore;

import org.springframework.beans.factory.annotation.Autowired;

@Component

public class MongoFileOperationUtil {

@Autowired

private static AdvancedDatastore dsForRW;

private static MongoFileOperationUtil mongoFileOperationUtil;

@PostConstruct

public void init() {

mongoFileOperationUtil = this;

mongoFileOperationUtil.dsForRW = this.dsForRW;

}

}

@PostConstruct 注解的方法在加载类的构造函数之后执行,也就是在加载了构造函数之后,执行 init 方法;(@PreDestroy 注解定义容器销毁之前的所做的操作)

这种方式和在 xml 中配置 init-method 和 destory-method 方法差不多,定义 spring 容器在初始化 bean 和容器销毁之前的所做的操作;

3.set 方法上添加 @Autowired 注解,类定义上添加 @Component 注解;

import org.mongodb.morphia.AdvancedDatastore;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

@Component

public class MongoFileOperationUtil {

private static AdvancedDatastore dsForRW;

@Autowired

public void setDatastore(AdvancedDatastore dsForRW) {

MongoFileOperationUtil.dsForRW = dsForRW;

}

}

首先 Spring 要能扫描到 AdvancedDatastore 的 bean,然后通过 setter 方法注入;

然后注意:成员变量上不需要再添加 @Autowired 注解;

本文借鉴多篇博客:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值