1、@SpringBootApplication:指定類爲應用啓動類html
包含@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScanjava
@ComponentScan:能夠經過該註解指定掃描某些包下包含以下註解的均自動註冊爲spring beans:@Component, @Service, @Repository, @Controller,@Entity等spring
2、@Autowired安全
消除代碼Java代碼裏面的getter/setter與bean屬性中的property,@Autowired默認按類型匹配的方式,在容器查找匹配的Bean,當有且僅有一個匹配的Bean時,Spring將其注入@Autowired標註的變量中。prototype
3、@Qualifiercode
若是容器中有一個以上匹配的Bean,則能夠經過@Qualifier註解限定Bean的名稱htm
public interface ICar { public String getCarName(); } public class BMWCar implements ICar{ public String getCarName(){ return "BMW car"; } } public class BenzCar implements ICar{ public String getCarName(){ return "Benz car"; } } public class CarFactory { @Autowired @Qualifier("bmwCar") private ICar car; public String toString(){ return car.getCarName(); } }
4、@Resource對象
一、@Resource註解與@Autowired註解做用類似blog
二、@Resource的裝配順序get
(1)、@Resource後面沒有任何內容,默認經過name屬性去匹配bean,找不到再按type去匹配
(2)、指定了name或者type則根據指定的類型去匹配bean
(3)、指定了name和type則根據指定的name和type去匹配bean,任何一個不匹配都將報錯
三、@Autowired和@Resource兩個註解的區別:
(1)、@Autowired默認按照byType方式進行bean匹配,@Resource默認按照byName方式進行bean匹配
(2)、@Autowired是Spring的註解,@Resource是J2EE的註解
5、@Service
@Service public class Zoo { @Autowired private Tiger tiger; @Autowired private Monkey monkey; public String toString(){ return tiger + "\n" + monkey; } }
(1)、聲明Zoo.java是一個bean,這點很重要,由於Zoo.java是一個bean,其餘的類纔可使用@Autowired將Zoo做爲一個成員變量自動注入。
(2)、Zoo.java在bean中的id是"zoo",即類名且首字母小寫。
6、@Component
@Component是全部受Spring 管理組件的通用形式,@Component註解能夠放在類的頭上,@Component不推薦使用。
7、@Controller
一、使用@Controller註解標識UserAction以後,就表示要把UserAction交給Spring容器管理,在Spring容器中會存在一個名字爲"userAction"的action,這個名字是根據UserAction類名來取的。
二、默認的bean名字爲這個類的類名首字母小寫。
【@Controller(value="UserAction")】或者【@Controller("UserAction")】,則使用value做爲bean的名字
8、@Scope
spring 默認scope 是單例模式(scope="singleton"),這樣只會建立一個Action對象,每次訪問都是同一Action對象,數據不安全。scope="prototype" 能夠保證當有請求的時候都建立一個Action對象
9、@ Repository
@Repository對應數據訪問層Bean。 @Repository(value="userDao")註解是告訴Spring,讓Spring建立一個名字叫"userDao"的UserDaoImpl實例。
參考地址:spring經常使用註解總結:http://www.javashuo.com/article/p-cmxrovau-cy.html