I have an example that is something like this
The class Other needs an instance of MyBean so I'm creating an attribute and using that attribute while creating and Other
@Configuration
public SomeClass {
@Resource
private MyBean b;
@Autowired
Environment env;
@Bean
public MyBean myBean() {
MyBean b = new MyBean();
b.foo(env.getProperty("mb"); // NPE
return b;
}
@Bean
public Other other() {
Other o = new Other(o);
return o;
}
}
But I'm getting NullPointerException while initializing the myBean object, I guess that's because the env property has not been wired still at that point.
If I don't use the bean and use the method directly everything works well.
@Configuration
public SomeClass {
@Autowired
Environment env;
@Bean
public MyBean myBean() {
MyBean b = new MyBean();
b.foo(env.getProperty("mb"); // NPE
return b;
}
@Bean
public Other other() {
Other o = new Other(myBean());
return o;
}
}
Is it because I'm defining the @Bean in the same @Configuration class?