1. 注入形式

在springBoot中能够对属性进行注入、能够对构造函数进行注入、以及对应setter办法进行注入。

@Componentpublic class User{// 进行属性注入@AutoWiredprivate ICorp corp;private IDept dept;// 结构器注入@AutoWiredUser(IDept dept){   this.dept = dept;}// setter办法注入@AutoWiredpublic void setIXx(IXx xx){}}

由上述代码中能够看到注入的形式有三种形式进行注入,每种形式都能够,注入须要借用特定的注解进行注入如@AutoWired

2. 注入注解

在spring中罕用注入注解有:@AutoWired、@Qulified、@Resource。

  1. @AutoWired 依照属性的类型进行查找对应类型的bean,如果查找多个则会依照属性的变量名作为bean的name进行查问
  2. @Qulified 经常和@AutoWired联合应用,如果类型的bean有多个,能够应用该注解指定名称
  3. @Resouce(name="xx") 依照指定的name的查找对应bean的name值,如果查找多个,则会依照属性的类型再排除