首页>代码>spring框架AOP编程Demo>/com.springAOP.test/src/main/java/com/yyc/a_proxy/a_jdk/MyBeanFactory.java
package com.yyc.a_proxy.a_jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyBeanFactory {

	public static UserService createService() {
		//1.目标类
		final UserService userService = new UserServiceImpl();
		//2.切面类
		final MyAspect myAspect = new MyAspect();
		/*
		 * 3.代理类:将目标类(切入点)和切面类(通知)结合--->切面
		 * Proxy.newProxyInstance
		 * 		参数1:loader,类加载器,动态代理类,运行时创建,任何类都需要类加载器将其加载到内存。
		 * 			  一般情况:当前类.class.getClassLoader();
		 * 					目标类实类.getClass().getClassLoader();
		 * 		参数2:interfaces代理类需要实现的所有接口
		 * 			 一般情况:方式1:目标类实类.getClass().getInterfaces(); 注意:只获得自己的接口,不能获得父类元素接口
		 * 					方式2:new Class[] {UserService.class}
		 * 		参数3:InvocationHandle  处理类,接口,必须进行实现类,一般采用匿名内部类
		 * 			 	提供invoke方法,代理类的每一个方法执行时,都将调用一次invoke
		 * 					参数3.1:Object proxy:代理对象
		 * 					参数3.2:Method method:代理对象当前执行的方法的描述对象(反射)
		 * 							执行方法名:method.getName();
		 * 							执行方法:method.invoke(对象,实际参数)
		 * 					参数3.3:Object[] args:方法实际参数
		 */
		UserService proxyService = (UserService) Proxy.newProxyInstance(
								   MyBeanFactory.class.getClassLoader(), 
								   userService.getClass().getInterfaces(), 
								   new InvocationHandler() {
									public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
										//前执行
										myAspect.before();
										//执行目标类方法
										Object obj = method.invoke(userService, args);
										//后执行
										myAspect.after();
										return obj;
									}
								});
		return proxyService;
	}
}
最近下载更多
1358849392  LV21 1月22日
猴子蹲蹲  LV2 2023年4月4日
2687772871  LV1 2020年8月29日
762879408  LV2 2020年3月26日
miguel109  LV2 2020年2月1日
liuwenlong  LV20 2019年12月28日
荒唐的羊  LV27 2019年12月12日
zhenghongixin4065  LV9 2019年10月19日
tuhao888  LV9 2019年9月3日
Rommel  LV27 2019年8月29日
最近浏览更多
1358849392  LV21 1月22日
xuthus  LV1 2023年6月29日
猴子蹲蹲  LV2 2023年4月4日
女王不该在山炮村养花  LV8 2023年4月4日
00044304  LV6 2022年8月24日
namiosjdij 2021年12月29日
暂无贡献等级
SunshineOuO  LV8 2021年8月18日
denliv_hui  LV13 2021年6月21日
 LV1 2021年5月12日
wennie001002  LV1 2020年12月25日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友