1、创建要被代理的类及其要实现的接口
package com . fang . calculate ;
public interface Calculator { / / 接口
public int add ( int i , int j ) ;
public int sub ( int i , int j ) ; / / subtraction 减法
public int div ( int i , int j ) ; / / division 除法
public int mul ( int i , int j ) ; / / multiplication 乘法
}
package com . fang . imp ;
import com . fang . calculate . Calculator ;
public class ImpCalculator implements Calculator { / / 上面接口的实现类
@ Override
public int add ( int i , int j ) {
int result = i + j ;
return result ;
}
@ Override
public int sub ( int i , int j ) {
int result = i-j ;
return result ;
}
@ Override
public int div ( int i , int j ) {
int result = i / j ;
return result ;
}
@ Override
public int mul ( int i , int j ) {
int result = i * j ;
return result ;
}
}
2、创建对应的动态代理类
package com . fang . proxy ;
import java . lang . reflect . InvocationHandler ;
import java . lang . reflect . Method ;
import java . lang . reflect . Proxy ;
import java . util . Arrays ;
import com . fang . calculate . Calculator ;
import com . fang . imp . ImpCalculator ;
import com . fang . log . LogUtils ;
public class CalculatorProxy {
public static Calculator getProxy ( final Calculator calculator ) {
/ / 方法执行器,帮我们执行目标对象要执行的方法
InvocationHandler h = new InvocationHandler ( ) {
/ * * Object proxy:代理对象,给JDK使用,不需要改变
* Method method:要执行的目标对象的方法
* Object [ ] args:方法调用时传入的参数
* * * /
@ Override
public Object invoke ( Object proxy , Method method , Object [ ] args ) throws Throwable {
Object result = null ;
try {
LogUtils . logStart ( method , args ) ; / / 自己创建的日志记录类
/ / 利用反射执行目标方法
result = method . invoke ( calculator , args ) ;
LogUtils . logReturn ( method , result ) ; / / 自己创建的日志记录类
} catch ( Exception e ) {
/ / 调用e . getCause ( ) 显示异常类型
LogUtils . errorss ( e ) ; / / 自己创建的日志记录类
} finally {
System . out . println ( "执行结束" ) ;
}
return result ;
}
} ;
/ * *
* * * /
ClassLoader loader = calculator . getClass ( ) . getClassLoader ( ) ;
Class < ? > [ ] interfaces = calculator . getClass ( ) . getInterfaces ( ) ;
Object proxy = Proxy . newProxyInstance ( loader , interfaces , h ) ;
return ( Calculator ) proxy ;
}
}
3、再创建对应的日志记录类
package com . fang . log ;
import java . lang . reflect . Method ;
import java . util . Arrays ;
public class LogUtils {
public static void logStart ( Method method , Object [ ] args ) {
System . out . println ( method . getName ( ) + "方法开始执行," + "参数是:" + Arrays . asList ( args ) ) ;
}
public static void logReturn ( Method method , Object result ) {
System . out . println ( method . getName ( ) + "方法结束执行," + "结果是:" + result ) ;
}
public static void errorss ( Exception e ) {
System . out . println ( "有异常产生:" + e . getCause ( ) ) ;
}
}
4、进行测试
package com . fang . test ;
import org . junit . Test ;
import org . springframework . context . ApplicationContext ;
import org . springframework . context . support . ClassPathXmlApplicationContext ;
import com . fang . calculate . Calculator ;
import com . fang . imp . ImpCalculator ;
import com . fang . proxy . CalculatorProxy ;
public class IocTest {
@ Test
public void test ( ) {
Calculator calculator = new ImpCalculator ( ) ;
Calculator proxy = CalculatorProxy . getProxy ( calculator ) ; / / 使用接口
proxy . add ( 2 , 1 ) ;
proxy . div ( 2 , 0 ) ;
}
}