Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


NPI – Lightrun – Spring (partner)

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

1. Overview

In this quick tutorial, we're going to focus on the BeanNameAware and BeanFactoryAware interfaces, in the Spring Framework.

We'll describe each interface separately with the pros and cons of their usage.

2. Aware Interface

Both BeanNameAware and BeanFactoryAware belong to the org.springframework.beans.factory.Aware root marker interface. This uses setter injection to get an object during the application context startup.

The Aware interface is a mix of callback, listener, and observer design patterns. It indicates that the bean is eligible to be notified by the Spring container through the callback methods.

3. BeanNameAware

BeanNameAware makes the object aware of the bean name defined in the container.

Let's have a look at an example:

public class MyBeanName implements BeanNameAware {

    public void setBeanName(String beanName) {

The beanName property represents the bean id registered in the Spring container. In our implementation, we're simply displaying the bean name.

Next, let's register a bean of this type in a Spring configuration class:

public class Config {

    @Bean(name = "myCustomBeanName")
    public MyBeanName getMyBeanName() {
        return new MyBeanName();

Here we've explicitly assigned a name to our MyBeanName class with the @Bean(name = “myCustomBeanName”) line.

Now we can start the application context and get the bean from it:

AnnotationConfigApplicationContext context 
  = new AnnotationConfigApplicationContext(Config.class);

MyBeanName myBeanName = context.getBean(MyBeanName.class);

As we expect, the setBeanName method prints out “myCustomBeanName”.

If we remove the name = “…” code from the @Bean annotation the container, in this case, assigns getMyBeanName()  method name into the bean. So the output will be “getMyBeanName”.

4. BeanFactoryAware

BeanFactoryAware is used to inject the BeanFactory object. This way we get access to the BeanFactory which created the object.

Here's an example of a MyBeanFactory class:

public class MyBeanFactory implements BeanFactoryAware {

    private BeanFactory beanFactory;

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;

    public void getMyBeanName() {
        MyBeanName myBeanName = beanFactory.getBean(MyBeanName.class);

With the help of the setBeanFactory() method, we assign the BeanFactory reference from the IoC container to the beanFactory property.

After that, we can use it directly like in the getMyBeanName() function.

Let's initialize the MyBeanFactory and call the getMyBeanName() method:

MyBeanFactory myBeanFactory = context.getBean(MyBeanFactory.class);

As we have already instantiated the MyBeanName class in the previous example, Spring will invoke the existing instance here.

The beanFactory.isSingleton(“myCustomBeanName”) line verifies that.

5. When to Use?

The typical use case for BeanNameAware could be acquiring the bean name for logging or wiring purposes. For the BeanFactoryAware it could be the ability to use a spring bean from legacy code.

In most cases, we should avoid using any of the Aware interfaces, unless we need them. Implementing these interfaces will couple the code to the Spring framework.

6. Conclusion

In this write-up, we learned about the BeanNameAware and BeanFactoryAware interfaces and how to use them in practice.

As usual, the complete code for this article is available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


Generic footer banner
Comments are closed on this article!