Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
When we call a service from the Controller layer, we simply use the @autowired annotation, as we often see in the following code:
@RestController
@RequestMapping("business")
public class BizResourceController {
@Autowired
private BusinessService businessService;
@RequestMapping(path = "/queryYearList", method = RequestMethod.POST)
public List<String> queryYearList(@RequestParam("cityCode") String cityCode) {
returnbusinessService.queryYearList(cityCode); }}Copy the code
The implication of the above code is to invoke the business layer method queryYearList by injecting BusinessService into the Controller. However, if we want to inject a Service or Mapper interface using the @AutoWired annotation in our own encapsulated Utils utility class or in a non-Controller ordinary class, direct injection is an error. Because Utils uses static methods, we cannot directly use non-static interfaces. When we meet a problem like this, we have to find a way to solve it. Such as:
public class RedisHelper {
private static final Logger logger = LoggerFactory.getLogger(RedisHelper.class);
@Autowired
private static StringRedisTemplate redisTemplate;
/** * scan implements **@paramPattern expression *@paramThe consumer operates on the iterated 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 newRuntimeException(e); }}); }/** * Get the key ** that meets the condition@paramPattern expression *@return* /
public static List<String> keys(String pattern) {
List<String> keys = new ArrayList<>();
scan(pattern, item -> {
// A qualified key
String key = new String(item, StandardCharsets.UTF_8);
keys.add(key);
});
return keys;
}
public static void delete(List<String> listKey) {
try {
logger.info("Need to delete key:" + listKey);
Long delete1 = redisTemplate.delete(listKey);
logger.info("Clear redis-key result: {}",delete1);
} catch(Exception e) { e.printStackTrace(); }}}Copy the code
The code above in the Redis utility class wants to inject StringRedisTemplate but when we use it we find that the StringRedisTemplate object is null. Therefore, when we need to have similar requirements for injection, we need to adjust the injection method and writing method, as follows:
@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 implements **@paramPattern expression *@paramThe consumer operates on the iterated 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 newRuntimeException(e); }}); }/** * Get the key ** that meets the condition@paramPattern expression *@return* /
public static List<String> keys(String pattern) {
List<String> keys = new ArrayList<>();
scan(pattern, item -> {
// A qualified key
String key = new String(item, StandardCharsets.UTF_8);
keys.add(key);
});
return keys;
}
public static void delete(List<String> listKey) {
try {
logger.info("Need to delete key:" + listKey);
Long delete1 = redisTemplate.delete(listKey);
logger.info("Clear redis-key result: {}",delete1);
} catch(Exception e) { e.printStackTrace(); }}}Copy the code
The core of its revision is:
Add the @Component annotation for Spring hosting, and inject the StringRedisTemplate as a set.