How to set and use profile in annotation-based Configuration in Spring framework

Spring DI

Configuration

In the Spring framework, a configuration file called Configuration is passed to the DI container, and "Return this instance in such a case" is set. There are three types of Configuration files.

--Java-based Configuration --XML-based Configuration --Annotation-based Configuration

The annotation-based Configuration is described below.

Annotation-based Configuration

@Configuration Indicates that it is a Configuration class.

AppConfig


package com.example;

@Configuration
public class AppConfig {

}

@Component Register the class as a bean to be managed by the DI container.

User


package com.example;

@Component
public class User {

  public String getName(){
    ...
  }

}

@ComponentScan Scan the class with @Component and register it in DI as a bean. The scan target can be specified as a package. The settings for the AppConfig class mentioned earlier are as follows.

Configuration


package com.example;

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

}

Annotation-based Configuration sample code

If you use the DI container with the above annotations, the code will be as follows.

DemoIoC


package com.example;

public class DemoIoC {
  public static void main(String args[]){

    //Build a DI container and pass a Configuration file
    AnnotationConfigApplicationContext context =
                      new AnnotationConfigApplicationContext(AppConfig.class);

    //Get Bean
    User user = context.getBean(User.class);
    System.out.println(User.getName())
  }
}

Profile settings

In Spring framework, Configuration can be grouped and managed as a profile. For example, Configuration for development, Configuration for testing, and Configuration for products can be managed separately, and the Configuration can be easily switched.

@Profile Set the profile. Change the User class hierarchy as follows and set the profile.

User


package com.example;

public interface User {
  public String getName();
}

UserDummy


package com.example;

@Component
@Profile("dev") //Set profile with the name dev
public class UserDummy implements User {
  ...
}

UserMaster


package com.example;

@Component
@Profile("test") //Set profile with the name test
public class UserImpl implements User {
  ...
}

Profile usage example

Let's set a profile and use the above User class hierarchy.

DemoIoC


package com.example;

public class DemoIoC {
  public static void main(String args[]){

    //Build DI container
    //Set dev as profile
    AnnotationConfigApplicationContext context =
                      new AnnotationConfigApplicationContext();
    context.register(AppConfig.class);
    context.getEnvironment().addActiveProfile("dev");
    context.refresh();

    //Get Bean
    //The implementation of user is UserDummy
    User user = context.getBean(User.class);
    System.out.println(User.getName())
  }
}

Recommended Posts

How to set and use profile in annotation-based Configuration in Spring framework
How to use Lombok in Spring
How to call and use API in Java (Spring Boot)
How to set up and use kapt
How to use CommandLineRunner in Spring Batch of Spring Boot
How to use In-Memory Job repository in Spring Batch
How to use StringBurrer and Arrays.toString.
How to use EventBus3 and ThreadMode
How to use Spring Data JDBC
How to set Spring Boot + PostgreSQL
How to use equality and equality (how to use equals)
How to use InjectorHolder in OpenAM
How to use ModelMapper (Spring boot)
How to use classes in Java?
How to set Lombok in Eclipse
Multilingual Locale in Java How to use Locale
How to use OrientJS and OrientDB together
How to use BootStrap with Play Framework
How to include Spring Tool in Eclipse 4.6.3?
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
How to use environment variables in RubyOnRails
How to use substring and substr methods
Understand in 5 minutes !! How to use Docker
How to use credentials.yml.enc introduced in Rails 5.2
How to use @Builder and @NoArgsConstructor together
How to use ExpandableListView in Android Studio
How to get the setting value (property value) from the database in Spring Framework
[Java] How to use FileReader class and BufferedReader class
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
[Rails] How to use select boxes in Ransack
How to use built-in h2db with spring boot
[Ruby] How to use gsub method and sub method
How to use "sign_in" in integration test (RSpec)
How to use Java framework with AWS Lambda! ??
How to use Spring Boot session attributes (@SessionAttributes)
How to use Segmented Control and points to note
How to add a classpath in Spring Boot
How to set up and operate jEnv (Mac)
Use CarrierWave to set a user's profile picture
How to use scope and pass processing (Jakarta)
How to use JQuery in js.erb of Rails6
I tried to link JavaFX and Spring Framework.
How to bind to property file in Spring Boot
How to define multiple orm.xml in Spring4, JPA2.1
[Rails] How to use PostgreSQL in Vagrant environment
Spring Boot --How to set session timeout time
[Java] How to use Calendar class and Date class
How to use ToolBar with super margin Part1 Set characters and change colors
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
How to set environment variables in the properties file of Spring boot application
[Spring Framework] Configuration split
[Ruby] How to use standard output in conditional branching
How to set the display time to Japan time in Rails
How to set Dependency Injection (DI) for Spring Boot
How to use Map
How to use rbenv
How to use letter_opener_web
How to use fields_for
How to use java.util.logging
How to use map