方法一
这是我认为相对简单好理解的一种方法
话不多说直接上码
UserInterface接口
package henu.rjxy.myinterface;
public interface UserInterface {
public void smile();
}
User继承UserInterface接口
package henu.rjxy.bean;
import henu.rjxy.myinterface.UserInterface;
public class User implements UserInterface {
@Override
public void smile() {
// TODO Auto-generated method stub
System.out.println("user smile");
}
}
BeforeAdvisor继承MethodBeforeAdvice接口
实现接口MethodBeforeAdvice该拦截器会在调用方法前执行
实现接口AfterReturningAdvice该拦截器会在调用方法后执行
实现接口MethodInterceptor该拦截器会在调用方法前后都执行
package henu.rjxy.aspect;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvisor implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] obj, Object args)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("Smile......");
}
}
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.xsd">
<bean id="beforadvisor" class="henu.rjxy.aspect.BeforeAdvisor"/>
<bean id="user" class="henu.rjxy.bean.User"/>
<bean id="userProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 代理的目标对象,即想要在哪个类上增加代理 -->
<property name="target" ref="user"/>
<!-- 代理类 -->
<property name="interceptorNames">
<value>beforadvisor</value>
</property>
<!-- 代理类所需要的接口 -->
<property name="proxyInterfaces">
<value>henu.rjxy.myinterface.UserInterface</value>
</property>
</bean>
</beans>
JUnit中测试
package henu.rjxy.test;
import static org.junit.Assert.*;
import henu.rjxy.myinterface.UserInterface;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void test() {
ApplicationContext act = new ClassPathXmlApplicationContext("application.xml");
UserInterface uProxy = (UserInterface)act.getBean("userProxy");
uProxy.smile();
}
}
测试效果
方法二
动态Proxy代理接口
UserInterface接口有一个say()方法,
User类继承UserInterface接口,并实现了say()方法.
JDKProxy类继承了InvocationHandler接口并实现了其中的invoke方法
package henu.rjxy.proxy;
import henu.rjxy.myinterface.UserInterface;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JDKProxy implements InvocationHandler {
private UserInterface user;
public void setUser(UserInterface user) {
this.user = user;
}
//Proxy.newProxyInstance(a,b,c);
//a:指明生成代理对象使用哪个类加载器 对象名.getClass().getClassLoader()
//b:指明要生成哪个对象的代理对象 对象名.getClass().getInterfaces() || new Class[]{类名 .class}
//c:指明产生这个对象要做什么事情 this代指本对象 || JDKProxy
public Object createProxy(){
return Proxy.newProxyInstance(user.getClass().getClassLoader(), user.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("我是user的代理人,找user请先联系我");
Object obj = method.invoke(user, args);
System.out.println("我是user的代理人,找user请先联系我");
return obj;
}
}
JUnit测试
package henu.rjxy.test;
import henu.rjxy.bean.User;
import henu.rjxy.myinterface.UserInterface;
import henu.rjxy.proxy.JDKProxy;
public class Test {
@org.junit.Test
public void test() {
UserInterface user = new User();
JDKProxy proxy = new JDKProxy();
proxy.setUser(user);
UserInterface proxyUser = (UserInterface)proxy.createProxy();
proxyUser.say();
}
}
运行结果: