Return to site

๐Ÿƒ๐ŸŽ“ SPRING CERTIFICATION QUESTION: How does Spring Boot work? How does it know what to configure?

ยท spring,vcp,java

๐Ÿ‘ฉโ€๐Ÿซ Answer:

Autoconfiguration โš™๏ธ facility is provided by ๐‘ ๐‘๐‘Ÿ๐‘–๐‘›๐‘”-๐‘๐‘œ๐‘œ๐‘ก-๐‘Ž๐‘ข๐‘ก๐‘œ๐‘๐‘œ๐‘›๐‘“๐‘–๐‘”๐‘ข๐‘Ÿ๐‘’ jar.

This jar lists autoconfiguration classes under ๐Œ๐„๐“๐€-๐ˆ๐๐…/๐ฌ๐ฉ๐ซ๐ข๐ง๐ .๐Ÿ๐š๐œ๐ญ๐จ๐ซ๐ข๐ž๐ฌ file, the presence of which is checked โœ… by Spring Boot.

It's important to emphasize that autoconfiguration classes should be registered ๐Ÿ’พ this way only, and should never be the target ๐ŸŽฏ of component scanning.

 

Usually, autoconfiguration classes are annotated with ๐‚๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐š๐ฅ annotation which specifies a condition that should match ๐Ÿค in order for a component to be registered.

Those conditional annotations can be divided ๐Ÿช“ into the following groups:

  • 1. Class conditions (๐‚๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐š๐ฅ๐Ž๐ง๐‚๐ฅ๐š๐ฌ๐ฌ, ๐‚๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐š๐ฅ๐Ž๐ง๐Œ๐ข๐ฌ๐ฌ๐ข๐ง๐ ๐‚๐ฅ๐š๐ฌ๐ฌ}) - which check the presence or absence of a class on the classpath.
  • 2. Bean conditions (๐‚๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐š๐ฅ๐Ž๐ง๐๐ž๐š๐ง, ๐‚๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐š๐ฅ๐Ž๐ง๐Œ๐ข๐ฌ๐ฌ๐ข๐ง๐ ๐๐ž๐š๐ง) - which check the presence or absence of a bean in the ๐๐ž๐š๐ง๐…๐š๐œ๐ญ๐จ๐ซ๐ฒ.
  • 3. Property conditions (๐‚๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐š๐ฅ๐Ž๐ง๐๐ซ๐จ๐ฉ๐ž๐ซ๐ญ๐ฒ) - which check presence or absence of Spring ๐„๐ง๐ฏ๐ข๐ซ๐จ๐ง๐ฆ๐ž๐ง๐ญ property.
  • 4. Resource conditions (๐‚๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐š๐ฅ๐Ž๐ง๐‘๐ž๐ฌ๐จ๐ฎ๐ซ๐œ๐ž) - which check the presence or absence of a resource.
  • 5. Application context type conditions (๐‚๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐š๐ฅ๐Ž๐ง๐–๐ž๐›๐€๐ฉ๐ฉ๐ฅ๐ข๐œ๐š๐ญ๐ข๐จ๐ง, ๐‚๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐š๐ฅ๐Ž๐ง๐๐จ๐ญ๐–๐ž๐›๐€๐ฉ๐ฉ๐ฅ๐ข๐œ๐š๐ญ๐ข๐จ๐ง) - which check the configured type of ๐€๐ฉ๐ฉ๐ฅ๐ข๐œ๐š๐ญ๐ข๐จ๐ง๐‚๐จ๐ง๐ญ๐ž๐ฑ๐ญ.
  • 6. SpEL conditions (๐‚๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐š๐ฅ๐Ž๐ง๐„๐ฑ๐ฉ๐ซ๐ž๐ฌ๐ฌ๐ข๐จ๐ง) - which check a result of specific SpEL expression.

#spring #certificationquestion #vcp

Spring Conditional Annotations ๐Ÿ‘‰ https://www.baeldung.com/spring-conditional-annotations