Spring Certified Professional 2023 · Free Practice Question Medium
Question 30
What should you use on a configuration class to enable aspect support?
-
A
@AspectJConfiguration -
B
@Configuration(enableAspectJAutoProxy=true) -
C
@EnableAspectJAutoProxy -
D
@Aspect
Reveal correct answer
Correct answer: C
Explanation
Spring doc:
EnableAspectJAutoProxy
Enables support for handling components marked with AspectJ's @Aspect annotation.
To be used on @Configuration classes as follows:
- @Configuration
- @EnableAspectJAutoProxy
- public class AppConfig {
- @Bean
- public FooService fooService() {
- return new FooService();
- }
- @Bean
- public MyAspect myAspect() {
- return new MyAspect();
- }
- }
Wrongs answers:
@Configuration does not contain such enableAspectJAutoProxy attribute.
There is no @AspectJConfiguration .
@Aspectis used to define aspects, not to enable aspect support.
More on AOP:
https://docs.spring.io/spring-framework/docs/2.5.x/reference/aop.html
Note:
I think it's worth adding, that
If AspectJ is on the classpath, Spring Boot’s auto-configuration will automatically enable AspectJ auto proxy such that @EnableAspectJAutoProxy is not required.
https://docs.spring.io/spring-boot/reference/features/aop.html
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
