Spring框架的四种通知
前置通知:
实现类:
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> 相关推荐
与卿画眉共浮生 2020-11-13
smalllove 2020-11-03
hellowordmonkey 2020-11-02
丽丽 2020-10-30
周太郎 2020-10-28
greensomnuss 2020-10-27
职业炮灰 2020-10-16
与卿画眉共浮生 2020-10-14
feinifi 2020-10-14
feinifi 2020-10-13
yangjinpingc 2020-10-09
davis 2020-09-29
RickyIT 2020-09-27
lisongchuang 2020-09-27
tangxiong0 2020-09-03
meleto 2020-08-17
幸运小侯子 2020-08-14
YangHuiLiang 2020-08-06