国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Spring @Resource、@Autowired、@Qualifier的注解注入及區別

2019-11-08 02:34:21
字體:
來源:轉載
供稿:網友

sPRing2.5提供了基于注解(Annotation-based)的配置,我們可以通過注解的方式來完成注入依賴。在java代碼中可以使用 @Resource或者@Autowired注解方式來經行注入。雖然@Resource和@Autowired都可以來完成注入依賴,但它們之間是有區別的。首先來看一下:

區別

a。@Resource默認是按照名稱來裝配注入的,只有當找不到與名稱匹配的bean才會按照類型來裝配注入; b。@Autowired默認是按照類型裝配注入的,如果想按照名稱來轉配注入,則需要結合@Qualifier一起使用; c。@Resource注解是又J2EE提供,而@Autowired是由spring提供,故減少系統對spring的依賴建議使用 @Resource的方式; d。 @Resource和@Autowired都可以書寫標注在字段或者該字段的setter方法之上

配置信息

使用注解的方式,我們需要修改spring配置文件的頭信息,一般都是appcontext-*.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-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:annotation-config/></beans>

注解

修改以上配置文件的頭信息后,我們就可以在Java代碼通過注解方式來注入bean,看下面代碼

@Resource

public class StudentService3 implements IStudentService {//@Resource(name="studentDao")放在此處也是可行的private IStudentDao studentDao;private String id;public void setId(String id) {this.id = id;}

@Resource(name="studentDao")// 通過此注解完成從spring配置文件中查找名稱為studentDao的bean來裝配字段studentDao,如果spring配置文件中不存在 studentDao名稱的bean則轉向按照bean類型經行查找

public void setStudentDao(IStudentDao studentDao) {this.studentDao = studentDao;}public void saveStudent() {studentDao.saveStudent();System.out.print(",ID 為:"+id); }}

配置文件添加如下信息

<bean id="studentDao" class="com.wch.dao.impl.StudentDao"></bean><bean id="studentService3" class="com.wch.service.impl.StudentService3" />

@Autowired

public class StudentService3 implements IStudentService { //@Autowired放在此處也是可行的private IStudentDao studentDao;private String id;public void setId(String id) {this.id = id;}

@Autowired//通過此注解完成從spring配置文件中 查找滿足studentDao類型的bean

//@Qualifier("studentDao")則按照名稱經行來查找轉配的 public void setStudentDao(IStudentDao studentDao) {this.studentDao = studentDao;}public void saveStudent() {studentDao.saveStudent();System.out.print(",ID 為:"+id);}}

配置文件添加如下信息

<bean id="studentDao" class="com.wch.dao.impl.StudentDao"></bean><bean id="studentService3" class="com.wch.service.impl.StudentService3" />

總結

在java代碼中可以使用@Autowire或者@Resource注解方式進行裝配,這兩個注解的區別是: @Autowired 默認按照類型裝配,默認情況下它要求依賴對象必須存在如果允許為null,可以設置它required屬性為false,如果我們想使用按照名稱裝配,可以結合@Qualifier注解一起使用;

@Resource默認按照名稱裝配,當找不到與名稱匹配的bean才會按照類型裝配,可以通過name屬性指定,如果沒有指定name屬 性,當注解標注在字段上,即默認取字段的名稱作為bean名稱尋找依賴對象,當注解標注在屬性的setter方法上,即默認取屬性名作為bean名稱尋找 依賴對象. 注意:如果沒有指定name屬性,并且按照默認的名稱仍然找不到依賴的對象時候,會回退到按照類型裝配,但一旦指定了name屬性,就只能按照名稱裝配了.

參考: http://blog.csdn.net/baple/article/details/17891755


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昌江| 巴塘县| 深泽县| 苏尼特左旗| 页游| 余姚市| 日土县| 安阳市| 肥西县| 湛江市| 滨州市| 会昌县| 曲靖市| 任丘市| 崇阳县| 富民县| 红安县| 杭州市| 搜索| 页游| 扶沟县| 伊宁市| 滨州市| 辽阳县| 嘉兴市| 崇阳县| 通化市| 故城县| 板桥市| 漾濞| 当涂县| 炎陵县| 甘肃省| 安吉县| 察隅县| 前郭尔| 绥宁县| 土默特左旗| 那坡县| 信丰县| 肥东县|