Field-Based DI in Spring

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

猜你喜欢

转载自blog.csdn.net/weixin_37646636/article/details/133325023