Field-Based DI
For Field-Based DI, we can inject the dependencies by marking them with an @Autowired annotation
。
If there’s no constructor or setter method
to inject the dependencies, the container will use reflection to inject dependencies
.
# Field-Based DI
public class Store {
@Autowired
private Item item;
}
# We can also achieve this using XML configuration.
参考:
Intro to Inversion of Control and Dependency Injection with Spring