rspec/rules/S4603/java/rule.adoc

68 lines
1.9 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
``++@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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
@ComponentScan
public class MyApplication {
...
}
@SpringBootApplication
public class MyApplication {
...
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
@Configuration
@Import({
DispatcherServletAutoConfiguration.class,
ErrorMvcAutoConfiguration.class,
HttpEncodingAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
MultipartAutoConfiguration.class,
ServerPropertiesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
WebMvcAutoConfiguration.class
})
public class MyApplication {
...
}
----
== Resources
2021-04-28 16:49:39 +02:00
* https://sites.google.com/site/appsdevelopersindia/google-blog/optimizingspringframeworkforappengineapplications[Optimizing Spring Framework for App Engine Applications]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]