java靜態(tài)工具類注入service出現(xiàn)NullPointerException異常處理
一般我們在controller
層調(diào)用service
時(shí),只需要使用@Autowired
注解即可,例如如下代碼我們經(jīng)??吹剑?/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è)務(wù)層類(BusinessService
)調(diào)用業(yè)務(wù)層方法queryYearList
。但是如果我們要在我們自己封裝的Utils工具類中或者非controller
普通類中使用@Autowired
注解注入Service
或者Mapper
接口,直接注入是報(bào)錯(cuò)的,因?yàn)?code>Utils使用了靜態(tài)的方法,我們是無法直接使用非靜態(tài)接口的,當(dāng)我們遇到這樣的問題,我們就要想辦法解決了。例如:
public class RedisHelper { private static final Logger logger = LoggerFactory.getLogger(RedisHelper.class); @Autowired private static StringRedisTemplate redisTemplate; /** * scan 實(shí)現(xiàn) * * @param pattern 表達(dá)式 * @param consumer 對迭代到的key進(jìn)行操作 */ 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 表達(dá)式 * @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("需要?jiǎng)h除key:" + listKey); Long delete1 = redisTemplate.delete(listKey); logger.info("清除redis-key結(jié)果:{}",delete1); } catch (Exception e) { e.printStackTrace(); } } }
如上代碼在redis
工具類中想要注入StringRedisTemplate
但是我們使用的時(shí)候會發(fā)現(xiàn),這個(gè)StringRedisTemplate
對象時(shí)null。所以當(dāng)我們需要有類似需求進(jìn)行注入的時(shí)候要調(diào)整注入方式和寫法,如下代碼:
@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 實(shí)現(xiàn) * * @param pattern 表達(dá)式 * @param consumer 對迭代到的key進(jìn)行操作 */ 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 表達(dá)式 * @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("需要?jiǎng)h除key:" + listKey); Long delete1 = redisTemplate.delete(listKey); logger.info("清除redis-key結(jié)果:{}",delete1); } catch (Exception e) { e.printStackTrace(); } } }
其修改的核心是:
首先加@Component
注解目的是讓spring
托管,另外注入StringRedisTemplate
我們采用set方式進(jìn)行注入即可。
到此這篇關(guān)于java靜態(tài)工具類注入service出現(xiàn)NullPointerException
異常處理的文章就介紹到這了,更多相關(guān)java靜態(tài)類注入service
出現(xiàn)NullPointerException
處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決springcloud中Feign導(dǎo)入依賴為unknow的情況
這篇文章主要介紹了解決springcloud中Feign導(dǎo)入依賴為unknow的情況,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03springboot用thymeleaf模板的paginate分頁完整代碼
本文根據(jù)一個(gè)簡單的user表為例,展示 springboot集成mybatis,再到前端分頁完整代碼,需要的朋友可以參考下2017-07-07Spring使用注解方式實(shí)現(xiàn)創(chuàng)建對象
這篇文章主要介紹了Spring使用注解方式實(shí)現(xiàn)創(chuàng)建對象,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2023-02-02在Java8與Java7中HashMap源碼實(shí)現(xiàn)的對比
這篇文章主要介紹了在Java8與Java7中HashMap源碼實(shí)現(xiàn)的對比,內(nèi)容包括HashMap 的原理簡單介紹、結(jié)合源碼在Java7中是如何解決hash沖突的以及優(yōu)缺點(diǎn),結(jié)合源碼以及在Java8中如何解決hash沖突,balance tree相關(guān)源碼介紹,需要的朋友可以參考借鑒。2017-01-01SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)動(dòng)態(tài)權(quán)限問題
這篇文章主要介紹了SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)-動(dòng)態(tài)權(quán)限,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06Java利用位運(yùn)算實(shí)現(xiàn)加減乘除的方法詳解
我們經(jīng)常使用的加減乘除,我們所看到的只是表面的效果,那么加減乘除在底層究竟是怎么實(shí)現(xiàn)的?今天就讓我們一探究竟2022-08-08