弹簧不会触发注释

时间:2011-10-03 04:54:20

标签: spring aop

我在方法上使用注释。每当该注释出现时,我想用aop拦截它。我错过了什么。

<bean id="emailAdvice" class="com.merc.spring.aop.advice.MultiThreadEmailAdvice"/> 

<aop:config>  
    <aop:aspect ref="emailAdvice">  
        <aop:around 
            method="fork"  
            pointcut="execution(* org.springframework.mail.javamail.JavaMailSenderImpl.send(..))"/>  
    </aop:aspect>  
    <aop:aspect ref="emailAdvice">
        <aop:around method="sendEmailAdvice" pointcut="@annotation(sendMailAnnotation)" arg-names="sendMailAnnotation"/>
    </aop:aspect>
</aop:config>
@SendMailAnnotation()
public void testAnnotationEmail() {
    System.out.println("send an email");
}

@Aspect
public class MultiThreadEmailAdvice {
    public void sendEmailAdvice(ProceedingJoinPoint pjp, SendMailAnnotation sendMailAnnotation) throws Throwable {
        System.out.println("before method execution");

        pjp.proceed();

        System.out.println("after method execution");

        System.out.println(sendMailAnnotation.from());
    }
}

2 个答案:

答案 0 :(得分:0)

尝试更改

@annotation(sendMailAnnotation)

@annotation(<package>.SendMailAnnotation).

在你的bean定义中。

实施例

<aop:around method="sendEmailAdvice" pointcut="@annotation(com.merc.spring.aop.advice.SendMailAnnotation)" arg-names="sendMailAnnotation"/>

答案 1 :(得分:0)

事实证明,调用带注释方法的服务类不是Spring管理的。一旦在春季管理,它运作良好