十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
@Autowired 的作用是什么?
@Autowired 是一個注釋,它可以對類成員變量、方法及構(gòu)造函數(shù)進(jìn)行標(biāo)注,讓 spring 完成 bean 自動裝配的工作。
@Autowired 默認(rèn)是按照類去匹配,配合 @Qualifier 指定按照名稱去裝配 bean。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import blog.service.ArticleService;
import blog.service.TagService;
import blog.service.TypeService;@Controller
br/>@Controller
//成員屬性字段使用 @Autowired,無需字段的 set 方法@Autowired
br/>@Autowired
//set 方法使用 @Autowired
private ArticleService articleService;@Autowired
br/>@Autowired
this.articleService = articleService;
}
//構(gòu)造方法使用 @Autowired
private TagService tagService;@Autowired
br/>@Autowired
this.tagService = tagService; }
}
?@Autowired的用法
br/>}
}
?@Autowired的用法
????
??????????
這樣你在userService里面要做一個userDao的setter/getter方法。
但如果你用了@Autowired的話,你只需要在UserService的實(shí)現(xiàn)類中聲明即可。
br/>????
這樣你在userService里面要做一個userDao的setter/getter方法。
但如果你用了@Autowired的話,你只需要在UserService的實(shí)現(xiàn)類中聲明即可。
private IUserDao userdao;Spring@Autowired注解與自動裝配
br/>Spring@Autowired注解與自動裝配
我們編寫spring?框架的代碼時候。一直遵循是這樣一個規(guī)則:所有在spring中注入的bean?都建議定義成私有的域變量。并且要配套寫上?get?和?set方法。
Boss?擁有?Office?和?Car?類型的兩個屬性:??
清單?3. Boss.java
package com.baobaotao;????
public class Boss {????
????private Car car;????
????private Office office;????????//?省略?get/setter?????
????@Override???
br/>????//?省略?get/setter?????
????@Override???
????????return "car:" + car + "/n" + "office:" + office;????
????}????
}????
??System.out.println必須實(shí)現(xiàn)toString方法
我們在?Spring?容器中將?Office?和?Car?聲明為?Bean,并注入到?Boss Bean?中:下面是使用傳統(tǒng)?XML?完成這個工作的配置文件?beans.xml:??
清單?4. beans.xml?將以上三個類配置成?Bean???
????
????xsi:schemaLocation="http://www.springframework.org/schema/beans?????
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">????
????
????????
????????
????
????
????????
????
????
????????
????????
????
????
當(dāng)我們運(yùn)行以下代碼時,控制臺將正確打出?boss?的信息:??
清單?5.?測試類:AnnoIoCTest.java??
import org.springframework.context.ApplicationContext;????
import org.springframework.context.support.ClassPathXmlApplicationContext;????
public class AnnoIoCTest {????
????public static void main(String[] args) {????
????????String[] locations = {"beans.xml"};????
????????ApplicationContext ctx =?????
????????????new ClassPathXmlApplicationContext(locations);????
????????Boss boss = (Boss) ctx.getBean("boss");????????????System.out.println(boss);????
????}????
}????
這說明?Spring?容器已經(jīng)正確完成了?Bean?創(chuàng)建和裝配的工作。??
?2???@Autowired
br/>????????System.out.println(boss);????
????}????
}????
這說明?Spring?容器已經(jīng)正確完成了?Bean?創(chuàng)建和裝配的工作。??
?2???@Autowired
要實(shí)現(xiàn)我們要精簡程序的目的。需要這樣來處理:
?在applicationContext.xml中加入:
??????
Spring?通過一個BeanPostProcessor?對?@Autowired?進(jìn)行解析,所以要讓?@Autowired?起作用必須事先在Spring?容器中聲明?AutowiredAnnotationBeanPostProcessor Bean。??
?修改在原來注入spirng容器中的bean的方法。?????在域變量上加上標(biāo)簽@Autowired,并且去掉?相應(yīng)的get?和set方法
br/>?????在域變量上加上標(biāo)簽@Autowired,并且去掉?相應(yīng)的get?和set方法
package com.baobaotao;????
import org.springframework.beans.factory.annotation.Autowired;????
public class Boss {????????@Autowired???
br/>????@Autowired???
br/>????@Autowired???
????…????
}?????
*?在applicatonContext.xml中?把原來?引用的
????
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">????
????????
br/>????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"???
????xsi:schemaLocation="http://www.springframework.org/schema/beans?????
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">????
????????
????
?????????
????
????
????????
????
????
????????
????????
???
?這樣,當(dāng)?Spring?容器啟動時,AutowiredAnnotationBeanPostProcessor?將掃描?Spring?容器中所有?Bean,當(dāng)發(fā)現(xiàn)?Bean?中擁有?@Autowired?注釋時就找到和其匹配(默認(rèn)按類型匹配)的?Bean,并注入到對應(yīng)的地方中去。??
br/>????????
???
?這樣,當(dāng)?Spring?容器啟動時,AutowiredAnnotationBeanPostProcessor?將掃描?Spring?容器中所有?Bean,當(dāng)發(fā)現(xiàn)?Bean?中擁有?@Autowired?注釋時就找到和其匹配(默認(rèn)按類型匹配)的?Bean,并注入到對應(yīng)的地方中去。??
當(dāng)然,您也可以通過?@Autowired?對方法或構(gòu)造函數(shù)進(jìn)行標(biāo)注,如果構(gòu)造函數(shù)有兩個入?yún)?,分別是?bean1?和bean2,@Autowired?將分別尋找和它們類型匹配的?Bean,將它們作為?CountryService (Bean1 bean1 ,Bean2 bean2)?的入?yún)韯?chuàng)建CountryService Bean。來看下面的代碼:??對方法
package com.baobaotao;????
public class Boss {????
????private Car car;????
????private Office office;?????????@Autowired????
br/>?????@Autowired????
????????this.car = car;????????}????
????@Autowired???
br/>????}????
????@Autowired???
????????this.office = office;????????}????
????…????
}????
這時,@Autowired?將查找被標(biāo)注的方法的入?yún)㈩愋偷?Bean,并調(diào)用方法自動注入這些?Bean。而下面的使用方法則對構(gòu)造函數(shù)進(jìn)行標(biāo)注:??
br/>????}????
????…????
}????
這時,@Autowired?將查找被標(biāo)注的方法的入?yún)㈩愋偷?Bean,并調(diào)用方法自動注入這些?Bean。而下面的使用方法則對構(gòu)造函數(shù)進(jìn)行標(biāo)注:??
public class Boss {????
????private Car car;????
????private Office office;????????@Autowired???
br/>????@Autowired???
????????this.car = car;????
????????this.office = office ;????
????}????
????…????
}????
由于?Boss()?構(gòu)造函數(shù)有兩個入?yún)?,分別是?car?和?office,@Autowired?將分別尋找和它們類型匹配的?Bean,將它們作為?Boss(Car car ,Office office)?的入?yún)韯?chuàng)建?Boss Bean。

在百色等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作按需求定制網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),全網(wǎng)營銷推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,百色網(wǎng)站建設(shè)費(fèi)用合理。