How to Ignore a Detection at Startup in SpringBoot

  • 2021-12-11 07:16:42
  • OfStack

Directory SpringBoot startup ignores a certain detection. You can fill startup in the startup file to eliminate some injection problems of bean

SpringBoot startup ignores a detection

When building springboot framework, startup will check whether some connections are normal, such as redis, rabbitmq and other components. If related services are not started at this time, or you do not use this component, you do not want to check this item.

You can fill in the startup file


management.health.redis.enabled: false  ## Startup does not detect redis
management.health.rabbit.enabled: false ## Start without checking rabbit

Initiate the injection to exclude some bean

Problem

When doing the project recently, it is necessary to introduce other jar. Then you need to scan some bean in these jar. So use the annotation: @ ComponentScan

This annotation can directly specify the package name. It will scan all class under this package and then judge whether it is resolved:


@ComponentScan(basePackages = {"your.pkg", "other.pkg"})
public class Application {
}   

redissonConfig is defined in other jar. Then my own project also defined redissonConfig, bean. Causing an error to be reported when the project starts. So exclude RedissonConfig. class from jar in the following way.


@ComponentScan(basePackages = {"com.xx.xx.*"}, excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {RedissonConfig.class}))

@ ComponentScan annotation. The scanned or parsed bean can only be defined internally in Spring, such as @ Component, @ Service, @ Controller, or @ Repository. If you have a custom annotation, such as @ Consumer, the annotation-modified class will not be scanned. At this time, we have to customize the scanner to complete this operation.

Used in the configuration file: The component-scan tag underneath uses the ClassPathBeanDefinitionScanner class to do the scanning. The @ ComponentScan annotation is used in conjunction with the @ Configuration annotation, and the bottom layer uses the ComponentScanAnnotationParser parser to complete the parsing work.


Related articles: