๐๐ SPRING CERTIFICATION QUESTION: Can you use @Bean together with @Profile?
๐๐ SPRING CERTIFICATION QUESTION: Can you use @Bean together with @Profile?
Answer:
๐ท๐๐๐๐๐๐ is itself meta-annotated with ๐ป๐๐๐๐๐ annotation which means that
it can be applied to methods, and more specifically factory ๐ฉ๐๐๐ methods.
Use cases for applying ๐ท๐๐๐๐๐๐ annotation on the method level:
1. Bean will only be registered in case the application runs with the same active profile declared in ๐ท๐๐๐๐๐๐ annotation.
2. In case of overloaded ๐ฉ๐๐๐ methods chosen ๐ท๐๐๐๐๐๐ must be the same for all overloaded methods.
If the profile is inconsistent among all overloaded methods, only ๐ท๐๐๐๐๐๐ of the first declared method will be considered.
Examples of mentioned terms:
๐ด๐๐๐๐๐ ๐ณ๐๐๐๐๐ท๐๐๐๐๐๐๐ช๐๐๐๐๐๐๐๐๐๐๐๐ - demonstrates simplistic usage of ๐ท๐๐๐๐๐๐ annotation applied at method level.
/** * Shows basic usage of {@link Profile} annotations applied on method level. */ @Configuration public class MethodLevelProfileConfiguration { /** * Point in time-related to application startup. */ @Profile("qa") @Bean public Instant startupInstant() { return Instant.now(); } /** * Point in time-related to the scheduled shutdown of the application. */ @Bean public Instant shutdownInstant() { return Instant.MAX; } /** * Point in time of 1970 year. */ @Profile("develop & production") @Bean public Instant epochInstant() { return Instant.EPOCH; } }
#spring #certificationquestion #vcp