码迷,mamicode.com
首页 > 编程语言 > 详细

Spring框架的四种通知

时间:2020-04-23 18:56:44      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:service   aspectj   dao   conf   方法体   proxy   override   wsad   cee   

前置通知:

实现类:

package org_shitang_servier;

import org_shitang.dao.IStudentDao;
import org_shitang_dao.impl.StudentDaoImp1;
import org_shitang_entity.student;

public class StudentServideImp1 implements IstudentService{
    IStudentDao studentDao = (IStudentDao) new StudentDaoImp1();
    public void setStudentDao(IStudentDao studentDao){
        this.studentDao=studentDao;
    }
    public void addStudent(student student){
        studentDao.addStudent(student);
    }
    public void deleteStudentByNo(int stuNo){
        System.out.println("模拟删除");
    }
    
}

前置通知LogBefor()通过一个接口实现特定功能

package org_shitang_aop;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;
//普通类转换成前置通知
public class LogBefore implements MethodBeforeAdvice{
    //前置通知的具体内容
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("前置通知。。。。");
        
    }
    
    
    
}

bean配置:

<!-- 配置前置通知  -->
    <!-- addStudent()所在方法 -->
    <bean id="studentService" class="org_shitang_servier.StudentServideImp1">
        <property name="studentDao" ref="studentDao"></property>
    </bean>
    <!-- 前置类 -->
    <bean id="LogBefore" class="org_shitang_aop.LogBefore">     
    </bean>
    <!-- 进行连接 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <aop:config>
        <!-- 配置切入点 -->
        <aop:pointcut expression="execution(public void org_shitang_servier.StudentServideImp1.deleteStudentByNo(int)) or execution(public void org_shitang_servier.StudentServideImp1.addStudent(org_shitang_entity.student))" id="pointcut"/>
        <aop:advisor advice-ref="LogBefore" pointcut-ref="pointcut"/>
    </aop:config>

后置通知LogAfter()

package org_shitang_aop;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class LogAfter implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("后置通知:目标对象"+target+",调用的方法名:"+method.getName()+",参数的个数"+args.length+",返回值:"+returnValue);
        
    }
    
}

bean配置:

<bean id ="LogAfter" class="org_shitang_aop.LogAfter"></bean>
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <aop:config>
        <!-- 配置切入点 -->
        <aop:pointcut expression="execution(public void org_shitang_servier.StudentServideImp1.deleteStudentByNo(int)) or execution(public void org_shitang_servier.StudentServideImp1.addStudent(org_shitang_entity.student))" id="pointcut1"/>
        <aop:advisor advice-ref="LogAfter" pointcut-ref="pointcut1"/>
    </aop:config>

异常通知LogException()

package org_shitang_aop;

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;

public class LogException implements ThrowsAdvice{
    //异常通知的具体方法
    public void afterThrowing(Method method,Object[] args,Object target,Throwable ex){
        System.out.println("异常通知:目标对象"+target+",调用的方法名:"+method.getName()+",参数的个数"+args.length+",异常:"+ex.getMessage());
    }
}

bean配置:

<bean id="LogException" class="org_shitang_aop.LogException">
    </bean>
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
    <aop:config>
        <aop:pointcut expression="execution(public void org_shitang_servier.StudentServideImp1.deleteStudentByNo(int)) or execution(public void org_shitang_servier.StudentServideImp1.addStudent(org_shitang_entity.student))" id="pointcut2"/>
        <aop:advisor advice-ref="LogException" pointcut-ref="pointcut2" />
    </aop:config>
    

环绕LogAround()

package org_shitang_aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.expression.MethodExecutor;

public class LogAround implements MethodInterceptor{

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        //方法体1
        Object result=null;
        try{
            //前置通知
            System.out.println("用环绕通知写的前置通知");
            result=invocation.proceed();//控制着目标代码的执行:相当于addStudent()
            //返回目标代码的返回值
            //后置通知
            System.out.println("用环绕通知写的后置通知");
        }catch(Exception e){
            //异常通知
            System.out.println("用环绕通知写的异常通知");
        }
        return result;
    }
    
}

bean配置:

<!-- 环绕通知 -->
    <bean id="LogAround" class="org_shitang_aop.LogAround">
    </bean>
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
    <aop:config>
        <aop:pointcut expression="execution(public void org_shitang_servier.StudentServideImp1.deleteStudentByNo(int)) or execution(public void org_shitang_servier.StudentServideImp1.addStudent(org_shitang_entity.student))" id="pointcut3"/>
        <aop:advisor advice-ref="LogAround" pointcut-ref="pointcut3" />
    </aop:config>

 

Spring框架的四种通知

标签:service   aspectj   dao   conf   方法体   proxy   override   wsad   cee   

原文地址:https://www.cnblogs.com/lmff/p/12762524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!