请教怎么用Spring的注解方式把一个Bean注入到过滤器中

2024-11-28 20:27:09
推荐回答(1个)
回答1:

参考:
UsersConnectionRepository bean =
(UsersConnectionRepository)WebApplicationContextUtils.
getRequiredWebApplicationContext(filterConfig.getServletContext()).
getBean("usersConnectionRepository");

下面是优化的写法:

UsersConnectionRepository bean = WebApplicationContextUtils.
getRequiredWebApplicationContext(filterConfig.getServletContext()).
getBean(UsersConnectionRepository.class);