上述代码中的annDef.getMetadata().getAnnotationAttributes方法就是获取对象中指定类型的注解的值。
(3).AnnotationConfigUtils处理注解Bean定义类中的通用注解:
AnnotationConfigUtils类的processCommonDefinitionAnnotations在向容器注册Bean之前,首先对注解Bean定义类中的通用Spring注解进行处理,源码如下:
[java]- //处理Bean定义中通用注解 static void processCommonDefinitionAnnotations(AnnotatedBeanDefinition abd) {
- //如果Bean定义中有@Primary注解,则为该Bean设置为autowiring自动依赖注入//装配的首选对象 if (abd.getMetadata().isAnnotated(Primary.class.getName())) {
- abd.setPrimary(true); }
- //如果Bean定义中有@Lazy注解,则将该Bean预实例化属性设置为@lazy注解的值 if (abd.getMetadata().isAnnotated(Lazy.class.getName())) {
- Boolean value = (Boolean) abd.getMetadata().getAnnotationAttributes(Lazy.class.getName()).get(value); abd.setLazyInit(value);
- } //如果Bean定义中有@ DependsOn注解,则为该Bean设置所依赖的Bean名称,
- //容器将确保在实例化该Bean之前首先实例化所依赖的Bean if (abd.getMetadata().isAnnotated(DependsOn.class.getName())) {
- String[] value = (String[]) abd.getMetadata().getAnnotationAttributes(DependsOn.class.getName()).get(value); abd.setDependsOn(value);
- } }
(4).AnnotationConfigUtils根据注解Bean定义类中配置的作用域为其应用相应的代理策略:
AnnotationConfigUtils类的applyScopedProxyMode方法根据注解Bean定义类中配置的作用域@Scope注解的值,为Bean定义应用相应的代理模式,主要是在Spring面向切面编程(AOP)中使用。源码如下:
[java]- //根据作用域为Bean应用引用的代码模式 static BeanDefinitionHolder applyScopedProxyMode(
- ScopeMetadata metadata, BeanDefinitionHolder definition, BeanDefinitionRegistry registry) { //获取注解Bean定义类中@Scope注解的proxyMode属性值
- ScopedProxyMode scopedProxyMode = metadata.getScopedProxyMode(); //如果配置的@Scope注解的proxyMode属性值为NO,则不应用代理模式
- if (scopedProxyMode.equals(ScopedProxyMode.NO)) { return definition;
- } //获取配置的@Scope注解的proxyMode属性值,如果为TARGET_CLASS,则返
- //回true,如果为INTERFACES,则返回false boolean proxyTargetClass = scopedProxyMode.equals(ScopedProxyMode.TARGET_CLASS);
- //为注册的Bean创建相应模式的代理对象 return ScopedProxyCreator.createScopedProxy(definition, registry, proxyTargetClass);
- }
这段为Bean引用创建相应模式的代理,如果在Spring面向切面编程(AOP)中涉及到再详细分析,这里不做深入的分析。
(5).BeanDefinitionReaderUtils向容器注册Bean:
BeanDefinitionReaderUtils向容器注册载入的Bean我们在第4篇博客中已经分析过,主要是校验Bean定义,然后将Bean添加到容器中一个管理Bean定义的HashMap中,这里就不做分析。
4.AnnotationConfigApplicationContext扫描指定包及其子包下的注解Bean:
当创建注解处理容器时,如果传入的初始参数是注解Bean定义类所在的包时,注解容器将扫描给定的包及其子包,将扫描到的注解Bean定义载入并注册。
(1).Spring中常用的注解:
a.Component注解:
[java]- @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME)
- @Documented public @interface Component {
- String value() default ; }
b.Service注解:
[java]- @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME)
- @Documented @Component
- public @interfac