Код: Выделить всё
@Component
public class JedisConfig {
private Jedis jedis;
public JedisConfig() {
jedis = new Jedis("localhost", 6379);
}
public Jedis getJedis() {return jedis;}
}
Код: Выделить всё
public class AuthInterceptor implements HandlerInterceptor {
@Autowired
private JedisConfig jc;
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Jedis jedis = jc.getJedis();
// do some other stuff with jedis below
}
}
В соответствующее примечание: я сделал почти то же самое в модульном тесте, и он работает нормально:
Код: Выделить всё
@Autowired
private JedisConfig jc;
@Test
public void testJedis(){
Jedis jedis = jc.getJedis();
jedis.set("user", "role");
assertThat(jedis.get("user"),is("role"));
}
Код: Выделить всё
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AuthInterceptor())
.addPathPatterns("/user/roleChange");
}
}
Подробнее здесь: https://stackoverflow.com/questions/726 ... nterceptor