javascript
Spring自学教程-注解的使用(三)
一、java中的注解
定義注解
下面是一個定義注解的實例。
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Description {
?String value();
}
其中的@interface是一個關鍵字,在設計annotations的時候必須把一個類型定義為@interface,而不能用class或interface關鍵字。所有的注解類都隱式繼承于java.lang.annotation.Annotation,注解不允許顯式繼承于其他的接口。
一個注解可以擁有多個成員,成員聲明和接口方法聲明類似,這里,我們僅定義了一個成員,成員的聲明有以下幾點限制:
a)?? 成員以無入參無拋出異常的方式聲明,如boolean value(String str)、boolean value() throws Exception等方式是非法的;
b)?? 可以通過default為成員指定一個默認值,如String level() default "LOW_LEVEL"、int high() default 2是合法的,當然也可以不指定默認值;
c)?? 成員類型是受限的,合法的類型包括原始類型及其封裝類、String、Class、enums、注解類型,以及上述類型的數組類型。如ForumService value()、List foo()是非法的。
d)?? 如果注解只有一個成員,則成員名必須取名為value(),在使用時可以忽略成員名和賦值號(=),如@Description("使用注解的實例")。注解類擁有多個成員時,如果僅對value成員進行賦值則也可不使用賦值號,如果同時對多個成員進行賦值,則必須使用賦值號,如@DeclareParents (value = "NaiveWaiter", defaultImpl = SmartSeller.class)。
e)?? 注解類可以沒有成員,沒有成員的注解稱為標識注解,解釋程序以標識注解存在與否進行相應的處理;
注解定義包含四個元注解,分別為@Target,@Retention,@Documented,@Inherited。各元注解的作用如下:
1)? @Target
表示該注解用于什么地方,可能的?ElemenetType?參數包括:
?? ElemenetType.CONSTRUCTOR?構造器聲明。
?? ElemenetType.FIELD?域聲明(包括?enum?實例)。
?? ElemenetType.LOCAL_VARIABLE?局部變量聲明。
?? ElemenetType.METHOD?方法聲明。
?? ElemenetType.PACKAGE?包聲明。
?? ElemenetType.PARAMETER?參數聲明。
?? ElemenetType.TYPE?類,接口(包括注解類型)或enum聲明。
2)? @Retention
表示在什么級別保存該注解信息。可選的?RetentionPolicy?參數包括:
?? RetentionPolicy.SOURCE?注解將被編譯器丟棄。
?? RetentionPolicy.CLASS?注解在class文件中可用,但會被VM丟棄。
?? RetentionPolicy.RUNTIME?VM將在運行期也保留注釋,因此可以通過反射機制讀取注解的信息。
舉一個例子,如@Override里面的Retention設為SOURCE,編譯成功了就不要這一些檢查的信息,相反,@Deprecated里面的Retention設為RUNTIME,表示除了在編譯時會警告我們使用了哪個被Deprecated的方法,在執行的時候也可以查出該方法是否被Deprecated。
3)? @Documented
將此注解包含在?javadoc?中
4)? @Inherited
允許子類繼承父類中的注解
二、spring中注解的使用
1、使用Spring注解來注入屬性
? ??@Resource默認按照名稱(name="test")進行裝配,名稱可以通過@resource的name屬性設定,當找不到與名稱匹配的bean才會按類型裝配? ??? ??注意:如果沒有指定name屬性,并且安裝默認的名稱依然找不到依賴對象時,@Resource會回退到按類型裝配。但一旦指定了name屬性,就只能按名稱裝配了。那么在類中怎樣使用呢?
<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:annotation-config />
</beans>?在配置文件中加上<context:annotationconfig />將隱式地向Spring容器注冊AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor這4個BeanPostProcessor注解解析器,這樣的話我們就不許要在對象中使用set方法了,更方便開發了。。。有木有
2、使用Spring注解完成Bean的定義
?以上我們介紹了通過@Autowired或@Resource來實現在Bean中自動注入的功能,下面我們將介紹如何注解Bean,從而從XML配置文件中完全移除Bean定義的配置。配置文件<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.kedacom.ksoa" />?
</beans>??
@Component(不推薦使用)、@Repository、@Service、@Controller
只需要在對應的類上加上一個@Component注解,就將該類定義為一個Bean了:
Java代碼
@Component
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
????...
}
@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。
<context:component-scan />的base-package屬性指定了需要掃描的類包,類包及其遞歸子包中所有的類都會被處理。
<context:component-scan />還允許定義過濾器將基包下的某些類納入或排除。Spring支持以下4種類型的過濾方式:
過濾器類型 表達式范例 說明
注解 org.example.SomeAnnotation 將所有使用SomeAnnotation注解的類過濾出來
類名指定 org.example.SomeClass 過濾指定的類
正則表達式 com\.kedacom\.spring\.annotation\.web\..* 通過正則表達式過濾一些類
AspectJ表達式 org.example..*Service+ 通過AspectJ表達式過濾一些類
以正則表達式為例,我列舉一個應用實例:
Java代碼
<context:component-scan base-package="com.casheen.spring.annotation">?
????<context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />?
</context:component-scan>?
????????<context:component-scan base-package="com.casheen.spring.annotation">
????????????????<context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />
????????</context:component-scan>
值得注意的是<context:component-scan />配置項不但啟用了對類包進行掃描以實施注釋驅動Bean定義的功能,同時還啟用了注釋驅動自動注入的功能(即還隱式地在內部注冊了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此當使用<context:component-scan />后,就可以將<context:annotation-config />移除了。?
總結:?
?
來自為知筆記(Wiz)
轉載于:https://www.cnblogs.com/wang3680/p/0f4eea023d8eb01b097c732fddba5725.html
總結
以上是生活随笔為你收集整理的Spring自学教程-注解的使用(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 课程改进意见
- 下一篇: Spring中DispacherServ