-
AOP面向切面编程
查看全部 -
责任链模式
查看全部 -
Spring 创建 AOP 代理类时序图
查看全部 -
代理模式。
查看全部 -
AOP代理
查看全部 -
package com.imooc.chain;
Chain与ChainHandler之间的函数回调设计,遍历Chain中的ChainHandler执行相关方法/**
* Created by cat on 2017-02-28.
*/
public abstract class ChainHandler {
public void execute(Chain chain){
handleProcess();
chain.proceed();
}
protected abstract void handleProcess();
}
package com.imooc.chain;
import java.util.List;
/**
* Created by cat on 2017-02-28.
*/
public class Chain {
private List<ChainHandler> handlers;
private int index = 0;
public Chain(List<ChainHandler> handlers) {
this.handlers = handlers;
}
public void proceed(){
if(index >= handlers.size()){
return ;
}
handlers.get(index++).execute(this);
}
}
/**
* Created by cat on 2017-02-28.
*/
public class ChainClient {
static class ChainHandlerA extends ChainHandler{
@Override
protected void handleProcess() {
System.out.println("handle by chain a");
}
}
static class ChainHandlerB extends ChainHandler{
@Override
protected void handleProcess() {
System.out.println("handle by chain b");
}
}
static class ChainHandlerC extends ChainHandler{
@Override
protected void handleProcess() {
System.out.println("handle by chain c");
}
}
public static void main(String[] args){
List<ChainHandler> handlers = Arrays.asList(
new ChainHandlerA(),
new ChainHandlerB(),
new ChainHandlerC()
);
Chain chain = new Chain(handlers);
chain.proceed();
}
}
查看全部 -
责任链的调用
/**
* Created by cat on 2017-02-28.
*/
public abstract class Handler {
private Handler sucessor;
public Handler getSucessor() {
return sucessor;
}
public void setSucessor(Handler sucessor) {
this.sucessor = sucessor;
}
public void execute(){
handleProcess();
if(sucessor != null){
sucessor.execute();
}
}
protected abstract void handleProcess();
}
package com.imooc.chain;
/**
* Created by cat on 2017-02-28.
*/
public class Client {
static class HandlerA extends Handler{
@Override
protected void handleProcess() {
System.out.println("handle by a");
}
}
static class HandlerB extends Handler{
@Override
protected void handleProcess() {
System.out.println("handle by b");
}
}
static class HandlerC extends Handler{
@Override
protected void handleProcess() {
System.out.println("handle by c");
}
}
public static void main(String[] args){
Handler handlerA = new HandlerA();
Handler handlerB = new HandlerB();
Handler handlerC = new HandlerC();
handlerA.setSucessor(handlerB);
handlerB.setSucessor(handlerC);
handlerA.execute();
}
}查看全部 -
责任链模式
查看全部 -
强制使用Cglib代理查看全部
-
springAOP的代理分情况使用jdk或者cglib的动态代理。查看全部
-
Spring如何选择使用jdk代理还是cglib代理?
1、如果目标对象实现了接口,默认使用JDK代理
2、如果目标对象没有实现接口,默认使用cglib代理
3、如果目标对象实现了接口,但我们可以强制使用cglib代理,方法如下:
@SpringBootApplication @EnableAspectJAutoProxy(proxyTargetClass = true)
查看全部 -
Spring 如何代理
查看全部 -
JDK与Cglib代理对比: 1:JDK只能针对有接口的类的接口方法进行动态代理; 2:Cglib基于继承来实现代理,无法对static、final类进行代理; 3:Cglib基于继承来实现代理,无法对private、static方法进行代理。
查看全部 -
public class Client {<br/><br/> public static void main(String[] args){<br/> Enhancer enhancer = new Enhancer();<br/> enhancer.setSuperclass(RealSubject.class);<br/> enhancer.setCallback(new DemoMethodInterceptor());<br/> Subject subject = (Subject) enhancer.create();<br/> subject.hello();<br/> }<br/>}查看全部
-
public class DemoMethodInterceptor implements MethodInterceptor{
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("before in cglib");
Object result = null;
try{
result = proxy.invokeSuper(obj, args);
}catch (Exception e){
System.out.println("get ex:"+e.getMessage());
throw e;
}finally {
System.out.println("after in cglib");
}
return result;
}
}查看全部
举报