spring bean parse

BeanDefinitionParserDelegate

解析<bean id="" name=""  name中的内容可以用“,”,“:”,“  ”作为分隔符

如果id为空,就将name中的第一个值赋给id

Entry-->BeanEntry

public final class ParseState {

/**
     * Tab character used when rendering the tree-style representation.
     */
    private static final char TAB = ‘\t‘;

/**
     * Internal {@link Stack} storage.
     */
    private final Stack<Entry> state;

/**
     * Create a new {@code ParseState} with an empty {@link Stack}.
     */
    public ParseState() {
        this.state = new Stack<Entry>();
    }

/**
     * Create a new {@code ParseState} whose {@link Stack} is a {@link Object#clone clone}
     * of that of the passed in {@code ParseState}.
     */
    @SuppressWarnings("unchecked")
    private ParseState(ParseState other) {
        this.state = (Stack<Entry>) other.state.clone();
    }

/**
     * Add a new {@link Entry} to the {@link Stack}.
     */
    public void push(Entry entry) {
        this.state.push(entry);
    }

/**
     * Remove an {@link Entry} from the {@link Stack}.
     */
    public void pop() {
        this.state.pop();
    }

/**
     * Return the {@link Entry} currently at the top of the {@link Stack} or
     * {@code null} if the {@link Stack} is empty.
     */
    public Entry peek() {
        return this.state.empty() ? null : this.state.peek();
    }

/**
     * Create a new instance of {@link ParseState} which is an independent snapshot
     * of this instance.
     */
    public ParseState snapshot() {
        return new ParseState(this);
    }

/**
     * Returns a tree-style representation of the current {@code ParseState}.
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int x = 0; x < this.state.size(); x++) {
            if (x > 0) {
                sb.append(‘\n‘);
                for (int y = 0; y < x; y++) {
                    sb.append(TAB);
                }
                sb.append("-> ");
            }
            sb.append(this.state.get(x));
        }
        return sb.toString();
    }

/**
     * Marker interface for entries into the {@link ParseState}.
     */
    public interface Entry {

}

}

public class BeanEntry implements ParseState.Entry {

private String beanDefinitionName;

/**
     * Creates a new instance of {@link BeanEntry} class.
     * @param beanDefinitionName the name of the associated bean definition
     */
    public BeanEntry(String beanDefinitionName) {
        this.beanDefinitionName = beanDefinitionName;
    }

@Override
    public String toString() {
        return "Bean ‘" + this.beanDefinitionName + "‘";
    }

}

BeanDefinition的实例:GenericBeanDefinition bd = new GenericBeanDefinition();

AbstractBeanDefinition-->BeanMetadataAttributeAccessor

public class BeanMetadataAttributeAccessor extends AttributeAccessorSupport implements BeanMetadataElement {

private Object source;

........

}

public abstract class AttributeAccessorSupport implements AttributeAccessor, Serializable {

/** Map with String keys and Object values */
    private final Map<String, Object> attributes = new LinkedHashMap<String, Object>(0);

....

}

BeanMetadataAttribute attribute :

public class BeanMetadataAttribute implements BeanMetadataElement {

private final String name;

private final Object value;

private Object source;

}

LookupOverride:

public class LookupOverride extends MethodOverride {

private final String beanName;

private Method method;
.......}

public abstract class MethodOverride implements BeanMetadataElement {

private final String methodName;

private boolean overloaded = true;

private Object source;

...........}

public class MethodOverrides {

private final Set<MethodOverride> overrides = new LinkedHashSet<MethodOverride>(0);

......}

ReplaceOverride:

public class ReplaceOverride extends MethodOverride {

private final String methodReplacerBeanName;

private List<String> typeIdentifiers = new LinkedList<String>();

。。。。。}

constructor-arg:只有一个子元素(must not contain more than one sub-element)

is only allowed to contain either ‘ref‘ attribute OR ‘value‘ attribute OR sub-element

ref作为属性,则转换成RuntimeBeanReference

public class RuntimeBeanReference implements BeanReference {

private final String beanName;

private final boolean toParent;

private Object source;

。。。。}

如果value作为属性则转换成:

public class TypedStringValue implements BeanMetadataElement {

private String value;

private volatile Object targetType;

private Object source;

private String specifiedTypeName;

private volatile boolean dynamic;

。。。。。}

构造函数解析存放的对象:

public class ConstructorArgumentValues {

private final Map<Integer, ValueHolder> indexedArgumentValues = new LinkedHashMap<Integer, ValueHolder>(0);

private final List<ValueHolder> genericArgumentValues = new LinkedList<ValueHolder>();

。。。。}

对应<constructor index....   利用indexedArgumentValues 存放

没有index的利用genericArgumentValues存放

Property解析:

public class PropertyValue extends BeanMetadataAttributeAccessor implements Serializable {

private final String name;

private final Object value;

private Object source;

private boolean optional = false;

private boolean converted = false;

private Object convertedValue;

/** Package-visible field that indicates whether conversion is necessary */
    volatile Boolean conversionNecessary;

/** Package-visible field for caching the resolved property path tokens */
    transient volatile Object resolvedTokens;

.......}

其中PropertyValue 中的value保存的类型就是ManagedList

public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor
        implements BeanDefinition, Cloneable {

.........

private MutablePropertyValues propertyValues;

.........

} 作为存储默认元素的属性

Qualifier子元素的解析:

public class AutowireCandidateQualifier extends BeanMetadataAttributeAccessor {

public static String VALUE_KEY = "value";

private final String typeName;

......

}

public class BeanMetadataAttributeAccessor extends AttributeAccessorSupport implements BeanMetadataElement {

private Object source;

..............

}

public abstract class AttributeAccessorSupport implements AttributeAccessor, Serializable {

/** Map with String keys and Object values */
    private final Map<String, Object> attributes = new LinkedHashMap<String, Object>(0);

........

}

				
时间: 2024-10-18 20:42:10

spring bean parse的相关文章

spring bean源码简单解析

最近在看spring的源码,发现看这个还是有点早,看的很吃力,有多东西还不是很明白,像代理等, 我感觉spring用abstract模板来写主要功能,用接口来拓展功能,用的出神入化,但也让很多简单 的东西变得不那么好懂了,就是写的啰嗦了,个人感觉.下面就是下spring bean源码的学习: private static final Resource RETURNS_NULL_CONTEXT = qualifiedResource(CLASS, "returnsNull.xml");

Spring - Bean Definition Inheritance

A bean definition can contain a lot of configuration information, including constructor arguments, property values, and container-specific information such as initialization method, static factory method name, and so on. A child bean definition inher

Spring点滴四:Spring Bean生命周期

Spring Bean 生命周期示意图: 了解Spring的生命周期非常重要,我们可以利用Spring机制来定制Bean的实例化过程. --------------------------------------------------------------------------------------------------------------------------------------------------- spring-service.xml: <?xml version=

非spring组件servlet、filter、interceptor中注入spring bean

问题:在filter和interceptor中经常需要调用Spring的bean,filter也是配置在web.xml中的,请问一下这样调用的话,filter中调用Spring的某个bean,这个bean一定存在吗?现在总是担心filter调用bean的时候,bean还没被实例化? 答案:因为spring bean.filter.interceptor加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter.最终得出

Spring Bean

一.Spring的几大模块:Data access & Integration.Transcation.Instrumentation.Core Spring Container.Testing. 二.Spring Bean 2.1.声明Bean a.简单的bean装配方式 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework

AspectJ切面具体是什么时候实例化的?实在Spring Bean之前还是之后?

问题出于<Spring In Action>中4.5节 注入AspectJ切面时,看的不是太懂! 不懂得地方是:    AspectJ切面中需要注入Spring的Bean,那么AspectJ切面具体是什么时候实例化的?是在Spring Bean之前还是之后?

Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域

//从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com/hsp/beans.xml" ) ; ac.getBean("beanId"); 当我们去实例化beans.xml,该文件中配置的 bean 就被实例化(不论你用还是不用,bean对象都在那),而且该对象是singleton单例的.(每个bean都有scope属性,可以人为的设

Spring Bean 注册方式小结

IOC容器 Spring的核心是一个IOC容器,管理着我们向容器注册的所有bean.下面我们来看下两种向容器注册bean的方式, 通过BeanDefinitionReader读取Spring Bean的配置文件,解析然后注册: 通过ClassPathBeanDefinitionScanner直接扫描带有Spring Bean注解的Java类并注册: Reader BeanDefinitionReader的使用方式如下, import org.springframework.beans.facto

Spring Bean的生命周期

Bean的初始化过程已经被Spring完全包装起来了,无法人工干预. Spring预留了两个回调方法的入口 回调方法:定义出来完成一定的功能,提供给回调者/服务器/上层容器调用的方法,叫做回调方法. Bean类 public class Bean1 { public Bean1() { //System.out.println("bean1...构造方法"); } public void show(){ System.out.println("bean1...方法"