生活随笔
收集整理的這篇文章主要介紹了
Java程序员从笨鸟到菜鸟之(七十二)细谈Spring(四)利用注解实现spring基本配置详解
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
注:由于本人不大習(xí)慣注解方式,所以講解完這里的注解實(shí)現(xiàn)基本配置之后,以后就不再單獨(dú)把注解拿出來(lái)講解了。
五:spring注解
1.準(zhǔn)備工作
(1)導(dǎo)入common-annotations.jar
(2)導(dǎo)入schema文件?文件名為spring-context-2.5.xsd
(3)在xml的beans節(jié)點(diǎn)中配置
2.xml配置工作
?
[html]?view plaincopy print?
<?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.xsd?? ????????????http://www.springframework.org/schema/context?? ????????????http://www.springframework.org/schema/context/spring-context-2.5.xsd"?? default-default-lazy-init="true">?? ?????? ?<context:annotation-config?/>????? ??? <tx:annotation-driventransaction-managertx:annotation-driventransaction-manager="transactionManager"?proxy-target-class="true"/>?? ??? <context:component-scanbase-packagecontext:component-scanbase-package="com"?/>?? ?.....????? ?<beans>????
注:<context:component-scan?base-package="*.*"?/>
?該配置隱式注冊(cè)了多個(gè)對(duì)注解進(jìn)行解析的處理器,如:
?AutowiredAnnotationBeanPostProcessor?????
?CommonAnnotationBeanPostProcessor
?PersistenceAnnotationBeanPostProcessor???
?RequiredAnnotationBeanPostProcessor
?其實(shí),注解本身做不了任何事情,和XML一樣,只起到配置的作用,主要在于背后強(qiáng)大的處理器,其中就包括了<context:annotation-config/>配置項(xiàng)里面的注解所使用的處理器,所以配置了<context:component-scanbase-package="">之后,便無(wú)需再配置<context:annotation-config>
1.在Java代碼中使用@Autowired或@Resource注解方式進(jìn)行裝配?,這兩個(gè)注解的區(qū)別是:@Autowired默認(rèn)按類型裝配,@Resource默認(rèn)按名稱裝配,當(dāng)找不到名稱匹配的bean才會(huì)按類型裝配。
@Autowired一般裝配在set方法之上,也可以裝配在屬性上邊,但是在屬性上邊配置,破壞了java的封裝,所以一般不建議使用
@Autowired是根據(jù)類型進(jìn)行自動(dòng)裝配的。如果當(dāng)Spring上下文中存在不止一個(gè)所要裝配類型的bean時(shí),就會(huì)拋出BeanCreationException異常;如果Spring上下文中不存在所要裝配類型的bean,也會(huì)拋出BeanCreationException異常。我們可以使用@Qualifier配合@Autowired來(lái)解決這些問(wèn)題。
[java]?view plaincopy print?
@Autowired???? public?void?setUserDao(@Qualifier("userDao")?UserDao?userDao)?{????? ???this.userDao?=?userDao;????? }????
這樣,Spring會(huì)找到id為userDao的bean進(jìn)行裝配。
可能不存在UserDao實(shí)例
[java]?view plaincopy print?
@Autowired(required?=?false)????? public?void?setUserDao(UserDao?userDao)?{????? ????this.userDao?=?userDao;????? }????
2.@Resource(JSR-250標(biāo)準(zhǔn)注解,推薦使用它來(lái)代替Spring專有的@Autowired注解)Spring?不但支持自己定義的@Autowired注解,還支持幾個(gè)由JSR-250規(guī)范定義的注解,它們分別是@Resource、@PostConstruct以及@PreDestroy。
@Resource的作用相當(dāng)于@Autowired,只不過(guò)@Autowired按byType自動(dòng)注入,而@Resource默認(rèn)按byName自動(dòng)注入罷了。@Resource有兩個(gè)屬性是比較重要的,分別是name和type,Spring將@Resource注解的name屬性解析為bean的名字,而type屬性則解析為bean的類型。所以如果使用name屬性,則使用byName的自動(dòng)注入策略,而使用type屬性時(shí)則使用byType自動(dòng)注入策略。如果既不指定name也不指定type屬性,這時(shí)將通過(guò)反射機(jī)制使用byName自動(dòng)注入策略
@Resource裝配順序
1?如果同時(shí)指定了name和type,則從Spring上下文中找到唯一匹配的bean進(jìn)行裝配,找不到則拋出異常
2?如果指定了name,則從上下文中查找名稱(id)匹配的bean進(jìn)行裝配,找不到則拋出異常
3?如果指定了type,則從上下文中找到類型匹配的唯一bean進(jìn)行裝配,找不到或者找到多個(gè),都會(huì)拋出異常
4?如果既沒(méi)有指定name,又沒(méi)有指定type,則自動(dòng)按照byName方式進(jìn)行裝配(見(jiàn)2);如果沒(méi)有匹配,則回退為一個(gè)原始類型(UserDao)進(jìn)行匹配,如果匹配則自動(dòng)裝配;
3.?@PostConstruct(JSR-250)
在方法上加上注解@PostConstruct,這個(gè)方法就會(huì)在Bean初始化之后被Spring容器執(zhí)行(注:Bean初始化包括,實(shí)例化Bean,并裝配Bean的屬性(依賴注入))。
它的一個(gè)典型的應(yīng)用場(chǎng)景是,當(dāng)你需要往Bean里注入一個(gè)其父類中定義的屬性,而你又無(wú)法復(fù)寫(xiě)父類的屬性或?qū)傩缘?/span>setter方法時(shí),如:
[java]?view plaincopy print?
public?class?UserDaoImpl?extends?HibernateDaoSupport?implements?UserDao?{????? ?private?SessionFactory?mySessionFacotry;????? @Resource???? public?void?setMySessionFacotry(SessionFactory?sessionFacotry)?{????? ????this.mySessionFacotry?=?sessionFacotry;????? ???}????? ??@PostConstruct???? ???public?void?injectSessionFactory()?{????? ??????super.setSessionFactory(mySessionFacotry);????? ????}???}????
這里通過(guò)@PostConstruct,為UserDaoImpl的父類里定義的一個(gè)sessionFactory私有屬性,注入了我們自己定義的sessionFactory(父類的setSessionFactory方法為final,不可復(fù)寫(xiě)),之后我們就可以通過(guò)調(diào)用super.getSessionFactory()來(lái)訪問(wèn)該屬性了。
4.@PreDestroy(JSR-250)
在方法上加上注解@PreDestroy,這個(gè)方法就會(huì)在Bean初始化之后被Spring容器執(zhí)行。由于我們當(dāng)前還沒(méi)有需要用到它的場(chǎng)景,這里不不去演示。其用法同@PostConstruct。
5.使用Spring注解完成Bean的定義
以上我們介紹了通過(guò)@Autowired或@Resource來(lái)實(shí)現(xiàn)在Bean中自動(dòng)注入的功能,下面我們將介紹如何注解Bean,從而從XML配置文件中完全移除Bean定義的配置。
@Component:只需要在對(duì)應(yīng)的類上加上一個(gè)@Component注解,就將該類定義為一個(gè)Bean了:
[java]?view plaincopy print?
@Component???? public?class?UserDaoImpl?extends?HibernateDaoSupport?implements?UserDao?{????? ????...????? }????
使用@Component注解定義的Bean,默認(rèn)的名稱(id)是小寫(xiě)開(kāi)頭的非限定類名。如這里定義的Bean名稱就是userDaoImpl。你也可以指定Bean的名稱:
@Component("userDao")
@Component是所有受Spring管理組件的通用形式,Spring還提供了更加細(xì)化的注解形式:@Repository、@Service、@Controller,它們分別對(duì)應(yīng)存儲(chǔ)層Bean,業(yè)務(wù)層Bean,和展示層Bean。目前版本(2.5)中,這些注解與@Component的語(yǔ)義是一樣的,完全通用,在Spring以后的版本中可能會(huì)給它們追加更多的語(yǔ)義。所以,我們推薦使用@Repository、@Service、@Controller來(lái)替代@Component。
6.使用<context:component-scan?/>讓Bean定義注解工作起來(lái)
[html]?view plaincopy print?
<pre?name="code"?class="html"><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.xsd???? ??http://www.springframework.org/schema/context??????? http://www.springframework.org/schema/context/spring-context-2.5.xsd">???? ???<context:component-scan?base-package="com.bzu"?/>????? </beans>??</pre><br>?? <br>?? <pre></pre>?? <br>?? <br>?? <p></p>?? <p?style="background:rgb(250,250,250)"><span?style="font-size:18px">這里,所有通過(guò)<span?style="font-family:Times?New?Roman"><bean></span><span?style="font-family:宋體">元素定義</span><span?style="font-family:Times?New?Roman">Bean</span><span?style="font-family:宋體">的配置內(nèi)容已經(jīng)被移除,僅需要添加一行</span><span?style="font-family:Times?New?Roman"><context:component-scan?/></span><span?style="font-family:宋體">配置就解決所有問(wèn)題了</span><span?style="font-family:Times?New?Roman">——Spring?XML</span><span?style="font-family:宋體">配置文件得到了極致的簡(jiǎn)化(當(dāng)然配置元數(shù)據(jù)還是需要的,只不過(guò)以注釋形式存在罷了)。</span><span?style="font-family:Times?New?Roman"><context:component-scan?/></span><span?style="font-family:宋體">的</span><span?style="font-family:Times?New?Roman">base-package</span><span?style="font-family:宋體">屬性指定了需要掃描的類包,類包及其遞歸子包中所有的類都會(huì)被處理。</span><br>?? <context:component-scan?/><span?style="font-family:宋體">還允許定義過(guò)濾器將基包下的某些類納入或排除。</span><span?style="font-family:Times?New?Roman">Spring</span><span?style="font-family:宋體">支持以下</span><span?style="font-family:Times?New?Roman">4</span><span?style="font-family:宋體">種類型的過(guò)濾方式:</span></span></p>?? <p?style="background:rgb(239,239,239)"><span?style="font-size:18px">·?過(guò)濾器類型?表達(dá)式范例?說(shuō)明</span></p>?? <p?style="background:rgb(239,239,239)"><span?style="font-size:18px">·?注解?<span?style="font-family:Helvetica">org.example.SomeAnnotation?</span><span?style="font-family:宋體">將所有使用</span><span?style="font-family:Helvetica">SomeAnnotation</span><span?style="font-family:宋體">注解的類過(guò)濾出來(lái)</span></span></p>?? <p?style="background:rgb(239,239,239)"><span?style="font-size:18px">·?類名指定?<span?style="font-family:Helvetica">org.example.SomeClass?</span><span?style="font-family:宋體">過(guò)濾指定的類</span></span></p>?? <p?style="background:rgb(239,239,239)"><span?style="font-size:18px">·?正則表達(dá)式?<span?style="font-family:Helvetica">com\.kedacom\.spring\.annotation\.web\..*?</span><span?style="font-family:宋體">通過(guò)正則表達(dá)式過(guò)濾一些類</span></span></p>?? <p?style="background:rgb(239,239,239)"><span?style="font-size:18px">·?AspectJ<span?style="font-family:宋體">表達(dá)式?</span><span?style="font-family:Helvetica">org.example..*Service+?</span><span?style="font-family:宋體">通過(guò)</span><span?style="font-family:Helvetica">AspectJ</span><span?style="font-family:宋體">表達(dá)式過(guò)濾一些類</span></span></p>?? <p><span?style="font-size:18px"><span?style="color:rgb(0,0,255)">7.</span><span?style="color:rgb(0,0,255)">使用<span?style="font-family:Times?New?Roman">@Scope</span><span?style="font-family:宋體">來(lái)定義</span><span?style="font-family:Times?New?Roman">Bean</span><span?style="font-family:宋體">的作用范圍</span></span><br>?? 在使用<span?style="font-family:Times?New?Roman">XML</span><span?style="font-family:宋體">定義</span><span?style="font-family:Times?New?Roman">Bean</span><span?style="font-family:宋體">時(shí),我們可能還需要通過(guò)</span><span?style="font-family:Times?New?Roman">bean</span><span?style="font-family:宋體">的</span><span?style="font-family:Times?New?Roman">scope</span><span?style="font-family:宋體">屬性來(lái)定義一個(gè)</span><span?style="font-family:Times?New?Roman">Bean</span><span?style="font-family:宋體">的作用范圍,我們同樣可以通過(guò)</span><span?style="font-family:Times?New?Roman">@Scope</span><span?style="font-family:宋體">注解來(lái)完成這項(xiàng)工作:</span></span></p>?? <p?style="background:rgb(250,250,250)"><span?style="font-size:18px"></span></p>?? <pre?name="code"?class="java">@Scope("session")????? @Component()????? public?class?UserSessionBean?implements?Serializable?{????? ????...????? }??</pre>?? <p></p>?? <p?style="background:rgb(250,250,250)"><span?style="font-size:18px"><br>?? <br>?? <br>?? <br>?? <br>?? <br>?? </span></p>?? <p></p>?? ? ? ? from:?http://blog.csdn.net/csh624366188/article/details/7647815
總結(jié)
以上是生活随笔為你收集整理的Java程序员从笨鸟到菜鸟之(七十二)细谈Spring(四)利用注解实现spring基本配置详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。