您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何解決Spring對靜態變量無法注入的問題”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何解決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(); } }
在類前加@Component注解,在set方法上加 @Autowired注解,這里注意兩點
1.配置文件里已經配置了變量的相關參數
2.靜態變量自動生成set方法時會有static修飾,要去掉,否則還是無法注入
@Component public class JDBCUtils { private static ComboPooledDataSource dataSource; @Autowired public void setDataSource(ComboPooledDataSource dataSource) { JDBCUtils.dataSource = dataSource; }
同樣注意將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加在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>
今天在寫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; } }
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; /** * Spring Context工具類. * * @author:Hohn */ @Component @Scope("singleton") public class SpringUtil implements ApplicationContextAware { /** * Spring應用上下文環境. */ private static ApplicationContext applicationContext; /** * 實現ApplicationContextAware接口的回調方法,設置上下文環境 * * <br>?param: applicationContext * @throws BeansException */ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringUtil.applicationContext = applicationContext; } /** * 獲取ApplicationContext. * * <br>?return: ApplicationContext */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 獲取對象. * * <br>?param: name * <br>?return: Object 一個以所給名字注冊的bean的實例 * @throws BeansException */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) throws BeansException { return (T) applicationContext.getBean(name); } /** * 獲取類型為requiredType的對象. * * <br>?param: clz * <br>?return: * @throws BeansException */ public static <T> T getBean(Class<T> clz) throws BeansException { return (T)applicationContext.getBean(clz); } /** * 如果BeanFactory包含一個與所給名稱匹配的bean定義,則返回true * * <br>?param: name * <br>?return: boolean */ public static boolean containsBean(String name) { return applicationContext.containsBean(name); } /** * 判斷以給定名字注冊的bean定義是一個singleton還是一個prototype。 * 如果與給定名字相應的bean定義沒有被找到,將會拋出一個異常(NoSuchBeanDefinitionException) * <br>?param: name * <br>?return: boolean * @throws NoSuchBeanDefinitionException */ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return applicationContext.isSingleton(name); } /** * <br>?param: name * <br>?return: Class 注冊對象的類型 * @throws NoSuchBeanDefinitionException */ public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { return applicationContext.getType(name); } /** * 如果給定的bean名字在bean定義中有別名,則返回這些別名 * * <br>?param: name * <br>?return: * @throws NoSuchBeanDefinitionException */ public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { return applicationContext.getAliases(name); } /** * 請求頭獲取請求token * @param servletRequest * @return */ public static String getJwtToken(HttpServletRequest servletRequest, String tokenId) { String token = servletRequest.getHeader(tokenId); if (StringUtils.isBlank(token)) { token = servletRequest.getParameter(tokenId); } return token; } }
感謝各位的閱讀,以上就是“如何解決Spring對靜態變量無法注入的問題”的內容了,經過本文的學習后,相信大家對如何解決Spring對靜態變量無法注入的問題這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。