3
votes

I have a class which implements ContainerRequestFilter, i want to inject some spring dependencies into it so i need to make Spring aware of the Jersey filter. The filter itself is configured in my web.xml with the Jersey servlet

<servlet>
    <servlet-name>Jersey Spring Web Application</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
        <param-value>com.MyFilter</param-value>
    </init-param>
</servlet>

The filter class then attempts to inject the Manager bean. In order to make spring aware of the Filter bean i have defined the bean in my applicationContext.xml and included.

@Component
public class MyFilter implements ContainerRequestFilter {

@Autowired
private Manager manager;

I've attempted to make the filter bean visible by forcing Spring to use proxy generated classes however this is not working

<mvc:annotation-driven />

<aop:aspect-autoproxy />    

<bean id="filter" class="com.MyFilter">
    <property name="manager" ref="Manager" />
</bean>

Any suggestions on how i can edit existing code to allow the filter see spring beans?

2

2 Answers

7
votes

Since you are using Jersey's Spring Servlet, spring dependencies can be injected into the filter using the annotation @InjectParam

//@Component -> This is not required.
public class MyFilter implements ContainerRequestFilter {

@InjectParam
private Manager manager;

I feel this is the idiomatic way of using Jersey and Spring together.

4
votes

This is something that is quite common for Spring Security applications. You should utilize the DelegatingFilterProxy for this.

You will add this filter to your web.xml via the <filter> tag, and you then configure it as a bean in your application container. You can then use it to delegate to a Spring configured filter that you have in your application context.

I am unfamiliar with how Jersey's SpringServlet work, but that is how I would delegate to a Spring configured filter.