Lesson

Create a Spring Bean

spring/Spring Boot

How to Create a Spring Bean (3 Ways)

1. Introduction

Spring Framework is used to build scalable Java applications.

In Spring:

  • Objects are called beans

  • Beans are managed by the IoC container

  • The container handles:

    • Object creation

    • Dependency injection

    • Lifecycle management


2. Ways to Create a Spring Bean

There are three main ways:

  1. XML Configuration

  2. Annotation-Based Configuration

  3. Java-Based Configuration


3. Using XML Configuration

This is the traditional approach.

Step 1: Create Bean Class

plaintext
package com.example;

public class UserService {

    public void showMessage() {
        System.out.println("Hello from UserService!");
    }
}

Step 2: Create XML File

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

    <bean id="userService"
          class="com.example.UserService"/>
</beans>

Step 3: Main Class

plaintext
package com.example;

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");

        UserService userService =
            (UserService) context.getBean("userService");

        userService.showMessage();
    }
}

4. Using Annotation-Based Configuration

Modern and widely used approach.

Step 1: Bean Class

plaintext
package com.example;

import org.springframework.stereotype.Component;

@Component
public class UserService {

    public void showMessage() {
        System.out.println("Hello from Annotation!");
    }
}

Step 2: Configuration Class

plaintext
package com.example;

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

@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
}

Step 3: Main Class

plaintext
package com.example;

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

public class App {

    public static void main(String[] args) {

        ApplicationContext context =
            new AnnotationConfigApplicationContext(AppConfig.class);

        UserService userService =
            context.getBean(UserService.class);

        userService.showMessage();
    }
}

5. Using Java-Based Configuration

Configuration is written fully in Java.

Step 1: Bean Class

plaintext
package com.example;

public class UserService {

    public void showMessage() {
        System.out.println("Hello from Java Config!");
    }
}

Step 2: Configuration Class

plaintext
package com.example;

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

@Configuration
public class AppConfig {

    @Bean
    public UserService userService() {
        return new UserService();
    }
}

Step 3: Main Class

plaintext
package com.example;

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

public class App {

    public static void main(String[] args) {

        ApplicationContext context =
            new AnnotationConfigApplicationContext(AppConfig.class);

        UserService service =
            context.getBean(UserService.class);

        service.showMessage();
    }
}

6. Comparison

XML Configuration


  • Separate configuration


  • More verbose


  • Older approach

Annotation-Based


  • Less code


  • Easy to use


  • Widely used in Spring Boot

Java-Based


  • Type-safe


  • Fully Java-based


  • Good for complex configurations


7. Conclusion


  • XML is traditional but less used now


  • Annotation is most popular


  • Java config is clean and flexible