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