spring注解

来源:互联网 发布:网络厂商排名 编辑:程序博客网 时间:2024/05/20 17:59
import org.springframework.beans.factory.annotation.Autowired;

@Autowired  使用后将不用写get,set方法

@Qualifier 注释指定注入 Bean 的名称
@Autowired 可以对成员变量、方法以及构造函数进行注释

对构造函数变量使用 @Qualifier 注释
                public class Boss {    private Car car;    private Air air;    @Autowired    public Boss(Car car , @Qualifier("air")Airair){        this.air= air;        this.car = car;}}
@Qualifier 只能和 @Autowired 结合使用,是对 @Autowired 有益的补充。一般来讲,@Qualifier 对方法签名中入参进行注释会降低代码的可读性,而对成员变量注释则相对好一些。

@Resource

@Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,面@Resource 默认按 byName 自动注入罢了


@Component

  • @Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。
  • @Service 通常作用在业务层,但是目前该功能与 @Component 相同。
  • @Constroller 通常作用在控制层,但是目前该功能与 @Component 相同。
0 0