在Filter中使用 Service 查询数据
1、问题
@Autowired
private SysRoleService sysRoleService;
@Autowired
private SysUserTokenService tokenService;
类似于以上在 Filter 中使用 @Autowired ,进行注入,sysRoleService == null
2、Why?
web应用启动的顺序是:listener->filter->servlet
3、解决
ServletContext context = request.getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
// 获取 Service
ShiroService shiroService = ctx.getBean(ShiroService.class);
// 使用Service 查询数据
SysUserTokenEntity tokenEntity = shiroService.queryByToken(token);