Reputation: 1
@Component
public class MyClass {
public MyClass() {
SomeInterface something;
// Spring magic that i don't know
something.toString();
}
}
What Spring magic do I need to use to inject a bean into "something"?
I also wouldn't mind if it was a field. It just has to be usable from within the constructor!
Upvotes: 0
Views: 768
Reputation: 691735
The basic rules also apply to Spring:
This thus leaves two choices:
constructor injection:
@Autowired
public MyClass(SomeInterface something) {
// use something
}
post-construct method, called after all the injections have been done, whatever the way:
@Autowired
private SomeInterface something;
@PostConstruct
private void initialize() {
// use something
}
Upvotes: 1