Details
-
Enhancement
-
Status: In Progress
-
Should
-
Resolution: Unresolved
-
None
-
None
-
None
-
Low
Description
@ComponentScan is used to find which Spring @Component beans (@Service or @Repository or Controller) are available in the classpath so they can be used in the application context. This is a convenient feature especially when you begin a new project but it comes with the drawback of slowing down the application start-up time especially when the application becomes bigger (ie: it references a large JAR file, or it references a significant number of JAR files, or the base-package refers to a large amount of .class files).
@ComponentScan should be replaced by an explicit list of Spring beans loaded by @Import.
The interface @SpringBootApplication is also considered by this rule because it is annotated with @ComponentScan.
Noncompliant Code Example
@ComponentScan public class MyApplication { ... } @SpringBootApplication public class MyApplication { ... }
Compliant Solution
@Configuration @Import({ DispatcherServletAutoConfiguration.class, ErrorMvcAutoConfiguration.class, HttpEncodingAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, MultipartAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, WebMvcAutoConfiguration.class }) public class MyApplication { ... }
https://rules.sonarsource.com/java/tag/performance/RSPEC-4603
This ticket is mostly about investigating if we can improve the performance of the reference application modules startup by taking this into consideration. https://github.com/search?l=&q=org%3Aopenmrs+component-scan&ref=advsearch&type=Code