Spring Framework Injecting Inner Beans

Spring Framework Injecting Inner Beans

In this article, I am going to discuss Spring Framework Injecting Inner Beans with Examples. Please read our previous article, where we discussed Spring Framework Setter Dependency Injection.

What are Inner Beans?

In the Spring Framework, inner beans are a concept used to define and declare a bean within the scope of another bean. Instead of defining a bean in a separate XML or Java configuration file, you can define it directly within the context of another bean. Inner beans are useful in situations where a bean is used only within the context of another bean and does not need to be accessed independently.

One of the primary advantages of using inner beans is encapsulation. By defining a bean within another bean, you limit its visibility and scope to only the enclosing bean. This helps to keep the configuration concise and organized, as the inner bean is closely tied to the outer bean and its purpose. Inner beans also promote better modularization and reduce clutter in the overall configuration.

Implementing Inner Beans Injection in Spring Framework

Before starting the project, ensure that the required JAR files are in the project’s “Referenced Libraries” folder.

Implementing Inner Beans Injection in Spring Framework

In this article, we will be building upon the project from the previous article (‘Spring Framework Setter Dependency Injection’).

Kettle.java
public class Kettle
{
    private Drink drink;

    public void pour()
    {System.out.println("Pouring " + drink.getName() + " into cups.");}

    public void setDrink(Drink drink)
    {
        System.out.println("Pouring drink into kettle: " + drink.getName());
        this.drink = drink;
    }
}

This class represents a POJO class which will be used to represent a kettle.

Drink.java
public class Drink
{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

This class represents a POJO class which will be used to represent a drink. Note that the setters and getters can be automatically added using the “Source Action…” option in the right-click menu of VS Code.

App.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
    public static void main(String[] args)
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        Kettle k = (Kettle) context.getBean("kettle");
        k.pour();
    }
}

The main function creates a student object and a grade object from the beans.xml file. It then prints out the objects to the terminal.

Step 1: Modify Beans.xml as follows:

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id = "kettle" class = "Kettle">
        <property name = "drink">
          <bean id = "drink" class = "Drink">
               <property name = "name" value = "tea"/>
          </bean>
        </property>
   </bean>
</beans>

We have added the bean (which was previously created separately) into the main bean. This means that only one bean will be created.

Step 2: Compile and execute the application. Ensure compilation is successful. Ensure that the output is as expected:

Spring Framework Injecting Inner Beans with Examples

Congratulations! You have completed the inner bean injection application in Spring Framework!

In the next article, I am going to discuss Spring Framework Injecting Collections. Here, in this article, I try to explain Spring Framework Injecting Inner Beans with Examples. I hope you enjoy this Spring Framework Injecting Inner Beans article.

Leave a Reply

Your email address will not be published. Required fields are marked *