【问题标题】:AOP can't pointcut class without @Service @Controller没有@Service @Controller AOP 不能切入类
【发布时间】:2018-03-26 13:29:27
【问题描述】:

AOP 可以像@Controller 或@Service 一样切入点。 但是在没有spring注解的Class上效果不好。

package com.erp.module;
@Slf4j
public class SalesOrderModule {
    public void cancel(){
        log.info("test");
    }
    public static SalesOrderModule init(int type) {
        SalesOrderModule salesOrderModule = new SalesOrderModule(salesOrder); 
        *****
        return salesOrderModule;

我的 Aspect.java 如下所示

@Aspect
@Component
@Slf4j
public class WebLogAspect {

    @Pointcut("execution(public * com.erp.controller.*.*(..)) || execution(public * com.erp.module.*.*(..))")
    public void logPointCut() {
    }

我调用取消如下:

SalesOrderModule so = SalesOrderModule.init(3);
so.cancel();

我知道这个问题与代理有关。但是我怎么能

【问题讨论】:

标签: java spring spring-boot aop spring-aop


【解决方案1】:

Spring Docs says:

因此,例如,Spring 框架的 AOP 功能通常与 Spring IoC 容器一起使用。方面使用普通的 bean 定义语法进行配置(尽管这允许强大的“自动代理”功能):这是与其他 AOP 实现的关键区别。有些事情你不能使用 Spring AOP 轻松或有效地完成,例如建议非常细粒度的对象(例如通常的域对象):在这种情况下,AspectJ 是最佳选择。但是,我们的经验是,Spring AOP 为企业 Java 应用程序中的大多数问题提供了出色的解决方案,这些问题都适用于 AOP。

所以如果需要拦截非spring托管代码,需要使用AspectJ而不是Spring AOP。 TBH,到目前为止我不需要那个。

【讨论】:

  • 另一个问题,@Transactional 也不适用于方法取消。我考虑是因为这不在 IoC 中。因此,我怎样才能使跨界工作?
  • 有很多关于@Transactional 不起作用的问题。我建议搜索 SO 或使用代码 sn-ps 创建单独的 SO 问题。
猜你喜欢
  • 2021-12-01
  • 1970-01-01
  • 2019-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-13
  • 2010-10-02
  • 1970-01-01
相关资源
最近更新 更多