Ways to Create Beans in Spring

If you’re reading this, chances are you’re either familiar with Java and Spring or just curious about how beans work in Spring. Either way, let’s start by understanding what beans are in the context of Spring.

Beans are objects that are instantiated, assembled, and managed by the Spring IoC (Inversion of Control) container.

Now, let’s dive into our main topic: the various ways to create beans in Spring. While there are several methods, the most popular approach is using annotations, which can sometimes lead newer developers to overlook other options. Let’s explore these methods in detail.

1. Using @Configuration and @Bean Annotations

The @Configuration and @Bean annotations offer a Java-based way to define beans, making your configuration type-safe and easy to refactor.

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

@Configuration
public class AppConfig {

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}

2. Using ApplicationContext with @Configuration

Another approach is to use ApplicationContext alongside a Java configuration class. This is useful when you want to programmatically initialize the application context.

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        MyService myService = context.getBean(MyService.class);
        myService.performTask();
    }
}

3. Using ApplicationContext with ClassPath XML

Spring also allows bean definitions through an XML configuration file. This traditional method is useful in legacy systems or when you want to separate configuration from code.

XML Configuration (beans.xml):

<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.xsd">

    <bean id="myService" class="com.example.MyServiceImpl"/>
</beans>

Java Code:

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

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        MyService myService = context.getBean(MyService.class);
        myService.performTask();
    }
}

4. Using the registerBean Method of ApplicationContext

For more dynamic control, Spring provides the registerBean method, allowing you to programmatically register beans within the ApplicationContext. This is particularly useful when you need to create beans dynamically during execution.

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.registerBean(MyService.class, MyServiceImpl::new);
        context.refresh();

        MyService myService = context.getBean(MyService.class);
        myService.performTask();
    }
}

Conclusion

Spring offers several ways to create and manage beans, each with its own advantages:

Each method has its place, depending on the needs of your project. By understanding these approaches, you can choose the one that best suits your application’s requirements.