I have something like this:
The class Other
needs an instance of MyBean
so create the attribute and use it when I create the bean 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 throw NullPointerException
when initializing the object myBean
, I guess it's because the property env
has not yet been injected at that point.
If I do not use the bean but call the method directly everything works fine.
@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 @Bean in the same @Configuration class?