Reputation: 635
Created myLoaders bean and autowired. after that code is calling loadingwhich internally calls h()
function. when h()
function is called checked a
and a1
and both are coming as null
.
Though a and a1 is passed and autowired , not sure why this is coming as null
.
package nestedbean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Autowired
Loaders myLoaders;
@Bean
public A a(){
return new A(b());
}
@Bean
public A1 a1(){
return new A1();
}
@Bean
public B b(){
return new B(c());
}
@Bean
public C c(){return new C();}
@Bean
public History history(){
return new History(a(), a1());
}
@Bean
public Loaders myLoaders() {
return new MyLoaders( history());
}
@Bean
public Void load () throws Exception {
myLoaders.loading();
return null ;
}
static class History{
A a ;
A1 a1 ;
@Autowired
public History(A a , A1 a1 ) {
a = a ; a1 = a1 ;
}
public void h(){ //when this function is called a and a1 both are null
a1.test();
}
}
static class MyLoaders implements Loaders {
public History h ;
@Autowired C c;
@Autowired
public MyLoaders(History history ){ h= history ;}
public void loading(){
System.out.println("loading");
h.h();
}
}
static class A {
B b;
public A(B b){ this.b = b ;}
}
static class A1 {
@Autowired
C c;
B b;
public void test(){
System.out.println("testing");
}
}
static class B{ C c;
public B(C c){ c = c ;}
}
static class C { }
public interface Loaders {
final String mystring = "4";
public void loading();
}
}
Can someone please check. If you can provide some references that would be really helpful.
thanks
Upvotes: 2
Views: 1037
Reputation: 797
Your constructor does not set the fields, you forgot the keyword "this" to reference your instance. Try with this contructor :
@Autowired
public History(A a , A1 a1 ) {
this.a = a ; this.a1 = a1 ;
}
Upvotes: 1