Spring-----注解實現自動裝配(自動匹配屬性類型) @Autowired 和 autowire


1.注解:實現Bean的屬性裝配

實現注解開發自動裝配步驟:

   1.導入XML 配置文件頭部 約束   增加命名空間  注解支持

 2.注解的支持   <context:annotation-config>   XML導入頭命名空間 連接

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context //增加支持注解的約束
   https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> //引入注解機制

 3.使用注解@Autowired  注入    。        不需要set方法,通過反射機制給屬性賦值,前提是bean對象在XML中創建。

@Autowired   匹配byName byType  仍無法匹配加注解@Qualifier(value=“”)指定Bean的ID。

@Resource(name="")  另一種裝配注解, 先通過byName,在通過byType,最后通過指定屬性name值來匹配裝zai,前提要定義了bean在XML中)最后崩潰。

@Qualifier(value="") 注解自動裝配類太多不能實現,用注解指定其唯一值,類似起別名和@Autowired配合使用.(還可以作為給bean 起別名,)

 

   

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM