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;
    }
}

猜你喜欢

转载自blog.csdn.net/afmin/article/details/82924078