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 – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

Self-injection means that a Spring bean injects itself as a dependency. It uses the Spring container to obtain a reference to itself and then uses that reference to perform some operation.

In this short tutorial, we’ll see how to use self-injection in Spring.

2. Use Case for Self-Injection

The most common use case for self-injection is to bypass Spring AOP limitations when it’s necessary to apply an aspect to a method or class that is self-referencing.

Suppose we have a service class that performs some business logic and needs to call a method on itself as part of that logic:

@Service
public class MyService {
    public void doSomething() {
        // ...
        doSomethingElse();
    }

    @Transactional
    public void doSomethingElse() {
        // ...
    }
}

However, when we run our application, we might notice that @Transactional isn’t applied. This is because the doSomething() method is calling doSomethingElse() directly, which bypasses the Spring proxy.

To work around this, we can use self-injection to get a reference to the Spring proxy and call the method through that proxy.

3. Self-Injection Using @Autowired

We can perform self-injection in Spring by using the @Autowired annotation on a field, constructor parameter, or setter method of a bean.

Here’s an example of using @Autowired with a field:

@Component
public class MyBean {
    @Autowired
    private MyBean self;

    public void doSomething() {
        // use self reference here
    }
}

And with a constructor parameter:

@Component
public class MyBean {
    private MyBean self;

    @Autowired
    public MyBean(MyBean self) {
        this.self = self;
    }

    // ...
}

4. Self-Injection Using ApplicationContextAware

Another way to perform self-injection is by implementing the ApplicationContextAware interface. This interface allows a bean to be aware of the Spring application context and obtain a reference to itself:

@Component
public class MyBean implements ApplicationContextAware {
    private ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        this.context = context;
    }

    public void doSomething() {
        MyBean self = context.getBean(MyBean.class);
        // ...
    }
}

5. Disadvantages

When a bean injects itself, it can create confusion about the responsibilities of the bean and make it harder to trace the flow of data through the application.

Self-injection can also create a circular dependency. Starting with version 2.6, Spring Boot will raise an exception if our project has circular dependencies. There are some workarounds, of course.

6. Conclusion

In this quick article, we’ve learned several ways we can use self-injection in Spring and when to use it. We’ve also learned some of the disadvantages of self-injection in Spring.

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 – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW