http://joe5456536.blog.163.com/blog/static/8537477320112119372998/
ApplicationContext.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="helloWorld" class="com.demo.spring.test.HelloWorld"/>
<bean id="myBeforeAdvice" class="com.demo.spring.aop.MyBeforeAdvice"/>
<bean id="myAroundAdvice" class="com.demo.spring.aop.MyAroundAdvice"/>
<bean id="myThrowsAdvice" class="com.demo.spring.aop.MyThrowsAdvice"/>
<bean id="myAfterAdvice" class="com.demo.spring.aop.MyAfterAdvice"/>
<bean id="myBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns">
<list>
<value>com\.demo\.spring\.test\.IHelloWorld\.execute</value>
</list>
</property>
<property name="advice">
<ref local="myBeforeAdvice" />
</property>
</bean>
<bean id="myAroundAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns">
<list>
<value>com\.demo\.spring\.test\.IHelloWorld\.execute</value>
</list>
</property>
<property name="advice">
<ref local="myAroundAdvice" />
</property>
</bean>
<bean id="myThrowsAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns">
<list>
<value>com\.demo\.spring\.test\.IHelloWorld\.execute</value>
</list>
</property>
<property name="advice">
<ref local="myThrowsAdvice" />
</property>
</bean>
<bean id="myAfterAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns">
<list>
<value>com\.demo\.spring\.test\.IHelloWorld\.execute</value>
</list>
</property>
<property name="advice">
<ref local="myAfterAdvice" />
</property>
</bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.demo.spring.test.IHelloWorld"/>
<property name="target" ref="helloWorld"/>
<property name="interceptorNames">
<list>
<value>myBeforeAdvisor</value>
<value>myAfterAdvisor</value>
<value>myAroundAdvisor</value>
<value>myThrowsAdvisor</value>
</list>
</property>
</bean>
</beans>Spring 里的通知类
前置通知:
package com.demo.spring.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;public class MyBeforeAdvice implements MethodBeforeAdvice {
public void before(Method arg0, Object[] args, Object target) throws Throwable {
System.out.println("方法名:" + arg0.getName());
System.out.println("参数个数:" + arg1.length);
System.out.println("目标类名:" + target);
System.out.println("Start to say:"); }
}
环绕通知:
package com.demo.spring.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;public class MyAroundAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("正在执行!");
System.out.println("代理类名:" + arg0.getClass().getName());
System.out.println("目标类名:" + arg0.getThis().getClass().getName());
System.out.println("参数个数:" + arg0.getArguments().length);
System.out.println("目标方法名:" + arg0.getMethod());
return arg0.proceed();
}
}返回后通知:
package com.demo.spring.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;public class MyAfterAdvice implements AfterReturningAdvice {
public void afterReturning(Object returnVal, Method arg1, Object[] args, Object target) throws Throwable {
System.out.println("END!");
}
}异常通知:
package com.demo.spring.aop;
import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Method m, Object[] args, Object target, Exception ex) throws Throwable{
System.out.println("有异常抛出!");
}
}接口类:
package com.demo.spring.test;
public interface
public void execute(String name) throws Exception;
}接口实现类:
package com.demo.spring.test;
import java.io.IOException;
public class HelloWorld implements IHelloWorld {
public void execute(String name) throws Exception{
System.out.println("Hello World!");
//if(true) throw new IOException();
}}
测试类:
package com.demo.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext("/applicationContext.xml");
IHelloWorld hello = (IHelloWorld) ctx.getBean("proxy");
try{
hello.execute("JOE");
}catch(Exception e){
e.printStackTrace();
}
}
}