(Transfer) Spring beans source code interpretation -- Bean annotation (annotation)

With the introduction of spring annotations, more and more developers begin to use annotations. This article will explain the mechanism of annotations in series. It does not seek to be in-depth, but seeks to string the pearls of spring beans annotations and show them to everyone. .

1. Common annotations of spring beans:

  Public @interface Autowired: You can annotate member variables, methods, and constructors to complete autowiring.

copy code
Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.
Only one constructor (at max) of any given bean class may carry this annotation, indicating the constructor to autowire when used as a Spring bean. Such a constructor does not have to be public.

Fields are injected right after construction of a bean, before any config methods are invoked. Such a config field does not have to be public.

Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container. Bean property setter methods are effectively just a special case of such a general config method. Such config methods do not have to be public.

In the case of multiple argument methods, the 'required' parameter is applicable for all arguments.
 In case of a Collection or Map dependency type, the container will autowire all beans matching the declared value type. In case of a Map, the keys must be declared as type String and will be resolved to the corresponding bean names.
 Note that actual injection is performed through a BeanPostProcessor which in turn means that you cannot use @Autowired to inject references into BeanPostProcessor or BeanFactoryPostProcessor types. Please consult the javadoc for the AutowiredAnnotationBeanPostProcessor class (which, by default, checks for the presence of this annotation). Since: 2.5
copy code

  The autowire attribute in the public @interface Configurable  @Configurable annotation allows Spring to autowire:  @Configurable(autowire=Autowire.BY_TYPE) alternatively  @Configurable(autowire=Autowire.BY_NAME, autowire by type or by name.

Marks a class as being eligible for Spring-driven configuration.
Typically used with the AspectJ AnnotationBeanConfigurerAspect.

Since:
2.0
public @interface Value: Used to inject SpEL expressions, can be placed on field methods or parameters.
copy code
Annotation at the field or method/constructor parameter level that indicates a default value expression for the affected argument.
Typically used for expression-driven dependency injection. Also supported for dynamic resolution of handler method parameters, e.g. in Spring MVC.

A common use case is to assign default field values using "#{systemProperties.myProp}" style expressions.

Note that actual processing of the @Value annotation is performed by a BeanPostProcessor which in turn means that you cannot use @Value within BeanPostProcessor or BeanFactoryPostProcessor types. Please consult the javadoc for the AutowiredAnnotationBeanPostProcessor class (which, by default, checks for the presence of this annotation).

Since:
3.0
copy code
public @interface Qualifier:指定限定描述符,对应于基于XML配置中的<qualifier>标签,@Qualifier限定描述符除了能根据名字进行注入,但能进行更细粒度的控制如何选择候选者
@Qualifier(value = "限定标识符") 。
This annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring. It may also be used to annotate other custom annotations that can then in turn be used as qualifiers.
Since:
2.5
public @interface Required 依赖检查;
copy code
Marks a method (typically a JavaBean setter method) as being 'required': that is, the setter method must be configured to be dependency-injected with a value.
Please do consult the javadoc for the RequiredAnnotationBeanPostProcessor class (which, by default, checks for the presence of this annotation).

Since:
2.0

 

copy code

2. 注解bean的定义AnnotatedBeanDefinition

copy code
public interface AnnotatedBeanDefinition extends BeanDefinition {

    /**
     * Obtain the annotation metadata (as well as basic class metadata)
     * for this bean definition's bean class.
     * @return the annotation metadata object (never {@code null})
     */
    AnnotationMetadata getMetadata();

}
copy code

   该接口继承了BeanDefinition,提供了一个getMetadata()方法来获取该bean definition的注解元数据。

其中,AnnotationMetadata定义了访问特定类的注解的抽象接口,它不需要加载该类即可访问。

public interface AnnotationMetadata extends ClassMetadata, AnnotatedTypeMetadata {
}
ClassMetadata定义了一个特定类的抽象元数据,不需要加载此类。主要方法如下:
String getClassName()返回该类的名称。boolean isInterface()返回该类是否是接口。boolean isAbstract()返回该类是否为抽象类。boolean isConcrete()返回该类是否为具体类。boolean isFinal()返回该类是否为final类boolean hasSuperClass()返回该类是否有父类
String getSuperClassName()返回父类的名称,没有的话返回null.
String[] getInterfaceNames()返回继承的接口数组,如果没有,返回空.
String[] getMemberClassNames()返回引用的类的名称。

AnnotatedTypeMetadata定义访问特定类型的注解,不需要加载类。主要方法有:

boolean isAnnotated(String annotationType)是否有匹配的注解类型
Map<String,Object> getAnnotationAttributes(String annotationType,boolean classValuesAsString)获取特定类型注解的属性

AnnotationMetadata的标准实现类StandardAnnotationMetadata,它使用标准的反射来获取制定类的内部注解信息。主要方法有:

getAllAnnotationAttributes(String annotationType) 
getAnnotatedMethods(String annotationType) 
hasMetaAnnotation(String annotationType) 
isAnnotated(String annotationType) 
hasAnnotatedMethods(String annotationType) 

AnnotationMetadata还有一个子类:AnnotationMetadataReadingVisitor,它是字节码访问实现。

class ClassMetadataReadingVisitor extends ClassVisitor implements ClassMetadata {
}

让我们了解一下visitor模式:

定义:

The Gang of Four defines the Visitor as: "Represent an operation to be performed on elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates."—″≤

The nature of the Visitor makes it an ideal pattern to plug into public APIs thus allowing its clients to perform operations on a class using a “visiting” class without having to modify the source.

uml 结构图如下:

<iframe id="iframe_0.3630733445752412" style="box-sizing: border-box; border-width: initial; border-style: none; width: 300px; height: 267px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/wikipedia/commons/thumb/9/9d/VisitorDiagram.svg/300px-VisitorDiagram.svg.png?_=4199459%22%20style=%22border:none;max-width:1611px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.3630733445752412',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>

 小结:vistor设置模式把状态抽象出来成为一个接口(访问者),不同的状态就作为状态的不同实现类(不同的访问者)。

 3. 注解bean的实现类

    3.1 AnnotatedGenericBeanDefinition

          继承了GenericBeanDefinition,增加了对注解元素的支持,这种支持是通过AnnotationBeanDefinition暴露的的注解元素接口。

    GenericBeanDefinition主要用来测试AnnotatedBeanDefinition上的操作的,例如:在spring的component扫描支持的实现中(默认实现类是ScannedGenericBeanDefinition,它同样实现了AnnotatedBeanDefinition接口)

  3.2 ConfigurationClassBeanDefinition

    ConfigurationClassBeanDefinition是ConfigurationClassBeanDefinitionReader的内部类,ConfigurationClassBeanDefinitionReader读取一组完全填充了属性的配置实例,通过context内给定的BeanDefinitionRegistry进行注册bean definition。这个类在BeanDefinitionReader这层后就改造,但没有继承或者扩展配置类。

   3.3 ScannedGenericBeanDefinition

   基于asm的类解析器,是GenericBeanDefinition类的扩展,支持注解元数据,这种支持通过AnnotatedBeanDefinition接口实现。

4. 注解的解析与处理

4.1 @Autowired注解实现AutowiredAnnotationBeanPostProcessor

   

public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter
        implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware {
}
AutowiredAnnotationBeanPostProcessor 间接继承了BeanPostProcessor,它自动绑定注解的field,setter方法和任意的配置方法。当检测到5个java注解时这些成员被注入其中。spring默认的注解为@Autowired和@Value。
另外:也支持JSR-330的@inject注解,作为@Autowired的替代方案。

当制定bean 类的唯一构造方法带有required 注解参数,且required值设置为true时,表明当作为spring一个bean时,构造方法默认自动绑定。若多个构造方法带有non-required 注解参数,它们将作为自动绑定的候选项。带有大量依赖的构造方法可以通过spring容器中的匹配的bean来构造,如果没有候选者满足条件,则会使用默认的构造器。注解构造器不一定是public的。
Field注入是在构造方法之后,配置方法之前,这种配置field不要求一定为public
配置方法可以有任意的名称和不定的参数列表,这些参数则被自动注入到spring容器中的匹配的bean。bean的属性setter方法仅仅是通用的配置方法的一个特例而已。配置方法不要求方法一定为public
注意:默认注册AutowiredAnnotationBeanPostProcessor的方式有<context:annotation-config> 和<context:component-scan> xml标签,如果你指定了一个自定义的AutowiredAnnotationBeanPostProcessor bean definition,移除或者关闭默认的注解配置。
其中,MergedBeanDefinitionPostProcessor的定义如下:
copy code
public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor {

    /**
     * Post-process the given merged bean definition for the specified bean.
     * @param beanDefinition the merged bean definition for the bean
     * @param beanType the actual type of the managed bean instance
     * @param beanName the name of the bean
     */
    void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName);

}
copy code
BeanPostProcessor 是一个可以定制修改一个新的bean实例的工厂钩子,例如:检查marker接口或者使用代理包装他们。
applicationContext可以在他们的bean容器中自动识别BeanPostProcessor bean,并将它们应用到接下来所创建的bean。一般的bean factory通过编程来注册Post-processor,并将它们应用到整个bean factory创建bean的过程中。

通常意义上,post-processor 设置bean属性通过marker 接口或者类似于实现postProcessBeforeInitialization(java.lang.Object, java.lang.String);使用代理包装bean通常实现postProcessAfterInitialization(java.lang.Object, java.lang.String).

4.2 @configurable注解实现AnnotationBeanWiringInfoResolver 

设置 @Configurable 注解中的autowire属性就可以让Spring来自动装配了: @Configurable(autowire=Autowire.BY_TYPE) 或者 @Configurable(autowire=Autowire.BY_NAME,这样就可以按类型或者按名字自动装配了。

    AnnotationBeanWiringInfoResolver 继承自BeanWiringInfoResolver,BeanWiringInfoResolver使用configurable注解来查找哪些类需要自动绑定。

public class AnnotationBeanWiringInfoResolver implements BeanWiringInfoResolver {
}

实现了BeanWiringInfoResolver的resolveWiringInfo方法

copy code
@Override
    public BeanWiringInfo resolveWiringInfo(Object beanInstance) {
        Assert.notNull(beanInstance, "Bean instance must not be null");
        Configurable annotation = beanInstance.getClass().getAnnotation(Configurable.class);
        return (annotation != null ? buildWiringInfo(beanInstance, annotation) : null);
    }

    /**
     * Build the BeanWiringInfo for the given Configurable annotation.
     * @param beanInstance the bean instance
     * @param annotation the Configurable annotation found on the bean class
     * @return the resolved BeanWiringInfo
     */
    protected BeanWiringInfo buildWiringInfo(Object beanInstance, Configurable annotation) {
        if (!Autowire.NO.equals(annotation.autowire())) {
            return new BeanWiringInfo(annotation.autowire().value(), annotation.dependencyCheck());
        }
        else {
            if (!"".equals(annotation.value())) {
                // explicitly specified bean name
                return new BeanWiringInfo(annotation.value(), false);
            }
            else {
                // default bean name
                return new BeanWiringInfo(getDefaultBeanName(beanInstance), true);
            }
        }
    }
copy code

 4.3 @qualifier的注解实现类QualifierAnnotationAutowireCandidateResolver

 

public class QualifierAnnotationAutowireCandidateResolver extends GenericTypeAwareAutowireCandidateResolver {
}

public class GenericTypeAwareAutowireCandidateResolver implements AutowireCandidateResolver, BeanFactoryAware {
}

其中,AutowireCandidateResolver是一个策略接口,由它来决定特定的bean definition对特定的依赖是否可以作为一个自动绑定的候选项,它的主要方法有:

boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor)

Object getLazyResolutionProxyIfNecessary(DependencyDescriptor descriptor,String beanName)

Object getSuggestedValue(DependencyDescriptor descriptor)

 QualifierAnnotationAutowireCandidateResolver间接实现了AutowireCandidateResolver,对要自动绑定的field或者参数和bean definition根据@qualifier注解进行匹配。同时也支持通过@value注解来绑定表达式的值。

另外,还只是JSR-330的javax.inject.Qualifier注解。

4.4 @required注解实现类RequiredAnnotationBeanPostProcessor。

public class RequiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter
        implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware {
}

和AutowiredAnnotationBeanPostProcessor一样,间接继承自BeanPostProcessor,它增加了对javaBean属性配置的约束,java 5 注解可以检测bean的required属性,spring默认是@Required注解。

注意:默认注册AutowiredAnnotationBeanPostProcessor的方式有<context:annotation-config> 和<context:component-scan> xml标签,如果你指定了一个自定义的
默认注册AutowiredAnnotationBeanPostProcessor的方式有<context:annotation-config> 和<context:component-scan> xml标签,如果你指定了一个自定义的AutowiredAnnotationBeanPostProcessor bean definition,移除或者关闭默认的注解配置。其余和AutowiredAnnotationBeanPostProcessor类似,不一一赘述了。

4.5 初始化和销毁方法的注解实现类InitDestroyAnnotationBeanPostProcessor

public class InitDestroyAnnotationBeanPostProcessor
        implements DestructionAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, PriorityOrdered, Serializable {
}

InitDestroyAnnotationBeanPostProcessor间接继承了BeanPostProcess,实现了通过注解来初始化和销毁方法,是spring的InitializingBean和DisposableBean回调接口的注解实现。

它通过"initAnnotationType"和"destroyAnnotationType"属性来检查指定的注解类型,任何自定义的注解都可以使用。

初始化和销毁注解可以用在任意可见的方法:public,package-protected,protected,private等。尽管可以对多个方法进行注解,但我们推荐只在一个初始化和销毁方法上各自进行注解。

 

小结:

 Spring3的基于注解实现Bean依赖注入支持如下三种注解:
Spring自带依赖注入注解: Spring自带的一套依赖注入注解;
JSR-250注解:Java平台的公共注解,是Java EE 5规范之一,在JDK6中默认包含这些注解,从Spring2.5开始支持。
JSR-330注解:Java 依赖注入标准,Java EE 6规范之一,可能在加入到未来JDK版本,从Spring3开始支持;

其中,
Spring自带依赖注入注解

1 @Required:依赖检查;
2 @Autowired:自动装配2 
自动装配,用于替代基于XML配置的自动装配
基于@Autowired的自动装配,默认是根据类型注入,可以用于构造器、字段、方法注入
3 @Value:注入SpEL表达式
用于注入SpEL表达式,可以放置在字段方法或参数上
@Value(value = "SpEL表达式")  
@Value(value = "#{message}")  
4 @Qualifier:限定描述符,用于细粒度选择候选者
@Qualifier限定描述符除了能根据名字进行注入,但能进行更细粒度的控制如何选择候选者
@Qualifier(value = "限定标识符") 


JSR-250注解

1 @Resource:自动装配,默认根据类型装配,如果指定name属性将根据名字装配,可以使用如下方式来指定
@Resource(name = "标识符")  
字段或setter方法 

2 @PostConstruct和PreDestroy:通过注解指定初始化和销毁方法定义

 

JSR-330 annotation
1 @Inject: Equivalent to the default @Autowired, but without the required attribute
2 @Named: Specify the bean name, which corresponds to the default injection based on the bean name in Spring's own @Qualifier
3 @Qualifier: only Corresponds to the extension @Qualifier qualified descriptor annotation in Spring's own @Qualifier, that is, it can only be extended and used, and there is no value attribute

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326711474&siteId=291194637