代理模式:
AOP的作用:不修改源码的情况下,程序运行期间对方法进行功能增强
AOP的实现机制:动态代理
代理模式:在一个原有功能的基础上添加新的功能。
分类:
静态代理:要求代理类存在
动态代理:可以在程序运行时,根据被代理对象动态生成代理对象.
传统的方式:核心代码和服务代码写在一起:
try {
System.out.println("开启事务");
System.out.println("执行核心业务代码-------");
System.out.println("提交事务");
} catch (Exception e) {
e.printStackTrace();
System.out.println("回滚事务");
} finally {
}
1.静态代理:
1.1 基于类的静态代理:
要求: 继承被代理的对象.
缺点:代理类只能代理一个类
public class ProxyStudentService extends StudentService { public void main(String[] args) { try { System.out.println("开启事务"); super.coreCode(); System.out.println("提交事务"); } catch (Exception e) { e.printStackTrace(); System.out.println("回滚事务"); } finally { } } }
1.2基于接口的静态代理:
要求:代理类和被代理类实现同一个接口,代理类可以代理多个类,也可以有多个代理类(多重代理):
缺点: 代理对象和需要与目标对象实现一样的接口,会产生很多的代理类,类太多,一旦接口增加方法,代理对象和被代理对象都要维护.
接口:
public interface IService {
void coreCode(); //需要增强的方法
}
代理类:
public class TranProxyService implements IService {
private IService iService ;
public TranProxyService(IService iService) {
this.iService = iService;
}
@Override
public void coreCode() {
try {
System.out.println("开启事务");
iService.coreCode();
System.out.println("提交事务");
} catch (Exception e) {
e.printStackTrace();
System.out.println("回滚事务");
} finally {
}
}
}
一个代理类支持多个被代理对象:
public class TeamService implements IService {
@Override
public void coreCode() {
System.out.println("teamService的核心代码----------");
}
}
public class TeacherService implements IService {
@Override
public void coreCode() {
System.out.println("这是teacherService 核心代码--------");
}
}
TeacherService teacherService=new TeacherService(); TranProxyService teachProxyService = new TranProxyService(teacherService); TranProxyService teamProxyService = new TranProxyService(teamService); teacherService.coreCode(); teamProxyService.coreCode();
多重代理:
TeamService target = new TeamService();
TranProxyService tranProxyService = new TranProxyService(target);
LogProxyService logProxyService =new LogProxyService(tranProxyService);
logProxyService.coreCode();
1.3 提取切面代码,作为AOP接口
public interface Aop {
void before();
void after();
void exception();
void finall();
}
public class LogAop implements Aop {
@Override
public void before() {
System.out.println("日志开始");
}
@Override
public void after() {
System.out.println("日志结束");
}
@Override
public void exception() {
System.out.println("日志异常");
}
@Override
public void finall() {
System.out.println("日志最终-------");
}
}
public class MyProxyService implements IService {
private IService iService;//被代理的对象
private Aop myAop;//切面对象
public MyProxyService(IService iService, LogAop logAop) {
this.iService = iService;
this.myAop = logAop;
}
@Override
public void coreCode() {
try {
myAop.before();
iService.coreCode();
myAop.after();
} catch (Exception e) {
myAop.exception();
e.printStackTrace();
} finally {
myAop.finall();
}
}
}
TeamService target = new TeamService(); //目标对象(被代理对象)
LogAop logAop = new LogAop(); //日志切面对象
LogProxyService logProxyService = new LogProxyService(target, logAop); //产生代理对象
logProxyService.coreCode(); //执行代理对象方法
2.动态代理:
2.1 基于jdk的动态代理:
final TeamService target = new TeamService(); //目标对象(被代理对象)
IService proxyTeamService = (IService) Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
System.out.println("before");
Object res = method.invoke(target, objects);//这里使用目标对象(被代理对象)
System.out.println("after");
return res;
}
});
proxyTeamService.coreCode();
基于jdk 动态代理的缺点:
目标对象需要实现一个或者多个接口
2.1 CGLib子类代理:
CGLIB代理,可以在运行期间,在内存中构建一个子类对象,从而实现对目标对象的功能扩展.
final TeamService target = new TeamService(); //目标对象(被代理对象)
TeamService proxyService = (TeamService) Enhancer.create(target.getClass(), new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("before");
Object invoke = methodProxy.invokeSuper(o, objects);
System.out.println("after");
return invoke;
}
});
proxyService.coreCode();