eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Summer Sale 2026 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

Course – Summer Sale 2026 – NPI (cat=Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

1. Introduction

Servlet filters offer a powerful mechanism for intercepting and manipulating incoming requests. However, accessing Spring-managed beans within these filters can pose a challenge.

In this tutorial, we’ll explore various approaches to seamlessly obtain Spring beans within a Servlet filter, which is a common requirement in Spring-based web applications.

2. Understanding the Limitations of @Autowired in Servlet Filter

While Spring’s dependency injection mechanism, @Autowired, is a convenient way to inject dependencies into Spring-managed components, it doesn’t work seamlessly with Servlet filters. This is because Servlet filters are initialized by the Servlet container, typically before Spring’s ApplicationContext is fully loaded and initialized.

As a result, when the container instantiates a Servlet filter, the Spring context may not yet be available, leading to null or uninitialized dependencies when attempting to use @Autowired annotations. Let’s explore alternative approaches for accessing Spring beans within Servlet filters.

3. Setup

Let’s create a common LoggingService that will be autowired into our filter:

@Service
public class LoggingService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    public void log(String message,String url){
        logger.info("Logging Request {} for URI : {}",message,url);
    }
}

We’ll then create our filter, which will intercept incoming HTTP requests to log the HTTP method and URI details using the LoggingService dependency:

@Component
public class LoggingFilter implements Filter {
    @Autowired
    LoggingService loggingService;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) 
      throws IOException, ServletException {
        HttpServletRequest httpServletRequest=(HttpServletRequest)servletRequest;
        loggingService.log(httpServletRequest.getMethod(),httpServletRequest.getRequestURI());
        filterChain.doFilter(servletRequest,servletResponse);
    }
}

Let’s expose a RestController that returns a list of users:

@RestController
public class UserController {
    @GetMapping("/users")
    public List<User> getUsers(){
        return Arrays.asList(new User("1","John","[email protected]"),
          new User("2","Smith","[email protected]"));
    }
}

We’ll set up our test to check if our LoggingService was autowired successfully into our filter:

@RunWith(SpringRunner.class)
@SpringBootTest
public class LoggingFilterTest {
    @Autowired
    private LoggingFilter loggingFilter;

    @Test
    public void givenFilter_whenAutowired_thenDependencyInjected() throws Exception {
        Assert.assertNotNull(loggingFilter);
        Assert.assertNotNull(getField(loggingFilter,"loggingService"));
    }

    private Object getField(Object target, String fieldName) throws NoSuchFieldException, IllegalAccessException {
        Field field = target.getClass().getDeclaredField(fieldName);
        field.setAccessible(true);
        return field.get(target);
    }
}

However, at this stage, it’s possible that the LoggingService won’t be injected into the LoggingFilter because the Spring context wasn’t available yet. We’ll explore the various options for resolving this issue in the following sections.

4. Using SpringBeanAutowiringSupport in Servlet Filter

Spring’s SpringBeanAutowiringSupport class provides support for dependency injection into non-Spring-managed classes such as Filters and Servlets. By using this class, Spring can inject dependencies, such as the LoggingService, which is a Spring-managed bean, into the LoggingFilter.

The init method is used to initialize a Filter instance, and we’ll override this method in the LoggingFilter to use SpringBeanAutowiringSupport:

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
      filterConfig.getServletContext());
}

The processInjectionBasedOnServletContext method uses the ApplicationContext associated with the ServletContext to perform the autowiring. It first retrieves the ApplicationContext from the ServletContext and then uses it to autowire the dependencies into the target object. This process involves inspecting the target object’s fields for @Autowired annotations and then resolving and injecting the corresponding beans from the ApplicationContext.

This mechanism allows non-Spring-managed objects, like filters and servlets, to benefit from Spring’s dependency injection capabilities.

5. Using WebApplicationContextUtils in Servlet Filter

WebApplicationContextUtils provides a utility method that retrieves the ApplicationContext associated with the ServletContext. The ApplicationContext contains all the beans managed by the Spring container.

Let’s override the init method of the LoggingFilter class:

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    loggingService = WebApplicationContextUtils
      .getRequiredWebApplicationContext(filterConfig.getServletContext())
      .getBean(LoggingService.class);
}

We retrieve an instance of LoggingService from the ApplicationContext and assign it to the loggingService field of the filter. This approach is useful when we need to access Spring-managed beans in a non-Spring-managed component, such as a Servlet or a Filter, and we cannot use annotation-based or constructor injection.

It should be noted that this approach tightly couples the filter with Spring, which may not be ideal in some cases.

6. Using FilterRegistrationBean in Configuration

FilterRegistrationBean is used to register Servlet filters in the servlet container programmatically. It provides a way to configure the filter registration dynamically in the application’s configuration classes.

By annotating the method with @Bean, the LoggingService is automatically injected into the method, allowing it to be passed to the LoggingFilter constructor. Let’s set up the method for FilterRegistrationBean in our config class:

@Bean
public FilterRegistrationBean<LoggingFilter> loggingFilterRegistration(LoggingService loggingService) {
    FilterRegistrationBean<LoggingFilter> registrationBean = new FilterRegistrationBean<>();
    registrationBean.setFilter(new LoggingFilter(loggingService));
    registrationBean.addUrlPatterns("/*");
    return registrationBean;
}

We’ll then include a constructor in our LoggingFilter to support the above configuration:

public LoggingFilter(LoggingService loggingService) {
    this.loggingService = loggingService;
}

This approach centralizes the configuration of filters and their dependencies, making the code more organized and easier to maintain.

7. Using DelegatingFilterProxy in Servlet Filter

The DelegatingFilterProxy is a Servlet filter that allows passing control to Filter classes that have access to the Spring ApplicationContext.

Let’s configure the DelegatingFilterProxy to delegate to a Spring-managed bean named “loggingFilter”. FilterRegistrationBean is used by Spring to register the filter with the Servlet container when the application starts up:

@Bean
public FilterRegistrationBean<DelegatingFilterProxy> loggingFilterRegistration() {
    FilterRegistrationBean<DelegatingFilterProxy> registrationBean = new FilterRegistrationBean<>();
    registrationBean.setFilter(new DelegatingFilterProxy("loggingFilter"));
    registrationBean.addUrlPatterns("/*");
    return registrationBean;
}

Let’s use the same bean name for our filter defined earlier:

@Component("loggingFilter")
public class LoggingFilter implements Filter {
    // standard methods
}

This approach allows us to use Spring’s dependency injection to manage the loggingFilter bean.

8. Comparing Dependency Injection Approaches in Servlet Filter

The DelegatingFilterProxy approach differs from both SpringBeanAutowiringSupport and the direct use of WebApplicationContextUtils in how it delegates the filter’s execution to a Spring-managed bean, allowing us to use Spring’s dependency injection.

The DelegatingFilterProxy is better aligned with the typical Spring application architecture and allows for a cleaner separation of concerns. The FilterRegistrationBean approach allows more control over the dependency injection of the filter and centralizes the configuration of dependencies.

In contrast, SpringBeanAutowiringSupport and WebApplicationContextUtils are more low-level approaches that can be useful in certain scenarios where we need more control over the filter’s initialization process or want to access the ApplicationContext directly. However, they require more manual setup and do not provide the same level of integration with Spring’s dependency injection mechanism.

9. Conclusion

In this article, we’ve explored the different approaches to autowire Spring beans into Servlet Filters. Each approach has its advantages and limitations, and the choice of approach depends on the specific requirements and constraints of the application. Overall, they enable seamless integration of Spring-managed beans into Servlet filters, enhancing the flexibility and maintainability of applications.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Summer Sale 2026 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

Course – Summer Sale 2026 – NPI (All)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

eBook Jackson – NPI EA – 3 (cat = Jackson)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments