AOP无法在Spring Boot中运行

时间:2017-02-08 06:04:19

标签: spring-boot aspectj spring-aop

我是第一次在AOP工作。它不起作用 - 也许我混淆了Spring AOP和AspectJ,我不确定。

Gradle文件中的依赖项声明

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    ...
    compile("org.aspectj:aspectjweaver:1.8.9")
    }
}

配置类

@Configuration
@EnableAspectJAutoProxy
public class AspectJConfiguration{

    @Bean
    public MailQueueServiceAspect mailQueueServiceAspect(){            
        return new MailQueueServiceAspect();
    }
}

Aspect Class

@Aspect
public class MailQueueServiceAspect {
    private final Logger log = LoggerFactory.getLogger(MailQueueServiceAspect.class);

    @Pointcut("execution(* com.biscom.bsend.core.SaveMailHelper.aopTest())")
    private void mailUpdatePointCut(){}

    @After("mailUpdatePointCut()")
    public void afterSampleCreation(JoinPoint joinPoint) {
        log.info("Mail Queue updated successfully.");
    }
}

联合点持有人类

public class SaveMailHelper extends AbstractHelper<SaveMailInput, SaveMailOutput> {
    @Inject
    private MailQueueService mailQueueService;

    private final Logger log = LoggerFactory.getLogger(SaveMailHelper.class);

    @Override
    protected void executeHelper(SaveMailInput input, SaveMailOutput output) throws AbstractException {
        MailQueue mailQueue = mailQueueService.get(input.getId(), new MailQueue());

        BlockExecutor e = new BlockExecutor();

        e.execute(input.getMailFrom().shouldSet(mailQueue.isNew(), mailQueue.getMailFrom()), () ->{
            mailQueue.setMailFrom(input.getMailFrom().get());
        });

        output.setEntity(mailQueue);
        aopTest();
    }

    public void aopTest(){
        log.info("AOPTest method called.");
    }
}

我还尝试通过在Aspect类上放置@Component注释来避免使用Configuration。

0 个答案:

没有答案