<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/p
http://www.springframework.org/schema/p/spring-p-2.5.xsd
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<!--===================================================
=
= BeforeAdvice 前置通知
============ ========================================-->
<bean id="loggingBeforeAdvice" class="cn.aop.advice.LoggingBeforeAdvice"/><!-- 前置通知 -->
<bean id="pointcut" class = "org.springframework.aop.support.NameMatchMethodPointcut" p:mappedName="getContext" /><!-- 切入点 -->
<bean id="loggingBeforeAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor" p:advice-ref="loggingBeforeAdvice" p:pointcut-ref="pointcut"/>
<!-- 自动pointcut实例化 -->
<bean id="loggingBeforAdvisor2" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor" p:advice-ref="loggingBeforeAdvice" p:mappedName="getContext"/>
<!-- 自动pointcut实例化 正则表达式方式名字匹配 -->
<bean id="loggingBeforAdvisor3" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" p:advice-ref="loggingBeforeAdvice" p:pattern=".*"/>
<!-- 有接口Bean -->
<bean id="helloworldbeanTarget" class="cn.aop.bean.HelloWorldAOPBean"></bean>
<!-- 无接口Bean -->
<bean id="helloworldbeanTarget2" class="cn.aop.bean.HelloWorldAOPBean2"></bean>
<!-- 指定接口AOPBean 指定target属性-->
<bean id="helloworldbean1" class="org.springframework.aop.framework.ProxyFactoryBean" p:proxyInterfaces="cn.aop.itf.IHelloWorldAOP" p:target-ref="helloworldbeanTarget">
<property name="interceptorNames">
<list>
<idref local="loggingBeforeAdvisor"/>
</list>
</property>
</bean>
<!-- 自动检测接口AOPBean 指定 target属性-->
<bean id="helloworldbean2" class="org.springframework.aop.framework.ProxyFactoryBean" p:target-ref="helloworldbeanTarget">
<property name="interceptorNames">
<list>
<idref local="loggingBeforAdvisor2"/>
</list>
</property>
<property name="autodetectInterfaces" value="true"/>
</bean>
<!-- 无接口目标对象需要启用CGLIB代理 需要加入CGLIB库 -->
<!-- 无接口AOPBean 指定target属性-->
<bean id="helloworldbean3" class="org.springframework.aop.framework.ProxyFactoryBean" p:target-ref="helloworldbeanTarget2">
<property name="interceptorNames">
<list>
<idref local="loggingBeforAdvisor2"/>
</list>
</property>
<property name="autodetectInterfaces" value="true"/>
</bean>
<!-- 无接口AOPBean 不指定 target属性-->
<bean id="helloworldbean4" class="org.springframework.aop.framework.ProxyFactoryBean" >
<property name="interceptorNames">
<list>
<idref local="loggingBeforAdvisor2"/>
<idref local="helloworldbeanTarget2"/><!-- 目标对象要放在最后一个 -->
</list>
</property>
<property name="autodetectInterfaces" value="true"/>
</bean>
<!--===================================================
=
= AfterAdvice 后置通知
============ ========================================-->
<bean id="loggingAfterReturningAdvice" class="cn.aop.advice.LogginAfterReturningAdvice"></bean>
<bean id="loggingAfterReturningAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" p:advice-ref="loggingAfterReturningAdvice" p:pattern=".*"></bean>
<bean id ="helloworldbean5" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="autodetectInterfaces" value="true"/>
<property name="interceptorNames">
<list>
<idref local="loggingAfterReturningAdvisor"/>
<idref local="loggingBeforAdvisor3"/>
<idref local="helloworldbeanTarget"/>
</list>
</property>
</bean>
<!--===================================================
=
= AfterThrowing 后置异常通知
============ ========================================-->
<bean id="helloworldbeanTarget3" class="cn.aop.bean.HelloWorldThrowing"></bean>
<bean id="loggingAfterThrowingAdvice" class="cn.aop.advice.LoggingAfterThrowingAdvice"></bean>
<bean id="loggingAfterThrowingAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" p:advice-ref="loggingAfterThrowingAdvice" p:pattern=".*"></bean>
<bean id ="helloworldbean6" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="autodetectInterfaces" value="true"/>
<property name="interceptorNames">
<list>
<idref local="loggingAfterThrowingAdvisor"/>
<idref local="helloworldbeanTarget3"/>
</list>
</property>
</bean>
<!--===================================================
=
= AroundAdvice 环绕通知
============ ========================================-->
<bean id="loggingAroundAdvice" class="cn.aop.advice.LoggingAroundAdvice"></bean>
<bean id="loggingAroundAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" p:advice-ref="loggingAroundAdvice" p:pattern=".*"></bean>
<bean id ="helloworldbean7" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="autodetectInterfaces" value="true"/>
<property name="interceptorNames">
<list>
<idref local="loggingAroundAdvisor"/>
<idref local="helloworldbeanTarget"/>
</list>
</property>
</bean>
</beans>
二、通知实例
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.MethodBeforeAdvice;
/**
* 前置通知
* @author wql
*
*/
public class LoggingBeforeAdvice implements MethodBeforeAdvice {
protected static final Log log = LogFactory.getLog(LoggingBeforeAdvice.class);
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
// TODO Auto-generated method stub
log.info(method+","+args[0]+","+target);
log.info("before: The Invocation of getContext()");
}
}
package cn.aop.advice;
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.AfterReturningAdvice;
/**
* 后置通知
* @author wql
*
*/
public class LogginAfterReturningAdvice implements AfterReturningAdvice {
protected static final Log log = LogFactory.getLog(LogginAfterReturningAdvice.class);
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
// TODO Auto-generated method stub
log.info(returnValue);
log.info(method+","+args[0]+","+target);
log.info("after: The Invocation of getContext()");
}
}
package cn.aop.advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 环绕通知
* @author wql
*
*/
public class LoggingAroundAdvice implements MethodInterceptor {
protected static final Log log = LogFactory.getLog(LoggingAroundAdvice.class);
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
log.info("befor: The Invocation of getContext()");
log.info(invocation);
invocation.getArguments()[0] = "world heart";
Object returnValue = invocation.proceed();
log.info("after: The Invocation of getContext()");
return returnValue;
}
}
package cn.aop.advice;
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.ThrowsAdvice;
/**
* 异常通知
* @author wql
*
*/
public class LoggingAfterThrowingAdvice implements ThrowsAdvice {
protected static final Log log = LogFactory.getLog(LoggingAfterThrowingAdvice.class);
public void afterThrowing(Method method,Object[] args, Object target,Throwable subClass) {
// TODO Auto-generated method stub
log.info(method+","+args[0]+","+target);
log.info(subClass);
}
}
三、主函数
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.aop.bean.HelloWorldAOPBean2;
import cn.aop.itf.IHelloWorldAOP;
public class AOPTestMain {
private static Log log =LogFactory.getLog(AOPTestMain.class);
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ListableBeanFactory factory = new ClassPathXmlApplicationContext("aop.xml");
GenericBeanFactoryAccessor gbf = new GenericBeanFactoryAccessor(factory);
log.info("=========================BeforeAdvice=============================");
IHelloWorldAOP hw1 = gbf.getBean("helloworldbean1");
IHelloWorldAOP hw2 = gbf.getBean("helloworldbean2");
HelloWorldAOPBean2 hw3 = gbf.getBean("helloworldbean3");
HelloWorldAOPBean2 hw4= gbf.getBean("helloworldbean4");
log.info("=======================指定接口AOPBean 指定target属性-==========================");
log.info(hw1.getContext("helloworld1"));
log.info("=======================自动检测接口AOPBean 指定 target属性-==========================");
log.info(hw2.getContext("helloworld2"));
log.info("=======================无接口AOPBean 指定target属性-==========================");
log.info(hw3.getContext("helloworld3"));
log.info("=======================无接口AOPBean 不指定 target属性-==========================");
log.info(hw4.getContext("helloworld4"));
log.info("=========================AfterReturningAdvice=============================");
IHelloWorldAOP hw5= gbf.getBean("helloworldbean5");
log.info("=======================自动检测接口AOPBean 不指定 target属性-==========================");
log.info(hw5.getContext("helloworld5"));
log.info("=========================AfterThrowingAdvice=============================");
IHelloWorldAOP hw6= gbf.getBean("helloworldbean6");
log.info("=======================自动检测接口AOPBean 不指定 target属性-==========================");
log.info(hw6.getContext("helloworld6"));
log.info("=========================AroundAdvice=============================");
IHelloWorldAOP hw7= gbf.getBean("helloworldbean7");
log.info("=======================自动检测接口AOPBean 不指定 target属性-==========================");
log.info(hw7.getContext("helloworld7"));
}
}
四、输出
AOPTestMain.java:29 -180 [main] - =======================指定接口AOPBean 指定target属性-==========================
LoggingBeforeAdvice.java:15 -182 [main] - public abstract java.lang.String cn.aop.itf.IHelloWorldAOP.getContext(java.lang.String,helloworld1,cn.aop.bean.HelloWorldAOPBean@16bd8ea
LoggingBeforeAdvice.java:16 -182 [main] - before: The Invocation of getContext(
AOPTestMain.java:30 -182 [main] - helloworld1
AOPTestMain.java:31 -182 [main] - =======================自动检测接口AOPBean 指定 target属性-==========================
LoggingBeforeAdvice.java:15 -183 [main] - public abstract java.lang.String cn.aop.itf.IHelloWorldAOP.getContext(java.lang.String,helloworld2,cn.aop.bean.HelloWorldAOPBean@16bd8ea
LoggingBeforeAdvice.java:16 -183 [main] - before: The Invocation of getContext(
AOPTestMain.java:32 -183 [main] - helloworld2
AOPTestMain.java:33 -183 [main] - =======================无接口AOPBean 指定target属性-==========================
LoggingBeforeAdvice.java:15 -185 [main] - public java.lang.String cn.aop.bean.HelloWorldAOPBean2.getContext(java.lang.String,helloworld3,cn.aop.bean.HelloWorldAOPBean2@19360e2
LoggingBeforeAdvice.java:16 -185 [main] - before: The Invocation of getContext(
AOPTestMain.java:34 -186 [main] - helloworld3
AOPTestMain.java:35 -186 [main] - =======================无接口AOPBean 不指定 target属性-==========================
LoggingBeforeAdvice.java:15 -186 [main] - public java.lang.String cn.aop.bean.HelloWorldAOPBean2.getContext(java.lang.String,helloworld4,cn.aop.bean.HelloWorldAOPBean2@19360e2
LoggingBeforeAdvice.java:16 -186 [main] - before: The Invocation of getContext(
AOPTestMain.java:36 -186 [main] - helloworld4
AOPTestMain.java:38 -186 [main] - =========================AfterReturningAdvice=============================
AOPTestMain.java:40 -187 [main] - =======================自动检测接口AOPBean 不指定 target属性-==========================
LoggingBeforeAdvice.java:15 -188 [main] - public abstract java.lang.String cn.aop.itf.IHelloWorldAOP.getContext(java.lang.String,helloworld5,cn.aop.bean.HelloWorldAOPBean@16bd8ea
LoggingBeforeAdvice.java:16 -188 [main] - before: The Invocation of getContext(
LogginAfterReturningAdvice.java:17 -188 [main] - helloworld5
LogginAfterReturningAdvice.java:18 -188 [main] - public abstract java.lang.String cn.aop.itf.IHelloWorldAOP.getContext(java.lang.String,helloworld5,cn.aop.bean.HelloWorldAOPBean@16bd8ea
LogginAfterReturningAdvice.java:19 -188 [main] - after: The Invocation of getContext(
AOPTestMain.java:41 -189 [main] - helloworld5
AOPTestMain.java:43 -189 [main] - =========================AfterThrowingAdvice=============================
AOPTestMain.java:45 -189 [main] - =======================自动检测接口AOPBean 不指定 target属性-==========================
AOPTestMain.java:46 -190 [main] - helloworld6
AOPTestMain.java:48 -190 [main] - =========================AroundAdvice=============================
AOPTestMain.java:50 -190 [main] - =======================自动检测接口AOPBean 不指定 target属性-==========================
LoggingAroundAdvice.java:13 -191 [main] - befor: The Invocation of getContext(
LoggingAroundAdvice.java:14 -191 [main] - ReflectiveMethodInvocation: public abstract java.lang.String cn.aop.itf.IHelloWorldAOP.getContext(java.lang.String; target is of class [cn.aop.bean.HelloWorldAOPBean]
LoggingAroundAdvice.java:17 -191 [main] - after: The Invocation of getContext(
AOPTestMain.java:51 -191 [main] - world heart