Injecting Third‑Party Classes in Spring Boot with @Configuration and @Bean

This tutorial explains how to register and inject a third‑party implementation like SwimCoach into a Spring Boot application using a @Configuration class and @Bean methods, covering bean creation, custom IDs, qualifier usage, and practical scenarios such as integrating AWS S3 clients.

Java One
Java One
Java One
Injecting Third‑Party Classes in Spring Boot with @Configuration and @Bean

1. Configuration Process

Directly using the SwimCoach class in a controller causes a startup error because Spring cannot find a bean definition for it.

@RestController
public class FunRestController {
    private Coach coach;

    @Autowired
    public FunRestController(@Qualifier("swimCoach") Coach coach) {
        System.out.println("In constructor: " + getClass().getSimpleName());
        this.coach = coach;
    }

    @GetMapping("/workout")
    public String getDailyWorkout() {
        return coach.getDailyWorkout();
    }
}

To resolve the error, create a configuration class that declares the SwimCoach as a Spring bean.

package com.greenbook.springbootdemo.config;

import com.greenbook.springbootdemo.service.Coach;
import com.greenbook.springbootdemo.service.impl.SwimCoach;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SportConfig {
    // Bean ID defaults to the method name
    @Bean
    public Coach swimCoach() {
        return new SwimCoach();
    }
}

After adding this configuration and restarting the application, the bean is injected successfully.

2. Custom Bean IDs and Qualifiers

The default bean ID is the method name, but you can specify a custom ID with @Bean("mySwimCoach"). When using a custom ID, update the qualifier in the injection point accordingly.

@Configuration
public class SportConfig {
    // Custom bean ID
    @Bean("mySwimCoach")
    public Coach swimCoach() {
        return new SwimCoach();
    }
}
@Autowired
public FunRestController(@Qualifier("mySwimCoach") Coach coach) {
    this.coach = coach;
}

3. Why Use @Bean Instead of @Component?

Using @Bean is useful when the class you need to inject is a third‑party component without source code or annotations, such as an AWS SDK client packaged in a JAR. By defining it as a bean, you make it manageable by the Spring container.

Example scenario: you want to use the Amazon S3 client in your Spring application. The client class comes from the AWS SDK JAR, so you cannot annotate it with @Component. Instead, you create a bean definition:

@Configuration
public class AwsConfig {
    @Bean
    public AmazonS3 amazonS3() {
        return AmazonS3ClientBuilder.standard()
                .withRegion(Regions.US_EAST_1)
                .build();
    }
}

Now the AmazonS3 bean can be injected wherever needed, enabling you to call upload, download, and other S3 operations through Spring’s dependency injection mechanism.

ConfigurationSpringAWSbeanspring-bootdependency-injection
Java One
Written by

Java One

Sharing common backend development knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.