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

来源:互联网 发布:响应式与js的区别 编辑:程序博客网 时间:2024/05/29 05:53

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

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

@Autowiredprivate 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方式实现:
<bean id="mongoFileOperationUtil" class="com.*.*.MongoFileOperationUtil" init-method="init">      <property name="dsForRW" ref="dsForRW"/>  </bean> 

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注解;

本文借鉴多篇博客:
http://blog.csdn.net/randyfeng007/article/details/45330793
http://blog.csdn.net/doctor_who2004/article/details/50532169
http://blog.csdn.net/chen1403876161/article/details/53644024

原创粉丝点击