Three ways for springboot to register bean

  • 2021-06-29 11:01:57
  • OfStack

spring registers bean (java component) in the ioc container on startup to reverse control. When a developer develops an application using spring, you do not see the new keyword. All objects should be obtained from the container. Their life cycle is determined when they are placed in the container!

Here are three ways to register bean

@ComponentScan @Bean @Import

@ComponentScan Registers the bean in the specified package

The Spring container scans the package path of the @ComponentScan configuration and finds a class tagged with the @Component annotation to join the Spring container.

Similar (registered with the IOC container) annotations we often use include the following:

@Configuration: Configuration class @Controller:web Controller @Repository: Data Warehouse @Service: Business logic

The following code completes the registration of EmailLogServiceImpl as an bean, which can also be registered in @Bean litem 1, as described in section @Bean.


@Component
public class EmailLogServiceImpl implements EmailLogService {
 private static final Logger logger = LoggerFactory.getLogger(EmailLogServiceImpl.class);

 @Override
 public void send(String email, String message) {
  Assert.notNull(email, "email must not be null!");
  logger.info("send email:{},message:{}", email, message);
 }
}

@Bean Annotation Direct Registration

Annotation @Bean is declared to have a return type required for methods, which is the type registered with the IOC container. Interfaces and classes are available, in contrast to the Interface-oriented principle, and return types are advocated as interfaces.

The following code registers multiple beans in one @Configuration annotated class.


@Configuration
public class LogServiceConfig {

 /**
  *  extend printLogService Behavior, directly affecting LogService Object because LogService Dependent on PrintLogService.
  *
  * @return
  */
 @Bean
 public PrintLogService printLogService() {
  return new PrintLogServiceImpl();
 }

 @Bean
 public EmailLogService emailLogService() {
  return new EmailLogServiceImpl();
 }

 @Bean
 public PrintLogService consolePrintLogService() {
  return new ConsolePrintLogService();
 }
}

@Import Register Bean

This method is the most direct, directly registering the specified type into the IOC container, to become an java bean, and can place @Import on the 8 ports of the program, which automatically completes the process of registering bean at program startup.


@Import({ LogService.class,PrintService.class })
public class RegistryBean {

}

I think the popularity of Spring is largely due to its design of automatic registration and automatic configuration, which really makes developers feel very comfortable. There are similar products in.net, such as the more popular abp framework in recent years, and Uncle has written similar lind framework, which are based on the concept of automatic registration and automatic configuration.


Related articles: