标签:public 依赖注入 编译器 getters min setters when 构造器 struct
@Override
表示重写
编译器可以验证@Override下面的方法名是否是父类中所有的
@Autowired
使用@Autowired 注解来实现依赖注入
可以用在属性、setter方法和构造器上
用在属性上
@Component("fooFormatter") public class FooFormatter { public String format() { return "foo"; } }
@Component public class FooService { @Autowired private FooFormatter fooFormatter; }
The annotation can be used directly on properties, therefore eliminating the need for getters and setters:
In the above example, Spring looks for and injects fooFormatter when FooService is created.
用在setter上
public class FooService {
private FooFormatter fooFormatter;
@Autowired public void setFooFormatter(FooFormatter fooFormatter) { this.fooFormatter = fooFormatter; } }
The @Autowired annotation can be used on setter methods. In the above example, when the annotation is used on the setter method, the setter method is called with the instance of FooFormatter when FooServiceis created:
用在构造器上
public class FooService { private FooFormatter fooFormatter; @Autowired public FooService(FooFormatter fooFormatter) { this.fooFormatter = fooFormatter; } }
The @Autowired annotation can also be used on constructors. In the above example, when the annotation is used on a constructor, an instance of FooFormatter is injected as an argument to the constructor when FooService is created:
String...
可变参数
相当于String[]
标签:public 依赖注入 编译器 getters min setters when 构造器 struct
原文地址:https://www.cnblogs.com/jialilue/p/10753317.html