There’s a convenient utility class SpringBeanAutowiringSupport introduced since 2.5.1 that can be used to autowire self-instantiated classes, it contains two static methods which I’ll explain next.


This method is tailored to Spring-based web application, does not work well with SBSA, because it use ContextLoader to locate the current WebApplicationContext, but SBSA use a custom context loader.


This one requires that we have explicitly reference to the current servlet context, we can use ServletContextInitializer to get the reference to the servlet context, as demonstrated in the following snippet:

public class Application implements ServletContextInitializer {
    private static ServletContext servletContext;
    public static void main(String[] args) {, args);
    public void onStartup(ServletContext servletContext) throws ServletException {
        // save reference to our servlet context, 
        // so later we can retrieve Spring ApplicationContext from it.
        Application.servletContext = servletContext;
    public static ServletContext getServletContext() {
        return Application.servletContext;

Now, we have access to the servlet context, let us autowire our dummy class.

class Example {
    private Service service;
    Example() {

We’re done, I hope you find it helpful!