DEV Community

realNameHidden
realNameHidden

Posted on

3 1 1 1 1

@Bean annotation example in spring boot

Directory Structure

Image description

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>3.4.3</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.example</groupId>
 <artifactId>BeanExample</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>BeanExample</name>
 <description>Demo project for Spring Boot</description>
 <url/>
 <licenses>
  <license/>
 </licenses>
 <developers>
  <developer/>
 </developers>
 <scm>
  <connection/>
  <developerConnection/>
  <tag/>
  <url/>
 </scm>
 <properties>
  <java.version>17</java.version>
 </properties>
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>

</project>

Enter fullscreen mode Exit fullscreen mode

📌 @bean Annotation Example in Spring Boot
The @bean annotation in Spring Boot is used to define a bean manually inside a @Configuration class. It tells Spring to manage an instance of the object and inject it wherever needed.

🔹 Example 1: Basic @bean Usage

✔ Scenario: You want to create and manage a HelloService bean manually instead of using @Component.

🚀 Implementation
Step 1: Create a Service Class

public class HelloService {
    public String sayHello() {
        return "Hello, Spring Boot!";
    }
}

Enter fullscreen mode Exit fullscreen mode

Step 2: Define Bean in a @Configuration Class

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

@Configuration
public class AppConfig {

    @Bean
    public HelloService helloService() {
        return new HelloService();
    }
}

Enter fullscreen mode Exit fullscreen mode

Step 3: Use the Bean in a Controller

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

    private final HelloService helloService;

    public HelloController(HelloService helloService) {
        this.helloService = helloService;
    }

    @GetMapping
    public String sayHello() {
        return helloService.sayHello();
    }
}

Enter fullscreen mode Exit fullscreen mode

🌟 Expected Output

GET /hello  --> Response: "Hello, Spring Boot!"

Enter fullscreen mode Exit fullscreen mode

🛠 When to Use @bean?
✅ When you need fine-grained control over bean creation.
✅ When the class does not use @Component, @Service, or @Repository.
✅ When you want to configure third-party libraries (e.g., RestTemplate, DataSource).

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More