[JAVA] Change the injection target for each environment with Spring Boot 2

Introduction

I will inject with @ Autowired in Spring Boot, but I will write about what to do if there is a case where you want to change the target to be read for each environment.

The environment here is the Spring Boog startup argument spring.profiles.active, and when this startup argument is changed, the class to be read is changed.

environment

Java10 Spring Boot2.0.4.RELEASE

Since it was made a while ago, the environment is old, but it should work with Java 11 and Boot 2.1 series.

Prepare interface

First, prepare an interface (an abstract class is also possible).

public interface TestInterface {
    void print();
}

Prepare the entity of interface

Since we want to change the target to be read for each environment, we will generate two classes that implement the interface above.

Modify these two classes with startup arguments. Suppose you give test and test2` as arguments.

@Service
@Profile("test")
public class TestServiceA implements TestInterface {
    public void print() {
        System.out.println("test");
    }
}
@Service
@Profile("test2")
public class TestServiceB implements TestInterface {
    public void move() {
        System.out.println("test2");
    }
}

Just add @Profile. If this is added, if the startup arguments do not match, it will not be converted to Bean.

Implementation on the user side

This time we will use this class from Controller.

@Controller
public class TestController {
    @Autowired
    private TestInterface testInterface;

    //Controller processing
}

Just set the target class to interface. Since there is only one target that is a Bean in Profile, the target changes depending on the startup argument.

Recommended Posts

Change the injection target for each environment with Spring Boot 2
Change the setting value for each environment with Digdag (RubyOnRails)
Spring Boot for the first time
Various switching application.properties for each environment when Spring Boot starts
Spring Boot environment construction with Docker (January 2021 version)
Specify the timeout for each path with Rack :: Timeout
Build Spring Boot project by environment with Gradle
Create Spring Boot environment with Windows + VS Code
Create a Spring Boot development environment with docker
Spring boot development-development environment-
Download with Spring Boot
Settings for connecting to MySQL with Spring Boot + Spring JDBC
How to set Dependency Injection (DI) for Spring Boot
Switch environment with Spring Boot application.properties and @Profile annotation
Access the built-in h2db of spring boot with jdbcTemplate
SSO with GitHub OAuth in Spring Boot 1.5.x environment
Prepare the environment for java11 and javaFx with Ubuntu 18.4
How to boot by environment with Spring Boot of Maven
Database environment construction with Docker in Spring boot (IntellJ)
[Spring Boot] Environment construction (macOS)
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Change the port with SpringBoot
Get started with Spring boot
Hello World with Spring Boot!
A story packed with the basics of Spring Boot (solved)
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Try hitting the zip code search API with Spring Boot
Spring Boot starting with Docker
I want to control the maximum file size in file upload for each URL in Spring Boot
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Docker × Spring Boot environment construction
Add module with Spring Boot
Getting Started with Spring Boot
Build Spring for Android 2.0.0 environment
[Android] Change the app name and app icon for each Flavor
Create microservices with Spring Boot
Send email with spring boot
Spring Boot for annotation learning
[Java] Sample project for developing web applications with Spring Boot
Be aware that the default for Spring boot redirect is http
See the relative redirect behavior with the server.tomcat.use-relative-redirects setting in Spring Boot
[Rails] How to change the page title of the browser for each page
See the behavior of entity update with Spring Boot + Spring Data JPA
Use Basic Authentication with Spring Boot
◆ Spring Boot + gradle environment construction memo
gRPC on Spring Boot with grpc-spring-boot-starter
Environment construction with Docker for beginners
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
Inquiry application creation with Spring Boot
Get validation results with Spring Boot