通过配置方式创建通知类

通过配置将 普通类–>通知类

基于Schema配置(类似于实现接口的方式)

 接口方式:
 
        //普通类 -> 后置通知
        public class LogAfter implements AfterReturningAdvice {}
Schema方式:

    1.编写一个普通类:

        如:
            public class Log_Schema{}

        a.类中前置通知方法:
            如:
             //前置通知
                public void before(){
                    System.out.println("{{[配置方式(Schema)]前置通知...}}");
                }
        b.类中后置通知方法:
            如:
             //后置通知
                 public void afterReturning(JoinPoint jp, Object returningValue){
                     System.out.println("{{[配置方式(Schema)]后置通知...}}:");
                     System.out.println("*[配置方式]*目标对象:"+jp.getTarget()+
                             ",\n*[配置方式]*调用的方法名:"+jp.getSignature().getName()+
                             ",\n*[配置方式]*方法的参数个数:"+jp.getArgs().length+
                             ",\n*[配置方式]*方法得到返回值:"+returningValue);
                 }
        c.类中异常通知方法:
            如:
             //异常通知
                 public void whenException(JoinPoint jp, NullPointerException e){
                     System.out.println("{{[配置方式(Schema)]--特定异常通知...}}:e:"+e.getMessage());
                 }
        d.类中的环绕通知方法:
            如:
              //环绕通知 :注意会返回目标方法的返回值
                 public Object around(ProceedingJoinPoint jp){
                     Object result = null;
                     try{
                         System.out.println("{{配置方式(Schema)[环绕通知]--前置通知...}}");

                         result = jp.proceed();//执行方法
                         System.out.println("{{配置方式(Schema)[环绕通知]--执行的方法:}}"+jp.getSignature().getName()+"的返回值为:"+result);

                         System.out.println("{{配置方式(Schema)[环绕通知]--后置通知...}}");
                     }catch (Throwable e){
                         System.out.println("{{配置方式(Schema)[环绕通知]--异常通知...}}");
                     }
                     return result;
                 }


    2.将该类通过配置,转为一个通知类:
        a.若要使用返回值,和注解方式相同需要声明,returning="返回值参数名";
        b.在特定异常通知中:要声明特定异常类型,throwing="异常类型参数名";

        如:
            <!--将准备转为通知的类纳入IOC容器中【配置方式将 普通类->通知类】-->
                <bean id="log_Schema" class="org.aop.Log_Schema"></bean>
                <!--将addStudent() 和 通知Log_Schema 进行关联 -->
                <aop:config>
                    <!--配置切入点(连接线的一端:业务类的具体方法)-->
                    <aop:pointcut
                            id="pointcut5"
                            expression="execution(public void org.service.impl.StudentServiceImpl.addStudent(org.entity.Student))"/>
                    <!--(连接线的另一端:通知类)-->
                     <aop:aspect ref="log_Schema">
                           <!--前置通知连接线:连接业务addStudent方法和通知before-->
                           <aop:before method="before" pointcut-ref="pointcut5"/>
                           <!--后置通知连接线:连接业务addStudent方法和通知after-returning-->
                           <aop:after-returning method="afterReturning" returning="returningValue" pointcut-ref="pointcut5"/>
                           <!--环绕通知连接线:连接业务addStudent方法和通知around-->
                           <aop:around method="around" pointcut-ref="pointcut5"/>
                           <!--特定异常通知连接线:连接业务addStudent方法和通知whenException-->
                           <aop:after-throwing method="whenException" throwing="e" pointcut-ref="pointcut5"/>
                     </aop:aspect>
                </aop:config>

注:
如果要获取目标对象信息:
注解方式、Schema方式:
JoinPoint类获取、其中环绕通知用:ProceedingJoinPoint类获取;
通过实现接口方式:
Object o, Method method, Object[] objects, Object o1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值