public class X{
public void sayHello(){
syso:say hello;
}
}
public class XProxy
{
private X x;
public void sayHello
{
startTime:
x.syHello();
endTime;
}
}
package study.javaenhance;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;
public class ProxyTest
{
public static void main(String[] args)
{
Class clazzProxy1 = Proxy.getProxyClass(Collection.class.getClassLoader(),Collection.class);
System.out.println(clazzProxy1.getName());
//上面输出的为一个类,那么一个类肯定有其构造方法和方法,下面我们来列出来
System.out.println("----------begin constructors list----------");
//1.列出构造方法
Constructor[] constructors = clazzProxy1.getConstructors();
for (Constructor constructor : constructors)
{
String name = constructor.getName();
StringBuilder sBuilder = new StringBuilder(name);
sBuilder.append('(');
Class[] clazzParams = constructor.getParameterTypes();
for (Class clazzParam : clazzParams) {
sBuilder.append(clazzParam.getName()).append(',');
}
if(clazzParams!=null && clazzParams.length != 0)
sBuilder.deleteCharAt(sBuilder.length()-1);
sBuilder.append(')');
System.out.println(sBuilder.toString());
}
//2.列出这个类字节码中的所有方法
System.out.println("----------begin methods list----------");
Method[] methods = clazzProxy1.getMethods();
for(Method method : methods){
String name = method.getName();
StringBuilder sBuilder = new StringBuilder(name);
sBuilder.append('(');
Class[] clazzParams = method.getParameterTypes();
for(Class clazzParam : clazzParams){
sBuilder.append(clazzParam.getName()).append(',');
}
if(clazzParams!=null && clazzParams.length != 0)
sBuilder.deleteCharAt(sBuilder.length()-1);
sBuilder.append(')');
System.out.println(sBuilder.toString());
}
}
}
package study.javaenhance;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class MyInvocationHandler implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
return null;
}
}
//3.创建实例对象
System.out.println("----------begin create instance object----------");
Constructor constructor = clazzProxy1.getConstructor(InvocationHandler.class);
MyInvocationHandler myInvocationHandler = new MyInvocationHandler();
Collection collection = (Collection) constructor.newInstance(myInvocationHandler);
System.out.println(collection);
collection.clear();
//collection.size(); //报错,异常会发生,产生异常的原因在于其返回值为int类型,但是每一次调用一个方法都会调用到invoke方法,我们此时的invoke返回的为null,所以是没有办法转换为int类型的。
//3.1 采用匿名内部类方式进行创建
Collection collection2 = (Collection) constructor.newInstance(new InvocationHandler()
{
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
return null;
}
});
System.out.println(collection2);
collection2.clear();
//collection2.size();
//3.3 采用Proxy 中提供的简单方法创建
Collection collection3 = (Collection)Proxy.newProxyInstance(Collection.class.getClassLoader(),
new Class[]{Collection.class},
new InvocationHandler()
{
private ArrayList target = new ArrayList();
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return method.invoke(target, args);
}
});
//System.out.println(collection3);
collection3.add("abc");
collection3.add("def");
collection3.add("hij");
System.out.println(collection3.size());
package study.javaenhance;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
public class ProxyTest
{
public static void main(String[] args) throws Exception
{
Class clazzProxy1 = Proxy.getProxyClass(Collection.class.getClassLoader(),Collection.class);
System.out.println(clazzProxy1.getName());
//上面输出的为一个类,那么一个类肯定有其构造方法和方法,下面我们来列出来
System.out.println("----------begin constructors list----------");
//1.列出构造方法
Constructor[] constructors = clazzProxy1.getConstructors();
for (Constructor constructor : constructors)
{
String name = constructor.getName();
StringBuilder sBuilder = new StringBuilder(name);
sBuilder.append('(');
Class[] clazzParams = constructor.getParameterTypes();
for (Class clazzParam : clazzParams) {
sBuilder.append(clazzParam.getName()).append(',');
}
if(clazzParams!=null && clazzParams.length != 0)
sBuilder.deleteCharAt(sBuilder.length()-1);
sBuilder.append(')');
System.out.println(sBuilder.toString());
}
//2.列出这个类字节码中的所有方法
System.out.println("----------begin methods list----------");
Method[] methods = clazzProxy1.getMethods();
for(Method method : methods){
String name = method.getName();
StringBuilder sBuilder = new StringBuilder(name);
sBuilder.append('(');
Class[] clazzParams = method.getParameterTypes();
for(Class clazzParam : clazzParams){
sBuilder.append(clazzParam.getName()).append(',');
}
if(clazzParams!=null && clazzParams.length != 0)
sBuilder.deleteCharAt(sBuilder.length()-1);
sBuilder.append(')');
System.out.println(sBuilder.toString());
}
//3.创建实例对象
System.out.println("----------begin create instance object----------");
Constructor constructor = clazzProxy1.getConstructor(InvocationHandler.class);
MyInvocationHandler myInvocationHandler = new MyInvocationHandler();
Collection collection = (Collection) constructor.newInstance(myInvocationHandler);
System.out.println(collection);
collection.clear();
//collection.size(); //报错,异常会发生,产生异常的原因在于其返回值为int类型,但是每一次调用一个方法都会调用到invoke方法,我们此时的invoke返回的为null,所以是没有办法转换为int类型的。
//3.1 采用匿名内部类方式进行创建
Collection collection2 = (Collection) constructor.newInstance(new InvocationHandler()
{
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
return null;
}
});
System.out.println(collection2);
collection2.clear();
//collection2.size();
//3.3 采用Proxy 中提供的简单方法创建
Collection collection3 = (Collection)Proxy.newProxyInstance(Collection.class.getClassLoader(),
new Class[]{Collection.class},
new InvocationHandler()
{
private ArrayList target = new ArrayList();
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return method.invoke(target, args);
}
});
//System.out.println(collection3);
collection3.add("abc");
collection3.add("def");
collection3.add("hij");
System.out.println(collection3.size());
System.out.println(collection3.getClass().getName());//这个返回的是
System.out.println("----------begin create instance object 抽化----------");
//3.4抽出动态代理让目标对象和切面对象都是传入进去的
final ArrayList target = new ArrayList();
Collection collection4 = (Collection)getProxy(target,new MyAdvice());
collection4.add("test1");
collection4.add("test2");
System.out.println(collection4.size());
}
private static Object getProxy(final Object target,final Advice advice) {
Object object = Proxy.newProxyInstance(target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler()
{
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
advice.beforeMethod(method);
Object retValue = method.invoke(target, args);
advice.afterMethod(method);
return retValue;
}
});
return object;
}
}
package study.javaenhance.aopframework;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import study.javaenhance.Advice;
public class BeanFactory
{
private Properties properties = new Properties();
public BeanFactory(InputStream inStream)
{
try
{
properties.load(inStream);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if(inStream != null)
{
try
{
inStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
public Object getBean(String name)
{
String className = properties.getProperty(name);
Object bean = null;
try
{
Class clazz = Class.forName(className);
bean = clazz.newInstance();
if(bean instanceof ProxyFactoryBean)
{
ProxyFactoryBean proxyBean = (ProxyFactoryBean) bean;
Advice advice = (Advice)Class.forName(properties.getProperty(name + ".advice")).newInstance();
Object target = Class.forName(properties.getProperty(name + ".target")).newInstance();
proxyBean.setAdvice(advice);
proxyBean.setTarget(target);
Object proxy = proxyBean.getProxy();
return proxy;
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return bean;
}
}
package study.javaenhance.aopframework;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import study.javaenhance.Advice;
public class ProxyFactoryBean
{
private Object target;
private Advice advice;
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
public Advice getAdvice() {
return advice;
}
public void setAdvice(Advice advice) {
this.advice = advice;
}
public Object getProxy()
{
Object object = Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),
new InvocationHandler()
{
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
advice.beforeMethod(method);
Object retValue = method.invoke(target, args);
advice.afterMethod(method);
return retValue;
}
});
return object;
}
}
package study.javaenhance.aopframework;
import java.io.InputStream;
import java.util.Collection;
public class AopFrameworkTest
{
public static void main(String[] args)
{
InputStream ips = AopFrameworkTest.class.getResourceAsStream("config.properties");
Object bean = new BeanFactory(ips).getBean("xxx");
System.out.println(bean.getClass().getName());
((Collection)bean).clear();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有