@EnableAspectJAutoProxy不起作用

Dan*_*ode 9 java spring aspectj

我正在使用Spring Boot,我想使用AspectJ.

以下作品(当然):

@Aspect
@Component
public class RequestMappingAspect {

    @Before("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void advice(JoinPoint joinPoint) {
        ...
    }
}

但是,如果@Component删除并@EnableAspectJAutoProxy添加,则以下操作无效.

@SpringBootApplication
@EnableSwagger2
@EnableAspectJAutoProxy
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

如何正确启用AspectJ自动代理?

veg*_*sen 5

想知道同样的事情,我们最终做了类似的事情:

@EnableAspectJAutoProxy(proxyTargetClass = true)
@Configuration("Main applicationContext")
@ComponentScan(
    basePackages = {"com.where.ever"},
    excludeFilters = {@ComponentScan.Filter(Aspect.class)})
public class ApplicationConfiguration {
    @Bean(autowire = Autowire.BY_TYPE)
    public SomeAspect someAspect() {
        return Aspects.aspectOf(SomeAspect.class);
    }
    ...
    ...
}
Run Code Online (Sandbox Code Playgroud)

这使我们能够@Aspect在方面添加-annotation,这也正确地连接了它们。可能这是一个毫无意义的回复,但是,它解释了我们如何解决问题 - 而不是问题的实际解决方案。如果您希望删除此内容,请告诉我。


Kal*_*oni 5

您既需要@EnableAspectJAutoProxy进行弹簧配置,又需要@Aspect / @Component批注的组合

@EnableAspectJAutoProxy与基于xml的<aop:aspectj-autoproxy>做相同的事情