欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-25 10:09:55
...
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


































--