springAop的配置XML实现

第一次写博客不太会排版,见谅了

看了那么多的springAop,但最终都没有自己敲出来的理解深刻,下面是一个通过xml配置实现的springAop功能

applicationContext.xml的配置

因为也打算自己参考用所以将大部分的schema也贴进来了,小伙伴根据自己需要选择就好

 
 

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!--开启spring注解扫描--> <context:annotation-config/> <context:component-scan base-package="controller"></context:component-scan> <context:component-scan base-package="service"></context:component-scan> <aop:config> <aop:aspect id="logAspect" ref="logSave"> <aop:pointcut id="logSaveCut" expression="execution(* controller.*.*test1(..))" /> <aop:before method="before" pointcut-ref="logSaveCut"></aop:before> <aop:after method="after" pointcut-ref="logSaveCut"></aop:after> <aop:after-returning method="returnMethod" pointcut-ref="logSaveCut"></aop:after-returning> <aop:after-throwing method="throwMethod" pointcut-ref="logSaveCut"></aop:after-throwing> <aop:around method="aroundMethod" pointcut-ref="logSaveCut"></aop:around> </aop:aspect> </aop:config> <bean id="logSave" class="common.LogSave"></bean>

<bean id="testBean" class="controller.logController"></bean>

切面class

package common;

import org.aspectj.lang.ProceedingJoinPoint;

public class LogSave {
    public void before(){
        System.out.println("执行前");
    }

    public void after(){
        System.out.println("执行后");
    }

    public void returnMethod(){
        System.out.println("返回值返回后");
    }

    public void throwMethod(){
        System.out.println("出现异常后");
    }

    public Object aroundMethod(ProceedingJoinPoint point){
        System.out.println("环绕方法执行前");
        try{
           Object result =   point.proceed();
         System.out.println("环绕方法执行后");
         return result;
        }catch (Throwable throwable){
            throwable.printStackTrace();
            return throwable.getMessage();

        }
    }


}

这里注意环绕方法的理解,我个人理解是:相当于before和after,只不过前后操作自己在环绕方法内控制

测试类

public class App 
{
    public static void main( String[] args ) throws Exception {

        //加载spring配置文件
        AbstractApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取测试用bean
        logController test = (logController)context.getBean("testBean");
        //会被拦截的方法
        test.firsttest1();
        System.out.println("----------------------");
        test.secondtest1();
        System.out.println("----------------------");
        test.aroundtest1();
        System.out.println("----------------------");
        test.returntest1();
        System.out.println("----------------------");
        test.throwtest1();
        System.out.println("----------------------");


        //不会被拦截的方法
        test.firsttest2();
        System.out.println("----------------------");
        test.secondtest2();
    }

测试结果

执行前
环绕方法执行前
第一个test1执行中
环绕方法执行后
返回值返回后
执行后
----------------------
执行前
环绕方法执行前
第二个test1执行中
环绕方法执行后
返回值返回后
执行后
----------------------
执行前
环绕方法执行前
环绕方法测试
环绕方法执行后
返回值返回后
执行后
----------------------
执行前
环绕方法执行前
环绕方法执行后
返回值返回后
执行后
----------------------
执行前
环绕方法执行前
要抛异常了
返回值返回后
执行后
----------------------
第一个test2执行中
----------------------
第二个test2执行中
java.lang.Exception: 测试异常
 at controller.logController.throwtest1(logController.java:15)
 at controller.logController$$FastClassBySpringCGLIB$$7d45bb2e.invoke(<generated>)
 at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
 at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
 at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
 at common.LogSave.aroundMethod(LogSave.java:27)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:629)
 at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:618)
 at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
 at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
 at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
 at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:47)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
 at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
 at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
 at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656)
 at controller.logController$$EnhancerBySpringCGLIB$$51b05925.throwtest1(<generated>)
 at logbackmvn.App.main(App.java:29)



猜你喜欢

转载自blog.csdn.net/qq_16765879/article/details/80582750