Spring AOP,Before Advices、After Advice、Around Advice、Throw Advice的解析与使用。

AOP是一个横向的关系:如果说“对象”是一个空心的圆柱体,其中封装的是对象的属性和行为;那么面向方面编程的方法,就仿佛一把利刃,将空心圆柱体剖开,以获得其内部的消息。而剖开的切面又能将剖开的切面复原,不留痕迹。
AOP的核心思想就是“将应用程序中的商业逻辑同对其提供支持的通用服务进行分离”。

切面(Aspect):一个关注点的模块化,这个关注点可能会横切多个对象(比如事务管理);
在Spring AOP中,切面可以使用基于模式或者基于@Aspect注解的方式来实现。

连接点(Joinpoint):在程序执行过程中某个特定的点,比如某方法调用时或者处理异常时;
在Spring AOP中,一个连接点总是表示一个方法的执行。

Spring AOP,Before Advices、After Advice、Around Advice、Throw Advice的解析与使用。
BeforeAdviceDemo:方法前被调用

public class BeforeAdviceDemo implements MethodBeforeAdvice{//实现MethodBeforeAdvice接口

	@Override
	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
		System.out.println("在方法运行前,先运行");
	}
}

AfterAdviceDemo:After处理逻辑在执行目标操作后执行afterRetruning( )方法:

public class AfterAdviceDemo implements AfterReturningAdvice{//实现AfterReturningAdvice接口

	@Override
	public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("方法执行后 .");   

	}

}

AroundAdviceDemo :Around Advice能够在执行目标操作前后执行,功能强大

public class AroundAdviceDemo implements MethodInterceptor{//实现MethodInterceptor接口

	public Object invoke(MethodInvocation arg0) throws Throwable {
		System.out.println("在方法执行之前");
		Object object=null;
		object=arg0.proceed();
		System.out.println("在方法执行之后");
		return object;
	}

}

ThowAdviceDemo : 如果连接点抛出异常,Throws Advice在连接点返回后被调用。

public class ThowAdviceDemo implements ThrowsAdvice {
	 public void afterThrowing(Method method, Object[] args, Object target,Throwable subclass) {
	        System.out.println("异常抛出后 ..");
	    }

}

xml文件配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
	<bean id="helloImpl" class="com.my.bean8.HelloImpl" />
	
	<!-- after -->
	<bean id="logAfterAdvice" class="com.my.bean8.AfterAdviceDemo"/>
	
	
	<!-- before -->
	<bean id="logBeforeAdvice" class="com.my.bean8.BeforeAdviceDemo" />
	
	<!-- around -->
	<bean id="logRoundAdvice" class="com.my.bean8.AroundAdviceDemo"></bean>
	
	<!-- throwable -->
	<bean id="throwAdvice" class="com.my.bean8.ThowAdviceDemo"></bean>
	
	<bean id="helloProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
	<property name="proxyInterfaces" value="com.my.bean8.IHello" />
	<property name="target" ref="helloImpl" />
    	<property name="interceptorNames">
		<list>
        	<value>logAfterAdvice</value>
        	<value>logBeforeAdvice</value>
        	<value>logRoundAdvice</value>
        	<value>throwAdvice</value>
        </list>
    </property>
	</bean>
	
</beans>

Test:

public class Test {
    public static void main(String[] args) {
       ApplicationContext context =  new FileSystemXmlApplicationContext("src/com/my/bean8/applicationContext.xml");
       IHello helloProxy = (IHello) context.getBean("helloProxy");
      try {
		helloProxy.hello("王小二");
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}}
}

绝知此事要躬行,代码还是自己敲一遍更加容易理解。