How is method injection implemented?

木有财 :

During my implementation of spring-like dependency injection, I was puzzled by the fact that spring can injected beans by invoking internal methods, but how did it do this?

How can I implement ioc container like this:

@Bean
public A a() {
    return new A();
}

@Bean
public B b() {
    B b = new B();
    b.setA(a());
    return b;
}

@Bean
public C c() {
    C c = new C();
    c.setB(b());
    return c;
}
Antoniossss :

Assuming we are talking about @Configuration class, dynamic instance proxy is created (using CGLIB) and than all methods invocations are intercepted by proxy logic.

In case of singleton beans (default bean scope) actual method will be invoked only once on first invocation - this is something you can verify yourself using the debugger. Consecuive calls are intercepted and proped instance is returned from the registry.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=400356&siteId=1