diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJBeanFactoryInitializationAotProcessor.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJBeanFactoryInitializationAotProcessor.java index 681338a7ec..5fe8c5c35b 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJBeanFactoryInitializationAotProcessor.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJBeanFactoryInitializationAotProcessor.java @@ -28,22 +28,42 @@ import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcess import org.springframework.beans.factory.aot.BeanFactoryInitializationCode; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.lang.Nullable; +import org.springframework.util.ClassUtils; /** * {@link BeanFactoryInitializationAotProcessor} implementation responsible for registering * hints for AOP advices. * * @author Sebastien Deleuze + * @author Stephane Nicoll * @since 6.0.11 */ class AspectJBeanFactoryInitializationAotProcessor implements BeanFactoryInitializationAotProcessor { + private static final boolean aspectJPresent = ClassUtils.isPresent( + "org.aspectj.lang.annotation.Pointcut", AspectJBeanFactoryInitializationAotProcessor.class.getClassLoader()); + @Nullable @Override public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) { - BeanFactoryAspectJAdvisorsBuilder builder = new BeanFactoryAspectJAdvisorsBuilder(beanFactory); - List advisors = builder.buildAspectJAdvisors(); - return advisors.isEmpty() ? null : new AspectContribution(advisors); + if (aspectJPresent) { + return AspectDelegate.processAheadOfTime(beanFactory); + } + return null; + } + + /** + * Inner class to avoid a hard dependency on AspectJ at runtime. + */ + private static class AspectDelegate { + + @Nullable + private static AspectContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) { + BeanFactoryAspectJAdvisorsBuilder builder = new BeanFactoryAspectJAdvisorsBuilder(beanFactory); + List advisors = builder.buildAspectJAdvisors(); + return advisors.isEmpty() ? null : new AspectContribution(advisors); + } + }