Spring註解編程之模式註解

模式註解(Stereotype Annotations)  
A stereotype annotation is an annotation that is used to declare the role that a component plays within the
application. For example, the  @Repository annotation in the Spring Framework is a marker for any class that fulfills the role or stereotype of a repository (also known as Data Access Object or DAO).@Component  is a generic stereotype for any Spring-managed component. Any component annotated with @Component  is a candidate for component scanning. Similarly, any component annotated with an annotation that is itself meta-annotated with  @Component  is also a candidate for component scanning. For example, @Service is meta-annotated with  @Component .
模式註解是一種用於聲明在應用中扮演“組件”角色的註解。如 Spring Framework 中的  @Repository 標註在任何類上 ,用
於扮演倉儲角色的模式註解。@Component  作爲一種由 Spring 容器託管的通用模式組件,任何被  @Component  標準的組件均爲組件掃描的候選對象。類似地,凡是被  @Component 元標註(meta-annotated)的註解,如  @Service  ,當任何組件標註它時,也被視作組件掃描的候選對象。

模式註解舉例

Spring Framework 註解         場景說明                     起始版本
@Repository                    數據倉儲模式註解                2.0
@Component                  通用組件模式註解                 2.5
@Service                         服務模式註解                        2.5
@Controller Web             控制器模式註解                    2.5
@Configuration                配置類模式註解                    3.0

Spring @Enable 模塊裝配

Spring Framework 3.1 開始支持”@Enable 模塊驅動“。所謂“模塊”是指具備相同領域的功能組件集合, 組合所形成一個獨立
的單元。比如 Web MVC 模塊、Aspect J代理模塊、Caching(緩存)模塊、JMX(Java 管 理擴展)模塊、Async(異步處
理)模塊等。

@Enable 註解模塊舉例

框架實現                                                        @Enable  註解模塊                                         激活模塊
Spring Framework                                         @Enable Web Mvc                                          Web MVC 模塊
                                                                      @Enable Transaction Management                事務管理模塊
                                                                       @Enable Caching                                           Caching 模塊
                                                                       @Enable MBean Export                                 JMX 模塊
                                                                       @Enable Async                                              異步處理模塊

                                                                       Enable Web Flux                                             Web Flux 模塊
                                                                      @Enable Aspect JAuto Proxy                           Aspect J 代理模塊
     
Spring Boot                                                   @Enable Auto Configuration                             自動裝配模塊
                                                                      @Enable Management Context                         Actuator 管理模塊
                                                                      @Enable Configuration Properties                     配置屬性綁定模塊
                                                                      @Enable OAuth2Sso                                         OAuth2 單點登錄模塊

Spring Cloud                                                  @Enable Eureka Server                                   Eureka服務器模塊
                                                                       @Enable Config Server                                    配置服務器模塊
                                                                       @Enable Feign Clients                                     Feign客戶端模塊
                                                                       @Enable Zuul Proxy                                         服務網關 Zuul 模塊
                                                                       @Enable Circuit Breaker                                   服務熔斷模塊
 

實現方式  
註解驅動方式  
@Retention(Retention Policy.RUNTIME)
@Target(Element Type.TYPE)
@Documented
@Import(Delegating Web Mvc Configuration.class)
public @interface Enable Web Mvc {
}

@Configuration
public class Delegating Web Mvc Configuration extends
Web Mvc Configuration Support {
    ...
}
 

接口編程方式

@Target(Element Type.TYPE)
@Retention(Retention Policy.RUNTIME)
@Documented
@Import(Caching Configuration Selector.class)
public @interface Enable Caching {
...
}
 

public class Caching Configuration Selector extends Advice Mode Import Selector<Enable Caching> {
    /**
    * {@inherit Doc}
    * @return {@link Proxy Caching Configuration} or {@code
    Aspect JCache Configuration} for
    * {@code PROXY} and {@code ASPECTJ} values of {@link
    Enable Caching#mode()}, respectively
    */
    public String[] select Imports(Advice Mode advice Mode) {
        switch (advice Mode) {
            case PROXY:
                return new String[] { 
Auto Proxy Registrar.class.get Name(),Proxy Caching Configuration.class.get Name() };
        case ASPECTJ:
            return new String[] {
                Annotation Config Utils.CACHE_ASPECT_CONFIGURATION_CLASS_NAME };
        default:
            return null;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章