package com.zj;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(final Object arg0, String arg1)
throws BeansException {
System.out.println("after");
return Proxy.newProxyInstance(MyBeanPostProcessor.class.getClassLoader(),
arg0.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("调用之前");
Object obj=method.invoke(arg0, args);
System.out.println("调用之后");
return obj;
}
});
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("before");
return arg0;
}
}
invoke的使用
最新推荐文章于 2023-06-27 13:23:49 发布