Skip to content
Advertisement

How can I get a Spring bean in a servlet filter?

I have defined a javax.servlet.Filter and I have Java class with Spring annotations.

JavaScript

I want to get the bean UsersConnectionRepository in my Filter, so I tried the following:

JavaScript

But it always returns null. How can I get a Spring bean in a Filter?

Advertisement

Answer

Try:

JavaScript

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

JavaScript

Also have a look at GenericFilterBean and its subclasses.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement