首页 > 代码库 > How to get a Spring bean in a Servlet filter?

How to get a Spring bean in a Servlet filter?

Try:

 

  1. UsersConnectionRepository bean =   (UsersConnectionRepository)WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig.getServletContext()).getBean("usersConnectionRepository");  

 

Where usersConnectionRepository is a name/id of your bean in the application context. Or even better:

 

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


Also have a look at GenericFilterBean


How to get a Spring bean in a Servlet filter?