DEV Community

Phoenix
Phoenix

Posted on

Creating Spring Beans

When you use Spring without Spring Boot, you start by creating something called the ApplicationContext.
This object is the Spring IoC container — it is the place where Spring keeps and manages all the objects it creates.

ApplicationContext context =
        new AnnotationConfigApplicationContext(AppConfig.class);

Enter fullscreen mode Exit fullscreen mode

Here, you are telling Spring:

“Start your container and use AppConfig as the configuration.”

What is AppConfig?

AppConfig is a class you write and mark with @Configuration:

@Configuration
public class AppConfig {
}

Enter fullscreen mode Exit fullscreen mode

This tells Spring:

  • This class contains methods that create objects Spring should manage (factory of Spring Beans)

Inside this class, you write methods marked with @Bean :

@Configuration
public class AppConfig {

    @Bean
    public Engine engine() {
        return new Engine();
    }

    @Bean
    public Car car() {
        return new Car(engine());
    }
}

Enter fullscreen mode Exit fullscreen mode

Each @bean method:

  • The object returned by this method should be stored in the Spring context as a Bean.
  • And hands it over to Spring

Spring then takes that object and stores it inside the ApplicationContext.

These stored objects are called Spring Beans.

What happens when the context is created?

When this line runs:

new AnnotationConfigApplicationContext(AppConfig.class);

Spring does the following:

  • Reads AppConfig
  • Finds all methods marked with @bean
  • Calls those methods
  • Creates the objects (Engine, Car)
  • Stores them inside the context
  • Keeps track of their dependencies and lifecycle

Now the IoC container fully controls these objects.

How do you use them?

Instead of writing:

Car car = new Car();

Enter fullscreen mode Exit fullscreen mode
Car car = context.getBean(Car.class);
Enter fullscreen mode Exit fullscreen mode

Spring gives you the object it created and manages.

That is Inversion of Control:
Spring controls object creation, not you.

Putting it all together

  • @Configuration tells Spring where the bean definitions are
  • @Bean tells Spring which objects to create
  • AnnotationConfigApplicationContext starts the IoC container
  • ApplicationContext stores and manages the beans
  • You ask the context for objects instead of using new

@Component is one of the most important Spring annotations — it’s how you tell Spring:

“Please create and manage an object of this class.”

What is @Component?

@Component marks a class as a Spring Bean so that Spring will automatically create and manage its object in the IoC container.

@Component
public class Engine {
}

Enter fullscreen mode Exit fullscreen mode

This tells Spring:

“When the context starts, create an Engine object and store it.”

How Spring finds it

When the ApplicationContext starts, Spring performs component scanning.

It looks through your packages and finds classes annotated with:

  • @Component
  • @Service
  • @Repository
  • @Controller These are all forms of @Component.

Top comments (0)