您的位置:首页 > 编程语言 > Java开发

一段Spring代码引起的调用绑定总结

2016-10-11 21:33 162 查看


代码

Java代码  


@Component  

public class B {  

    void test() {  

        System.out.println("hello");  

    }  

}  

Java代码  


@Component  

public class A {  

    @Autowired  

    private B b;  

    public final void test() {  

        b.test();  

    }  

}  

 

Java代码  


@Component  

@Aspect  

public class MyAspect {  

    @Before("execution(* *(..))")  

    public void before() {  

  

    }  

}  

 

Java代码  


@Configuration  

@ComponentScan  

@EnableAspectJAutoProxy(proxyTargetClass = true)  

public class Test {  

    public static void main(String[] args) {  

        AnnotationConfigApplicationContext ctx =  

                new AnnotationConfigApplicationContext(Test.class);  

        A a = ctx.getBean(A.class);  

        a.test();  

    }  

}  

 

问题 

1、A通过字段注入方式注入B ;

2、A的test方法是final的,因此该方法不能被代理;

3、被代理的对象的调用顺序:

    Proxy.test()

       --->Aspect Before/Around Advice

      ---->Target.test()

      ---->Aspect After/Around Advice

即当某个目标对象被代理后,我们首先调用代理对象的方法,其首先调用切面的前置增强/环绕增强,然后调用目标对象的方法,最后调用后置/环绕增强完成整个调用流程。

 

但是我们知道如果是基于CGLIB的代理:

final的类不能生成代理对象;因为final的类不能生成代理对象;

final的方法不能被代理;但是还是能生成代理对象的;

 

在我们的示例里,A的test方法是无法被代理的,但是A还是会生成一个代理对象(因为我们的切入点是execution(* *(..)),还是可以对如toString()之类的方法代理的):

 

即如果调用a.toString()相当于:

   proxy.toString() [com.github.zhangkaitao.A$$EnhancerByCGLIB$$73d79efe]

   ---->MyAspect.before() 

  ----->target.toString() [com.github.zhangkaitao.A]

 

但是如果调用a.test()相当于:

   proxy.test() [com.github.zhangkaitao.A$$EnhancerByCGLIB$$73d79efe]

 

 

当我们直接调用生成的代理对象的test方法。接着会得到空指针异常:

写道

Exception in thread "main" java.lang.NullPointerException

at com.github.zhangkaitao.A.test(A.java:16)

at com.github.zhangkaitao.Test.main(Test.java:21)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:601)

at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

从异常可以看出是A.test()方法中的b对象是空;

 

但是我们发现b对象是注入了,但是注入给的是目标对象,而代理对象是没有注入的,请看debug信息:



 

从上图可以看出,目标对象的b注入了;而生成的代理对象的b是没有值的;又因为我们调用“代理对象.final方法()”是属于编译期绑定,所以会抛出如上的空指针异常。也就是此问题还是因为对象与方法的绑定问题造成的。

 


调用绑定

所谓调用绑定,即当我们使用“对象.字段”/“对象.方法()”调用时,对象与字段/方法之间是如何绑定的;此处有两种绑定:编译期绑定与运行期绑定。

 

编译期绑定:对象与字段/方法之间的绑定关系发生在写代码期间(即编译期间),即它们的关系在编译期间(写完代码)就确定了,如:

Java代码  


public class StaticBindTest {  

    static class A {  

        public int i = 1;  

        public static void hello() {  

            System.out.println("1");  

        }  

    }  

    static class B extends A {  

        public int i = 2;  

        public static void hello() {  

            System.out.println("2");  

        }  

    }  

  

    public static void main(String[] args) {  

        A a = new B();  

        System.out.println(a.i);  

        a.hello();  

    }  

}  

如上代码将输出1,即A的i值,而不是B的i值;这就是所谓的编译期绑定,即访问的字段/方法绑定到声明类型上,而不是运行时的那个对象的类型上。

 

还有如:

Java代码  


public class StaticBindTest2 {  

    static class A {  

        public void hello(Number i) {  

            System.out.println("Number");  

        }  

        public void hello(Integer i) {  

            System.out.println("Integer");  

        }  

        public void hello(Long i) {  

            System.out.println("Long");  

        }  

    }  

    public static void main(String[] args) {  

        A a = new A();  

        Number i = Integer.valueOf(1);  

        Number l = Long.valueOf(1L);  

        a.hello(i);  

        a.hello(l);  

    }  

}  

都讲输出Number,而不是Integer和Long;这也是编译期绑定;即方法参数绑定时根据声明时的类型进行绑定也叫做静态绑定/早绑定。

 

如果我们使用“a.hello(null);”调用会发生什么情况呢?此时就会发生二义性,即绑定到Integer/Long参数上都可以的,所以我们应该使用“a.hello((Integer)null);”来强制调用。还有在绑定时都是先子类型(Integer/Long)到父类型(Number)进行绑定。

 

编译期绑定:调用的都是声明的类型的字段/方法或者根据参数声明时类型调用重载方法;静态字段/方法、private/final方法、实例对象的字段/重载方法都是编译期绑定,即除了方法覆盖都是编译期绑定;也可以说成除了运行期绑定之外的绑定都是编译期绑定。为什么这么说呢?接着往下看。

 

运行期绑定“对象.方法()”是根据程序运行期间对象的实际类型来绑定方法的,如:

Java代码  


public class DynamicBindTest {  

    static class A {  

        public void hello() {  

            System.out.println("a");  

        }  

    }  

    static class B extends A {  

        public void hello() {  

            System.out.println("b");  

        }  

    }  

  

    public static void main(String[] args) {  

        A a = new B();  

        a.hello();  

    }  

}  

如上代码将输出b,即说明了hello()方法调用不是根据声明时类型决定,而是根据运行期间的那个对象类型决定的;也叫做动态绑定/迟绑定。

 

运行期绑定:“对象.方法()”是根据运行期对象的实际类型决定的;即new的哪个对象就绑定该方法到那个对象类型上;只有方法覆盖是运行期绑定;其他都是编译期绑定;该机制用于实现多态。

 

在Java中,除了方法覆盖是运行期绑定,其他都是静态绑定就好理解了。

 


单分派与双分派

单分派:调用对象的方法是由对象的类型决定的;

多分派:调用对象的方法是由对象的类型决定的和其他因素(如方法参数类型)决定的;双分派是多分派的特例。

 

Java是一种单分派语言,可以通过如访问者设计模式来模拟多分派。

 

比如之前的重载的编译期绑定,和覆盖的运行期绑定,都是根据对象类型(不管是声明时类型/运行时类型)决定调用的哪个方法;跟方法参数实际运行时类型无关(而与声明时类型有关)。

 

接下来看一个双分派的例子:

Java代码  


public class DoubleDispatchTest {  

  

    static interface Element {  

        public void accept(Visitor v);  

    }  

    static class AElement implements Element {  

        public void accept(Visitor v) {  

            v.visit(this);  

        }  

    }  

    static class BElement implements Element {  

        public void accept(Visitor v) {  

            v.visit(this);  

        }  

    }  

  

    static interface Visitor {  

        public void visit(AElement aElement);  

        public void visit(BElement bElement);  

    }  

  

    static class Visitor1 implements Visitor {  

        public void visit(AElement aElement) {  

            System.out.println("1A");  

        }  

        public void visit(BElement bElement) {  

            System.out.println("1B");  

        }  

    }  

  

    static class Visitor2 implements Visitor {  

        public void visit(AElement aElement) {  

            System.out.println("2A");  

        }  

        public void visit(BElement bElement) {  

            System.out.println("2B");  

        }  

    }  

  

  

    public static void main(String[] args) {  

        Element a = new AElement();  

        Element b = new BElement();  

        Visitor v1 = new Visitor1();  

        Visitor v2 = new Visitor2();  

        a.accept(v1);  

        a.accept(v2);  

        b.accept(v1);  

        b.accept(v2);  

    }  

}  

此处可以看出如"a.accept(v)",根据Element类型和Visitor类型来决定调用的是哪个方法。

 
http://jinnianshilongnian.iteye.com/blog/2025189
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: