beans.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 配置被代理的对象 -->
<bean id="testService" class="com.aop.TestService">
<property name="name" value="韩逸俊"/>
</bean>
<!-- 配置前置通知 -->
<bean id="MyMethodBeforeAdvice" class="com.aop.MyMethodBeforeAdvice"/>
<!-- 配置后知通知 -->
<bean id="MyAfterRetruningAdvice" class="com.aop.MyAfterRetruningAdvice"/>
<!-- 配置环绕童通知 -->
<bean id="MyMethodInterceptor" class="com.aop.MyMethodInterceptor"></bean>
<!-- 配置异常通知 -->
<bean id="MyThrowsAdvice" class="com.aop.MyThrowsAdvice"></bean>
<!-- 定义前置通知的切入点 -->
<bean id="MyMethodBeforeAdviceFilter" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="MyMethodBeforeAdvice"/>
<property name="mappedNames">
<list>
<value>sayHello</value>
</list>
</property>
</bean>
<!-- 配置代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 代理接口集 -->
<property name="proxyInterfaces">
<list>
<value>com.aop.TestServiceInter</value>
<value>com.aop.TestServiceInter2</value>
</list>
</property>
<!--把通知织入到代理对象 -->
<property name="interceptorNames">
<list>
<!-- 相当于把MyMethodBeforeAdvice前置通知和代理对象关联起来,我们也可以把通知看成拦截器 -->
<value>MyMethodBeforeAdviceFilter</value>
<!-- 织入后知通知 -->
<value>MyAfterRetruningAdvice</value>
<!-- 织入环绕通知 -->
<value>MyMethodInterceptor</value>
<!-- 织入异常通知 -->
<value>MyThrowsAdvice</value>
</list>
</property>
<!-- 配置被代理对象,可以指定 -->
<property name="target" ref="testService"/>
</bean>
</beans>
MyAfterRetruningAdvice:
package com.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class MyAfterRetruningAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
// TODO Auto-generated method stub
System.out.println("关闭资源...");
}
}
MyMethodBeforeAdvice:
package com.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
/**
* method:被调用的方法
* arg1:给method传递的参数
* arg2:目标对象
*/
@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
// TODO Auto-generated method stub
System.out.println("记录日志..."+arg0.getName());
}
}
MyMethodInterceptor:
package com.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
// TODO Auto-generated method stud
System.out.println("调用方法前。。。");
Object a=arg0.proceed();
System.out.println("调用方法后。。。");
return a;
}
}
MyThrowsAdvice:
package com.aop;
import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Method m,Object[] os,Object target,Exception e){
System.out.println("出事了"+e.getMessage());
}
}
TestServiceInter
package com.aop;
public interface TestServiceInter {
public void sayHello();
}
TestServiceInter2
package com.aop;
public interface TestServiceInter2 {
public void sayBye();
}
TestService
package com.aop;
public class TestService implements TestServiceInter,TestServiceInter2 {
private String name;
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("HI,"+name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void sayBye() {
// TODO Auto-generated method stub
System.out.println("Bye,"+name);
}
}
测试:
package com.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.inter.ChangeLetter;
public class App1 {
public static void main(String []args){
ApplicationContext ac=new ClassPathXmlApplicationContext("com/aop/beans.xml");
TestServiceInter t=(TestServiceInter) ac.getBean("proxyFactoryBean");
t.sayHello();
((TestServiceInter2)t).sayBye();
}
}