Spring对静态变量无法注入
问题
今天在学习的过程中想写一个连接和线程绑定的JDBCUtils工具类,但测试时发现一直报空指针异常,上网查了之后Spring并不支持对静态成员变量注入,所以光试用@Autowired肯定是不行的。
可是我们编写工具类时肯定是要使用静态变量和方法的,我总结一下我用过可以实现对静态成员变量注入的方法。
@Component
public class JDBCUtils {
@Autowired
private static ComboPooledDataSource dataSource;
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
public static Connection getThreadConnection(){
Connection conn = tl.get();
if (conn == null){
conn = getConnection();
tl.set(conn);
}
return conn;
}
public static DataSource getDataSource(){
return dataSource;
}
public static Connection getConnection(){
Connection connection = null;
try {
connection = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void removeThreadConnection(){
tl.remove();
}
}
set方法注入
注解方式
在类前加@Component注解,在set方法上加 @Autowired注解,这里注意两点
1.配置文件里已经配置了变量的相关参数
2.静态变量自动生成set方法时会有static修饰,要去掉,否则还是无法注入
@Component
public class JDBCUtils {
private static ComboPooledDataSource dataSource;
@Autowired
public void setDataSource(ComboPooledDataSource dataSource) {
JDBCUtils.dataSource = dataSource;
}
xml方式
同样注意将set方法上的static去掉
public class JDBCUtils {
private static ComboPooledDataSource dataSource;
public void setDataSource(ComboPooledDataSource dataSource) {
this.dataSource = dataSource;
}
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
public static Connection getThreadConnection(){
Connection conn = tl.get();
if (conn == null){
conn = getConnection();
tl.set(conn);
}
return conn;
}
public static DataSource getDataSource(){
return dataSource;
}
public static Connection getConnection(){
Connection connection = null;
try {
connection = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void removeThreadConnection(){
tl.remove();
}
}
<bean id="JDBCUtils" class="com.cc.utils.JDBCUtils">
<property name="dataSource" ref="dataSource"></property>
</bean>
@PostConstruct注解方式注入
用@PostConstruct加在init方法上,在类初始化后执行该方法,对成员变量赋值。在这之前,我们要改造一下工具类,去掉我们想注入变量的static的修饰符,这样我们就可以用@Autowired实现对其注入。
然后加一个静态的类自身的引用对象,当我们想要变量时通过这个引用对象来获取。
@Component
public class JDBCUtils {
@Autowired
private ComboPooledDataSource dataSource;
private static JDBCUtils jdbcUtils;
@PostConstruct
public void init(){
jdbcUtils = this;
this.dataSource = dataSource;
}
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
public static Connection getThreadConnection(){
Connection conn = tl.get();
if (conn == null){
conn = getConnection();
tl.set(conn);
}
return conn;
}
public static DataSource getDataSource(){
return jdbcUtils.dataSource;
}
public static Connection getConnection(){
Connection connection = null;
try {
connection = jdbcUtils.dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void removeThreadConnection(){
tl.remove();
}
}
当然这种用初始化方法也可以用xml配置,原理一样。
public class JDBCUtils {
private ComboPooledDataSource dataSource;
public void setDataSource(ComboPooledDataSource dataSource) {
this.dataSource = dataSource;
}
private static JDBCUtils jdbcUtils;
public void init(){
jdbcUtils = this;
this.dataSource = dataSource;
}
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
public static Connection getThreadConnection(){
Connection conn = tl.get();
if (conn == null){
conn = getConnection();
tl.set(conn);
}
return conn;
}
public static DataSource getDataSource(){
return jdbcUtils.dataSource;
}
public static Connection getConnection(){
Connection connection = null;
try {
connection = jdbcUtils.dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void removeThreadConnection(){
tl.remove();
}
}
<bean id="JDBCUtils" class="com.cc.utils.JDBCUtils" init-method="init">
<property name="dataSource" ref="dataSource"></property>
</bean>
静态方法注入bean失败原因
今天在写redission 的一个工具类的时候,随手写出下面的代码
package com.wt.redission.wtredission.utils;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class RedissionUtilserror {
@Autowired
private static RedissonClient redissonClient;
public static RLock getRLock(String objectName) {
RLock rLock =redissonClient.getLock(objectName);
return rLock;
}
//根据名字获取map
public static <K, V> RMap<K, V> getRMap(String objectName) {
RMap<K, V> map = redissonClient.getMap(objectName);
return map;
}
//根据名字和值设置map
public static void setMap(String objectName,Object key,Object value){
RMap<Object, Object> map =redissonClient.getMap(objectName);
map.put(key,value);
}
//根据名字获取set
public static <V> RSet<V> getSet(String objectName) {
RSet<V> set = redissonClient.getSet(objectName);
return set;
}
//根据名字和值设置set
public static void setSet(String objectName,Object value){
RSet<Object> set = redissonClient.getSet(objectName);
set.add(value);
}
//根据名字获取list
public static <V> RList<V> getRList(String objectName) {
RList<V> rList = redissonClient.getList(objectName);
return rList;
}
//根据名字和值设置list
public static void setList(String objectName, int index,Object element ){
RList<Object> objectRList = redissonClient.getList(objectName);
objectRList.set(index,element);
}
//根据名字获取bucket
public static <T> RBucket<T> getRBucket(String objectName) {
RBucket<T> bucket = redissonClient.getBucket(objectName);
return bucket;
}
//根据名字和值 设置对应的bucket
public static <T> T setBucket(String objectName,String value){
RBucket<Object> bucket = redissonClient.getBucket(objectName);
bucket.set(value);
T t= (T) bucket.get(); //值类型由返回值确定
return t;
}
}
乍一看好像没问题 我写一个静态方法 然后在方法中使用静态变量redissonClient ,哇....,一切看得如此正常
当我开始测试时,NPE.............,我去这是怎么回事,自己在想这不科学啊,怎么会空指针,于是我开始找原因
最后发现是基础不牢啊............,对jvm的类加载机制几乎就没考虑,简要说要错误的原因
jvm在进行类加载的时候,首先会加载类变量,类方法,也就是我这里被static修饰的方法,然后当我调用静态方法进行使用的时候,会使用到redissionClient,注意这个redissionClient是通过autowired进来的,关键问题就在这里,autowired的底层是通过构造器和set方法注入bean的
redissionClient被static修饰 并且还是一个接口 在被调用的时候肯定没有实例化
下面提供三种方式正确使用
方式一
package com.wt.redission.wtredission.utils;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class RedissionUtils {
private static RedissonClient redissonClient;
@Autowired
public RedissionUtils(RedissonClient redissonClient){
RedissionUtils.redissonClient=redissonClient;
}
public static RLock getRLock(String objectName) {
RLock rLock = redissonClient.getLock(objectName);
return rLock;
}
//根据名字获取map
public static <K, V> RMap<K, V> getRMap(String objectName) {
RMap<K, V> map = redissonClient.getMap(objectName);
return map;
}
//根据名字和值设置map
public static void setMap(String objectName,Object key,Object value){
RMap<Object, Object> map =redissonClient.getMap(objectName);
map.put(key,value);
}
//根据名字获取set
public static <V> RSet<V> getSet(String objectName) {
RSet<V> set = redissonClient.getSet(objectName);
return set;
}
//根据名字和值设置set
public static void setSet(String objectName,Object value){
RSet<Object> set = redissonClient.getSet(objectName);
set.add(value);
}
//根据名字获取list
public static <V> RList<V> getRList(String objectName) {
RList<V> rList = redissonClient.getList(objectName);
return rList;
}
//根据名字和值设置list
public static void setList(String objectName, int index,Object element ){
RList<Object> objectRList = redissonClient.getList(objectName);
objectRList.set(index,element);
}
//根据名字获取bucket
public static <T> RBucket<T> getRBucket(String objectName) {
RBucket<T> bucket = redissonClient.getBucket(objectName);
return bucket;
}
//根据名字和值 设置对应的bucket
public static <T> T setBucket(String objectName,String value){
RBucket<Object> bucket = redissonClient.getBucket(objectName);
bucket.set(value);
T t= (T) bucket.get(); //值类型由返回值确定
return t;
}
}
方式二
package com.wt.redission.wtredission.utils;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class RedissionUtils2 {
@Autowired
RedissonClient redissonClient;
public static RedissionUtils2 redissionUtils;
@PostConstruct
public void init(){
redissionUtils=this;
redissionUtils.redissonClient=this.redissonClient;
}
public static RLock getRLock(String objectName) {
RLock rLock = redissionUtils.redissonClient.getLock(objectName);
return rLock;
}
//根据名字获取map
public static <K, V> RMap<K, V> getRMap(String objectName) {
RMap<K, V> map = redissionUtils.redissonClient.getMap(objectName);
return map;
}
//根据名字和值设置map
public static void setMap(String objectName,Object key,Object value){
RMap<Object, Object> map =redissionUtils.redissonClient.getMap(objectName);
map.put(key,value);
}
//根据名字获取set
public static <V> RSet<V> getSet(String objectName) {
RSet<V> set = redissionUtils.redissonClient.getSet(objectName);
return set;
}
//根据名字和值设置set
public static void setSet(String objectName,Object value){
RSet<Object> set = redissionUtils.redissonClient.getSet(objectName);
set.add(value);
}
//根据名字获取list
public static <V> RList<V> getRList(String objectName) {
RList<V> rList = redissionUtils.redissonClient.getList(objectName);
return rList;
}
//根据名字和值设置list
public static void setList(String objectName, int index,Object element ){
RList<Object> objectRList = redissionUtils.redissonClient.getList(objectName);
objectRList.set(index,element);
}
//根据名字获取bucket
public static <T> RBucket<T> getRBucket(String objectName) {
RBucket<T> bucket = redissionUtils.redissonClient.getBucket(objectName);
return bucket;
}
//根据名字和值 设置对应的bucket
public static <T> T setBucket(String objectName,String value){
RBucket<Object> bucket = redissionUtils.redissonClient.getBucket(objectName);
bucket.set(value);
T t= (T) bucket.get(); //值类型由返回值确定
return t;
}
}
方式三 通过spring上下文获取
package com.wt.redission.wtredission.utils;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@Component
@Scope("singleton")
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) throws BeansException {
return (T) applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> clz) throws BeansException {
return (T)applicationContext.getBean(clz);
}
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
}
public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getType(name);
}
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getAliases(name);
}
public static String getJwtToken(HttpServletRequest servletRequest, String tokenId) {
String token = servletRequest.getHeader(tokenId);
if (StringUtils.isBlank(token)) {
token = servletRequest.getParameter(tokenId);
}
return token;
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。