Aop spring 切面编程

Aop spring 切面编程

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

">

<bean id="aspectBean" class="com.xwr.app.cczg.aop.TestAspect" />

<aop:aspectj-autoproxy proxy-target-class="true" /><!--启动aspetj框架 -->

<!--<aop:aspectj-autoproxy />启动aspetj框架 -->

<aop:config>

<aop:aspect id="TestAspects" ref="aspectBean">

<!--配置com.spring.service包下所有类或接口的所有方法 -->

<aop:pointcut id="ljw"

expression="execution(* com.xwr.app.*.*(..))" />

<aop:before pointcut-ref="ljw" method="doBefore" />

<aop:after pointcut-ref="ljw" method="doAfter" />

<aop:around pointcut-ref="ljw" method="doAround" />

<aop:after-throwing pointcut-ref="ljw" method="doThrowing"

throwing="ex" />

</aop:aspect>

</aop:config>

</beans>




public class TestAspect { 

 

    public void doAfter(JoinPoint jp) { 

        System.out.println(&quot;加载之前方法log Ending method: &quot; 

                + jp.getTarget().getClass().getName() + &quot;.&quot; 

                + jp.getSignature().getName()); 

    } 

 

    public Object doAround(ProceedingJoinPoint pjp) throws Throwable { 

        long time = System.currentTimeMillis(); 

        Object retVal = pjp.proceed(); 

        time = System.currentTimeMillis() - time; 

        System.out.println(&quot;运行时间process time: &quot; + time + &quot; ms&quot;); 

        return retVal; 

    } 

 

    public void doBefore(JoinPoint jp) { 

        System.out.println(&quot;加载之后log Begining method: &quot; 

                + jp.getTarget().getClass().getName() + &quot;.&quot; 

                + jp.getSignature().getName()); 

    } 

 

    public void doThrowing(JoinPoint jp, Throwable ex) { 

        System.out.println(&quot;异常处理method &quot; + jp.getTarget().getClass().getName() 

                + &quot;.&quot; + jp.getSignature().getName() + &quot; throw exception&quot;); 

        System.out.println(ex.getMessage()); 

    } 

 

    private void sendEx(String ex) { 

        //TODO 发送短信或邮件提醒 

    } 

}  

为什么切不进来呢??????????????????????




猜你喜欢

转载自lijiwen1987.iteye.com/blog/2197225