一般我们在controller
层调用service
时,只需要使用@Autowired
注解即可,例如如下代码我们经常看到:
@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
中注入业务层类(BusinessService
)调用业务层方法queryYearList
。但是如果我们要在我们自己封装的Utils工具类中或者非controller
普通类中使用@Autowired
注解注入Service
或者Mapper
接口,直接注入是报错的,因为Utils
使用了静态的方法,我们是无法直接使用非静态接口的,当我们遇到这样的问题,我们就要想办法解决了。例如:
public class RedisHelper {
private static final Logger logger = LoggerFactory.getLogger(RedisHelper.class);
@Autowired
private static StringRedisTemplate redisTemplate;
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);
}
});
}
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
但是我们使用的时候会发现,这个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;
}
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);
}
});
}
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静态工具类注入service出现NullPointerException
异常处理的文章就介绍到这了,更多相关java静态类注入service
出现NullPointerException
处理内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!