接口IUser
public interface IUser {
public void login();
public void logout();
}
接口IAdvice
public interface IAdvice {
public void beforeAdvice();
public void afterAdvice();
}
IUser接口实现类(UserImp类)
public class UserImp implements IUser {
@Override
public void login() {
System.out.println("登陆成功!");
}
@Override
public void logout() {
System.out.println("登出成功!");
}
}
IAdvice接口实现类(AdviceImp类)
public class AdviceImp implements IAdvice {
@Override
public void beforeAdvice() {
System.out.println("验证身份中");
}
@Override
public void afterAdvice() {
System.out.println("验证成功!");
}
}
动态代理类(ProxyFactory类)
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyFactory implements InvocationHandler {
private Object target;
private AdviceImp advice;
public Object getProxy(){
Class<?> tclass = target.getClass();
return Proxy.newProxyInstance(tclass.getClassLoader()
,tclass.getInterfaces()
,this);
}
public void setTarget(Object target) {
this.target = target;
}
public void setAdvice(Object advice) {
this.advice = (AdviceImp) advice;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
advice.beforeAdvice();
Object obj = method.invoke(target);
advice.afterAdvice();
return obj;
}
}
组装类(BeanFactory类)
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
public class BeanFactory {
private Properties prop=new Properties();
public BeanFactory(InputStream in)throws IOException {
prop.load(in);
}
public Object getBean(String name){
Object bean=null;
String classname = prop.getProperty(name);
try {
Class<?> aClass = Class.forName(classname);
Object target = Class.forName(prop.getProperty(name + ".target"))
.getDeclaredConstructor()
.newInstance();
Object advice = Class.forName(prop.getProperty(name + ".advice"))
.getDeclaredConstructor()
.newInstance();
bean = Class.forName(classname)
.getDeclaredConstructor()
.newInstance();
PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(Class.forName(classname)).getPropertyDescriptors();
for(PropertyDescriptor pd:propertyDescriptors){
String propname = pd.getName();
Method writeMethod = pd.getWriteMethod();
if(propname.equals("target")){
writeMethod.invoke(bean,target);
}else if(propname.equals("advice")){
writeMethod.invoke(bean,advice);
}
}
}catch (ClassNotFoundException|NoSuchMethodException e){
e.printStackTrace();
}catch ( InvocationTargetException|IllegalAccessException e){
e.printStackTrace();
}catch (InstantiationException| IntrospectionException e){
e.printStackTrace();
}
return bean;
}
}
属性文件(config.properties)
bean.advice=aop.AdviceImp
bean.target=aop.UserImp
bean=aop.ProxyFactory
测试类(AopTest类)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class AopTest {
public static void main(String[] args) {
try {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("aop/config.properties");
BeanFactory beanFactory=new BeanFactory(in);
ProxyFactory bean=(ProxyFactory) beanFactory.getBean("bean");
IUser proxy = (IUser) bean.getProxy();
proxy.login();
proxy.logout();
}catch (IOException e){
e.printStackTrace();
}
}
}
结果
"C:\Program Files\Java\jdk-9.0.4\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\lib\idea_rt.jar=6057:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\bin" -Dfile.encoding=UTF-8 -classpath "D:\java\test\bin;D:\java\test\libs\guava-27.0-jre.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\lib\junit.jar;D:\java\test\libs\apache-mina-2.0.20\dist\mina-core-2.0.20.jar;D:\java\test\libs\apache-mina-2.0.20\lib\slf4j-api-1.7.26.jar" aop.AopTest
验证身份中
登陆成功!
验证成功!
验证身份中
登出成功!
验证成功!