Spring4.X之Constructor Injection 构造方法注入

阅读更多
Spring4.X之Constructor Injection 构造方法注入


With Spring Framework 4.3 it’s very easy to write components that use constructor injections as you no longer need to use @Autowired. As long as you have a single constructor, Spring will implicitly consider it an autowire target:


在 Spring4.x 中增加了新的特性:

如果类只提供了一个带参数的构造方法,则不需要对对其内部的属性写 @Autowired 注解,
Spring 会自动为你注入属性。

注意:
要被注入的Bean,需要在class前写上诸如 @Component,@Service 的注解。

@Component
public class MyComponent {
    
    private final SomeService service;

    public MyComponent(SomeService service) {
        this.service = service;
    }

} 


@Service
public class SomeService {

} 








引用:

https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4


































--

你可能感兴趣的:(spring4,constructor,injection)