码迷,mamicode.com
首页 > 编程语言 > 详细

Java基础知识

时间:2019-04-22 22:48:51      阅读:166      评论:0      收藏:0      [点我收藏+]

标签: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[]

Java基础知识

标签:public   依赖注入   编译器   getters   min   setters   when   构造器   struct   

原文地址:https://www.cnblogs.com/jialilue/p/10753317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!