SpringAOP通知配置和使用方法

1.导入相关jar包:

commons-logging-1.1.1.jar
spring-aop-4.3.9.RELEASE.jar
spring-beans-4.3.9.RELEASE.jar
spring-context-4.3.9.RELEASE.jar
spring-core-4.3.9.RELEASE.jar
spring-expression-4.3.9.RELEASE.jar
aopalliance-1.0.jar
aspectjweaver-1.5.3.jar

2.通知配置

(1)前置通知:新建一个类实现MethodBeforeAdvice接口,实现before方法即可

public class logBefore implements MethodBeforeAdvice {

	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("前置通知!");
		System.out.println("调用方法:"+method+",参数个数:"+args.length+"调用对象:"+target);
	}

	
	
}

method:方法      args:参数列表     target:目标对象

(2)后置通知:新建一个类实现AfterReturningAdvice接口,实现afterReturning方法即可

public class logafter implements AfterReturningAdvice{

	
	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
		// TODO Auto-generated method stub
		
		System.out.println("后置通知!");
	}

}

(3)异常通知:新建一个类实现ThrowsAdvice接口,实现afterThrowing方法即可

public class logExceprion implements ThrowsAdvice {
	
	public void afterThrowing(Method method,Object[] args, Object target, Throwable x) {
		
	System.out.println("异常通知:"+"方法:"+method+",对象:"+target+"异常:"+x.getMessage());

}
}

ps:afterThrowing方法要去ThrowsAdvice注释里找
其中自己要给afterThrowing方法加上public .并且该方法参数只能有2种可能:
public void afterThrowing([Method, args, target], ThrowableSubclass):
a.public void afterThrowing(Method, args, target, ThrowableSubclass)    b.public void afterThrowing( ThrowableSubclass)

<p>There are not any methods on this interface, as methods are invoked by
 * reflection. Implementing classes must implement methods of the form:
 *
 * <pre class="code">void afterThrowing([Method, args, target], ThrowableSubclass);</pre>
 *
 * <p>Some examples of valid methods would be:
 *
 * <pre class="code">public void afterThrowing(Exception ex)</pre>
 * <pre class="code">public void afterThrowing(RemoteException)</pre>
 * <pre class="code">public void afterThrowing(Method method, Object[] args, Object target, Exception ex)</pre>
 * <pre class="code">public void afterThrowing(Method method, Object[] args, Object target, ServletException ex)</pre>
 *

(4)环绕通知:新建一个类实现MethodInterceptor接口,实现invoke方法即可

public class logAround implements MethodInterceptor {

	
	Object result=null;
	public Object invoke(MethodInvocation invocation) throws Throwable {
		
	
			
			System.out.println("环绕通知实现的前置通知----");
			 result=invocation.proceed();//决定目标方法是否执行
			 System.out.println("环绕通知实现的后置通知----");
			 System.out.println(invocation.getThis()+invocation.getMethod().getName());
			
			
	
		
		
		return result;
	}

}

invocation.proceed()决定目标方法是否实现;写在该方法前则为前置通知;写在该方法后为后置通知
在目标方法的前后、异常发生时、最终等各个地方都可以 进行的通知,最强大的一个通知;可以获取目标方法的 全部控制权(目标方法是否执行、执行之前、执行之后、参数、返回值等)
在使用环绕通知时,目标方法的一切信息 都可以通过invocation参数获取到 环绕通知底层是通过拦截器实现的。

3.编写业务类及其方法

4.将业务类和通知注入SpringIOC容器中

<bean  id="addStudent" class="org.Test.DaoImpl.addStudentImpl">

</bean>
<bean  id="logException" class="org.Test.LogException.logExceprion">

</bean>
<bean id="logafter" class="org.Test.LogAfter.logafter">

</bean>
<bean id="logBefore" class="org.Test.LogBefore.logBefore">
</bean>
<bean id="logAround" class="org.Test.LogAround.logAround">
</bean>

5.关联切入点和通知:

<!-- 关联 -->
<aop:config  proxy-target-class="true">

	<aop:pointcut expression="execution( public void org.Test.DaoImpl.addStudentImpl.add())" id="pointcut"/>
	<aop:advisor advice-ref="logBefore" pointcut-ref="pointcut"/>

</aop:config>
<aop:config proxy-target-class="true">
	<aop:pointcut expression="execution(public void org.Test.DaoImpl.addStudentImpl.add())" id="pointcut2"/>
	<aop:advisor advice-ref="logafter" pointcut-ref="pointcut2"/>
</aop:config>

<aop:config proxy-target-class="true">
	<aop:pointcut expression="execution(public void org.Test.DaoImpl.addStudentImpl.add())" id="pointcut3"/>
	<aop:advisor advice-ref="logException" pointcut-ref="pointcut3"/>
</aop:config>
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution(public void org.Test.DaoImpl.addStudentImpl.add())" id="pointcut4"/>
<aop:advisor advice-ref="logAround" pointcut-ref="pointcut4"/>
</aop:config>

<aop:advisor advice-ref="logAround" pointcut-ref="pointcut4"/>  是连接彼此的线,左端是通知,右端是切点

展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值