0

Inner bean is a bean defined inside another bean.

but I want to clarify that with an annotation-based example. Most of the answers are considering XML-based configurations and explain what an inner bean means, for example: XML-based-example.

Here I tried to code an example using annotations.

Q1: The Racer.java class has a car property. I use the @Bean method inside the Race.java bean. Does that mean Car is an inner bean?

Q2: If I annotate the Car.java class with @Component annotation and remove the @Bean method in Racer.java. Would the Car.java be still an inner bean?

I would appreciate having a simple explanation. Thanks in advance.

Entity(s)

Racer.java

package oi.spring.demo.entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class Racer {

    /* properties of a Racer. */
    private String name;

    @Autowired
    private Car car;

    public Racer() {
        System.out.println(this.getClass().getSimpleName() + ": no-args constructor executed!");
    }

    // Bean method for `car`.
    @Bean
    public Car car() {
        return new Car();
    }

    // setters and getters

    public String getName() {
        return name;
    }

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

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    @Override
    public String toString() {
        return "Racer{" +
                "name='" + name + '\'' +
                ", car=" + car +
                '}';
    }
}

Car.java

package oi.spring.demo.entity;

public class Car {
    private String manufacturer;
    private String name;

    public Car() {
        System.out.println(this.getClass().getSimpleName() + ": no-args constructor executed!");
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Car{" +
                "manufacturer='" + manufacturer + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

Config

AppConfig.java

package oi.spring.demo.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan(basePackages = {"oi.spring.demo.entity"})
@Configuration
public class AppConfig {
}

AppInitializer.java

package oi.spring.demo;

import oi.spring.demo.config.AppConfig;
import oi.spring.demo.entity.Racer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AppInitializer {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(AppConfig.class);
        ctx.refresh();

        Racer racer = ctx.getBean(Racer.class);
        System.out.println("Just setting some values: ---------------");
        racer.setName("Lewis Hamilton");
        racer.getCar().setManufacturer("Mercedes");
        racer.getCar().setName("Mercedes EQC");

        System.out.println(racer);
    }
}

pom.xml dependencies

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.13</version>
        </dependency>
Dhanusha_Perera07
  • 909
  • 1
  • 8
  • 18

0 Answers0