Create a bean and autowire it
I currently have numerous classes with a field like
private MyStuff myStuff = new MyStuff();
It would be preferable if there was a MyStuff singleton which all the
classes used. Our project has a MyConfiguration class with some Beans in
it, but they don't seem to get used, at least not directly, so I can't use
them for examples. I have been tasked to create a MyStuff bean in the
MyConfiguration class, and then inject it into my other classes.
What I have so far:
@Configuration
public class MyConfiguration
{
@Bean
public MyStuff myStuff()
{
return new MyStuff();
}
}
public SomeClass
{
public void dealWithStuff()
{
myStuff.myMethod();
}
@Autowired
private MyStuff someStuff;
}
This does not compile. The compiler says "variable someStuff may not have
been initialized". Apparently it does not see the bean or make the
connection. What am I missing?
No comments:
Post a Comment