`

(转)spring beans源码解读之--Bean的注解(annotation)

 
阅读更多

随着spring注解的引入,越来越多的开发者开始使用注解,这篇文章将对注解的机制进行串联式的讲解,不求深入透彻,但求串起spring beans注解的珍珠,展示给大家。

1. spring beans常用的注解:

  public @interface Autowired:可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。

复制代码
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
复制代码

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

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

Since:
2.0
public @interface Value:用于注入SpEL表达式,可以放置在字段方法或参数上。
复制代码
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
复制代码
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 依赖检查;
复制代码
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

 

复制代码

2. 注解bean的定义AnnotatedBeanDefinition

复制代码
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();

}
复制代码

   该接口继承了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的定义如下:
复制代码
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);

}
复制代码
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方法

复制代码
@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);
            }
        }
    }
复制代码

 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注解
1 @Inject:等价于默认的@Autowired,只是没有required属性
2 @Named:指定Bean名字,对应于Spring自带@Qualifier中的缺省的根据Bean名字注入情况
3 @Qualifier:只对应于Spring自带@Qualifier中的扩展@Qualifier限定描述符注解,即只能扩展使用,没有value属性

分享到:
评论

相关推荐

    spring源码spring-framework-4.2.5.RELEASE

    在`org.springframework.beans`和`org.springframework.context`包中,我们可以看到BeanFactory和ApplicationContext的实现,它们通过XML、注解或Java配置来创建和管理Bean。 2. **AOP(Aspect Oriented ...

    spring 5.2.9 07 源码分析-spring的bean工厂准备工作 测试用例

    在Spring框架中,Bean工厂是核心组件之一,它负责管理所有Bean的生命周期,包括实例化、配置、装配以及销毁。本章将详细分析Spring 5.2.9版本中Bean工厂的准备工作,特别是通过测试用例来理解其实现细节。 在Spring...

    12 @Autowired是如何工作的?-Spring注解源码深度揭秘-慕课专栏1

    `@Autowired`的定义位于`org.springframework.beans.factory.annotation.Autowired`包中,其核心属性只有一个`required`,默认值为`true`。这个属性表示依赖是否必需,如果设置为`false`,即使找不到匹配的依赖,...

    SpringMVC源码总结(三)mvc:annotation-driven和mvc:message-converters简单介绍

    在Spring MVC框架中,`mvc:annotation-driven`和`mvc:message-converters`是两个非常重要的元素,它们在处理基于注解的控制器和数据转换方面起着关键作用。本篇文章将深入探讨这两个组件的工作原理以及如何在实际...

    JSF2整合Spring3------JSF学习笔记4

    要在JSF Managed Bean中使用Spring bean,我们需要使用`@ManagedProperty`注解,并指定Spring bean的ID。例如,在JSF的Managed Bean `UserBean`中注入`UserService`: ```java import javax.faces.bean.Managed...

    SPRING MVC3.2案例讲解---配置

    `mvc:annotation-driven`则是开启Spring MVC的注解驱动,支持我们在Controller方法上使用@RequestMapping等注解。 除了基本配置,我们还可以配置拦截器(Interceptor)、异常处理器(HandlerExceptionResolver)...

    spring源码雷神spring源码雷神

    在压缩包文件中,`.classpath`、`spring-annotation.iml`、`.project`、`.springBeans`、`pom.xml`、`target`、`.settings`、`src`、`.idea`等文件代表的是一个标准的Spring项目结构: - `.classpath`和`.project`...

    spring-framework-4.3.0-rc1源码

    IoC容器的核心类`org.springframework.beans.factory.BeanFactory`和`org.springframework.context.ApplicationContext`提供了bean的实例化、初始化、装配和管理功能。通过`ApplicationContext`,我们可以获取到bean...

    spring[1]-使用ImportBeanDefinitionRegistrar自定义注册bean(基于源码).zip

    import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.type.AnnotationMetadata; ...

    Java Bean 遍历和使用注解给Bean的属性添加属性值

    - **Spring Framework**:Spring提供了`org.springframework.beans.BeanWrapper`和`org.springframework.beans.PropertyAccessor`接口,简化了对Bean的操作,包括注解的处理。 在实际开发中,遍历Java Bean和利用...

    spring_MVC源码

    弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...

    Spring中的@Transactional事物回滚实例源码

    1. **XML配置**:在Spring的配置文件中,使用`&lt;tx:annotation-driven&gt;`元素启用基于注解的事务管理。 2. **Java配置**:在@Configuration类中,使用@EnableTransactionManagement注解开启事务管理,并可以使用@Bean...

    Spring 自定义注解的解析

    在Spring框架中,自定义注解的解析是一个强大的特性,允许开发者根据业务需求创建特定的注解,并在Spring容器启动时自动处理这些注解。本文将深入探讨如何在Spring环境中通过`component-scan`配置来处理自定义Java...

    Spring的Autowired自动装配(XML版本+Annotation版本+源码+解析)

    在Java开发领域,Spring框架是应用最广泛的轻量级框架之一,它提供了强大的依赖注入(Dependency Injection,简称DI)功能,极大地简化了企业级应用的开发。本篇将重点讲解Spring的@Autowired自动装配机制,包括XML...

    spring-framework-5.3.29.tar.gz

    2. 依赖注入(Dependency Injection):在`org.springframework.beans.factory.annotation`包下,有如AutowiredAnnotationBeanPostProcessor等类,它们实现了基于注解的依赖注入。 3. AOP代理(AOP Proxy):在`org...

    spring-扩展点-namespacehandler(Spring自定义标签)

    例如,`&lt;mvc:annotation-driven&gt;`标签会注册一些关键的Bean,如`HandlerMapping`和`HandlerAdapter`,它们使得Spring MVC能识别并处理带有注解的控制器方法。 在Spring框架中,自定义`NamespaceHandler`的实现一般...

    Spring3.0源码2

    源码中,`org.springframework.beans.factory` 包下的`BeanFactory`和`ApplicationContext`接口是实现DI的关键,它们负责创建、管理和查找 beans。通过`@Autowired`注解,Spring可以自动将依赖注入到bean中,而无需...

    spring3.2.14源码zip

    对于注解驱动的装配,`org.springframework.context.annotation`包提供了`@Component`、`@Service`、`@Repository`和`@Controller`等注解,以及对应的扫描和处理逻辑。 除此之外,Spring的JDBC抽象层简化了数据库...

    Spring Boot集成Mybatis(注解)

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql....

    Spring AOP配置源码

    &lt;/beans&gt;&lt;context:annotation-config/&gt; &lt;context:component-scan base-package="com.spring.*"/&gt; 两行为开启spring的注解配置 &lt;aop:aspect id="aspect" ref="logIntercepter"&gt; 引入具体的AOP操作类 (* ...

Global site tag (gtag.js) - Google Analytics