java靜態(tài)工具類注入service出現(xiàn)NullPointerException異常處理
一般我們在controller
層調用service
時,只需要使用@Autowired
注解即可,例如如下代碼我們經??吹剑?/strong>
@RestController @RequestMapping("business") public class BizResourceController { @Autowired private BusinessService businessService; @RequestMapping(path = "/queryYearList", method = RequestMethod.POST) public List<String> queryYearList(@RequestParam("cityCode") String cityCode) { return businessService.queryYearList(cityCode); } }
以上代碼的含義就是通過在controller
中注入業(yè)務層類(BusinessService
)調用業(yè)務層方法queryYearList
。但是如果我們要在我們自己封裝的Utils工具類中或者非controller
普通類中使用@Autowired
注解注入Service
或者Mapper
接口,直接注入是報錯的,因為Utils
使用了靜態(tài)的方法,我們是無法直接使用非靜態(tài)接口的,當我們遇到這樣的問題,我們就要想辦法解決了。例如:
public class RedisHelper { private static final Logger logger = LoggerFactory.getLogger(RedisHelper.class); @Autowired private static StringRedisTemplate redisTemplate; /** * scan 實現(xiàn) * * @param pattern 表達式 * @param consumer 對迭代到的key進行操作 */ public static void scan(String pattern, Consumer<byte[]> consumer) { redisTemplate.execute((RedisConnection connection) -> { try (Cursor<byte[]> cursor = connection.scan(ScanOptions.scanOptions().count(Long.MAX_VALUE).match(pattern).build())) { cursor.forEachRemaining(consumer); return null; } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } }); } /** * 獲取符合條件的key * * @param pattern 表達式 * @return */ public static List<String> keys(String pattern) { List<String> keys = new ArrayList<>(); scan(pattern, item -> { //符合條件的key String key = new String(item, StandardCharsets.UTF_8); keys.add(key); }); return keys; } public static void delete(List<String> listKey) { try { logger.info("需要刪除key:" + listKey); Long delete1 = redisTemplate.delete(listKey); logger.info("清除redis-key結果:{}",delete1); } catch (Exception e) { e.printStackTrace(); } } }
如上代碼在redis
工具類中想要注入StringRedisTemplate
但是我們使用的時候會發(fā)現(xiàn),這個StringRedisTemplate
對象時null。所以當我們需要有類似需求進行注入的時候要調整注入方式和寫法,如下代碼:
@Component public class RedisHelper { private static final Logger logger = LoggerFactory.getLogger(RedisHelper.class); private static StringRedisTemplate redisTemplate; @Autowired public void setRedisTemplate(StringRedisTemplate redisTemplate) { RedisHelper.redisTemplate = redisTemplate; } /** * scan 實現(xiàn) * * @param pattern 表達式 * @param consumer 對迭代到的key進行操作 */ public static void scan(String pattern, Consumer<byte[]> consumer) { redisTemplate.execute((RedisConnection connection) -> { try (Cursor<byte[]> cursor = connection.scan(ScanOptions.scanOptions().count(Long.MAX_VALUE).match(pattern).build())) { cursor.forEachRemaining(consumer); return null; } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } }); } /** * 獲取符合條件的key * * @param pattern 表達式 * @return */ public static List<String> keys(String pattern) { List<String> keys = new ArrayList<>(); scan(pattern, item -> { //符合條件的key String key = new String(item, StandardCharsets.UTF_8); keys.add(key); }); return keys; } public static void delete(List<String> listKey) { try { logger.info("需要刪除key:" + listKey); Long delete1 = redisTemplate.delete(listKey); logger.info("清除redis-key結果:{}",delete1); } catch (Exception e) { e.printStackTrace(); } } }
其修改的核心是:
首先加@Component
注解目的是讓spring
托管,另外注入StringRedisTemplate
我們采用set方式進行注入即可。
到此這篇關于java靜態(tài)工具類注入service出現(xiàn)NullPointerException
異常處理的文章就介紹到這了,更多相關java靜態(tài)類注入service
出現(xiàn)NullPointerException
處理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決springcloud中Feign導入依賴為unknow的情況
這篇文章主要介紹了解決springcloud中Feign導入依賴為unknow的情況,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03springboot用thymeleaf模板的paginate分頁完整代碼
本文根據(jù)一個簡單的user表為例,展示 springboot集成mybatis,再到前端分頁完整代碼,需要的朋友可以參考下2017-07-07Spring使用注解方式實現(xiàn)創(chuàng)建對象
這篇文章主要介紹了Spring使用注解方式實現(xiàn)創(chuàng)建對象,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2023-02-02在Java8與Java7中HashMap源碼實現(xiàn)的對比
這篇文章主要介紹了在Java8與Java7中HashMap源碼實現(xiàn)的對比,內容包括HashMap 的原理簡單介紹、結合源碼在Java7中是如何解決hash沖突的以及優(yōu)缺點,結合源碼以及在Java8中如何解決hash沖突,balance tree相關源碼介紹,需要的朋友可以參考借鑒。2017-01-01SpringBoot整合SpringSecurityOauth2實現(xiàn)鑒權動態(tài)權限問題
這篇文章主要介紹了SpringBoot整合SpringSecurityOauth2實現(xiàn)鑒權-動態(tài)權限,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06