我按着nutz.book的配置说明,从配置文件搭起redis.properties,redis.js,然后弄过滤器
1、弄过滤器
import org.nutz.aop.InterceptorChain;
import org.nutz.aop.MethodInterceptor;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@IocBean(name = "redis")
public class RedisInterceptor implements MethodInterceptor {
@Inject
JedisPool jedisPool;
static ThreadLocal<Jedis> TL = new ThreadLocal<Jedis>();
@Override
public void filter(InterceptorChain chain) throws Throwable {
if (TL.get() != null) {
chain.doChain();
return;
}
try (Jedis jedis = jedisPool.getResource()) {
TL.set(jedis);
chain.doChain();
} finally {
TL.remove();
}
}
public static Jedis jedis() {
return TL.get();
}
}
2、然后搞RedisService的ioc bean
3、最后其他service层引进的时候,
@Inject
private RedisService redisService;
redisService.get("XXXXXXX");
4、现在报空指针
@Aop("redis")
public void set(String key, String val) {
jedis().set(key, val);
}
jedis()这个对象为null