Spring beans source code interpretation-Bean definition and packaging

Source: Internet
Author: User
Tags propertyaccessor

Spring beans source code interpretation-Bean definition and packaging
Bean definition, packaging is the basis of java bean. I can't emphasize the importance of this code too much. Therefore, a deep understanding of this piece of code can play a multiplier effect on future code research. 1. Bean definition BeanDefinition 1.1 BeanDefinition function a BeanDefinition describes a bean instance, including attribute values, constructor parameter values, and more information about the classes inherited from it. BeanDefinition is just the simplest interface. Its main function is to allow BeanFactoryPostProcessor, such as PropertyPlaceHolderConfigure, to retrieve and modify the metadata of attribute values and other beans. 1.2 BeanDefinition: AttributeAccessor, BeanMetadataElement sub-interface: AbstractBeanDefinition, delimiter, ChildBeanDefinition, GenericBeanDefinition, RootBeanDefinition, and so on, the AttributeAccessor interface defines the most basic modification or retrieval of metadata of any Object. The main methods are: String [] attributeNames () Object getAttribute (String name) boolean hasAttribute (String name) Objec T removeAttribute (String name) void setAttribute (String name, Object value) BeanMetadataElement interface provides a getResource () method to transmit a configurable source Object. 1.3 BeanDefinition abstract class AbstractBeanDefinition public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor implements BeanDefinition, Cloneable {} Where the Terminate interface implements the getResource () provided by BeanMetadataElement Interface () the AttributeAccessorSupport method also provides the buteaccessorsupport Method for adding, deleting, modifying, and querying attributes. Public class BeanMetadataAttributeAccessor extends AttributeAccessorSupport implements BeanMetadataElement {} Let's take a look at the attributes involved in the creation of an abstract beanDefinition? Create a new default AbstractBeanDefinition, as shown in the following code. For more information, see BeanDefinitionDefaults. The second constructor is created based on the constructor parameter value and attribute parameter value. /*** Create a new AbstractBeanDefinition with default settings. */protected AbstractBeanDefinition () {this (null, null);}/*** Create a new AbstractBeanDefinition with the given * constructor argument values and property values. */protected AbstractBeanDefinition (ConstructorArgumentValues cargs, MutablePropertyValues pvs) {setConstructorArgumentValues (cargs); setPropertyValues (pvs);} third Constructor Is to deeply copy an original beandefinition. /*** Create a new AbstractBeanDefinition as a deep copy of the given * bean definition. * @ param original the original bean definition to copy from */protected AbstractBeanDefinition (BeanDefinition original) {setParentName (original. getParentName (); setBeanClassName (original. getBeanClassName (); setFactoryBeanName (original. getFactoryBeanName (); setFactoryMethodName (original. getfactorymethodnm E (); setScope (original. getScope (); setAbstract (original. isAbstract (); setLazyInit (original. isLazyInit (); setRole (original. getRole (); setConstructorArgumentValues (new ConstructorArgumentValues (original. getConstructorArgumentValues (); setPropertyValues (new MutablePropertyValues (original. getPropertyValues (); setSource (original. getSource (); copyAttributesFrom (original); if (original instance Of AbstractBeanDefinition) {AbstractBeanDefinition originalAbd = (AbstractBeanDefinition) original; if (originalAbd. hasBeanClass () {setBeanClass (originalAbd. getBeanClass ();} setAutowireMode (originalAbd. getAutowireMode (); setDependencyCheck (originalAbd. getDependencyCheck (); setDependsOn (originalAbd. getDependsOn (); setAutowireCandidate (originalAbd. isAutowireCandidate (); copyQualifiersFrom (o RiginalAbd); setPrimary (originalAbd. isPrimary (); setNonPublicAccessAllowed (originalAbd. isNonPublicAccessAllowed (); setLenientConstructorResolution (originalAbd. isLenientConstructorResolution (); setInitMethodName (originalAbd. getInitMethodName (); setEnforceInitMethod (originalAbd. isEnforceInitMethod (); setDestroyMethodName (originalAbd. getDestroyMethodName (); setEnforceDestroyMethod (originalAbd. I SEnforceDestroyMethod (); setMethodOverrides (new MethodOverrides (originalAbd. getMethodOverrides (); setSynthetic (originalAbd. isSynthetic (); setResource (originalAbd. getResource ();} else {setResourceDescription (original. getResourceDescription () ;}} the interfaces and classes involved in the above three methods include ConstructorArgumentValues: ConstructorArgumentValues, which saves all the parameter values of the constructor, it is usually used as part of bean definition. It not only supports common parameters for type matching, but also supports providing parameters based on the index location in the parameter list. Two variables are provided to save parameters: public class ConstructorArgumentValues {private final Map <Integer, ValueHolder> indexedArgumentValues = new LinkedHashMap <Integer, valueHolder> (0); private final List <ValueHolder> genericArgumentValues = new inclulist <ValueHolder> ();} MutablePropertyValues: Default implementation of the PropertyValues interface. Simple attribute operations are supported, provides deep replication for Constructor methods and supports using map to obtain constructor structures. 2. Bean packaging BeanWrapper 2.1 function: provides analysis and operation methods for standard javabean: Obtain and set attribute values in a single or batch, obtain attribute descriptors, and query the readability and writability of attributes. Supports nested attribute settings with no depth limit. 2.2 inheritance relationship: public interface BeanWrapper extends {} public interface extends PropertyAccessor, PropertyEditorRegistry, and TypeConverter {}. The delimiter interface encapsulates the configuration method of PropertyAccessor and inherits the PropertyEditorRegistry interface, provides methods to manage PropertyEditor. The inherent method of this interface: ConversionService getConversionService () returns the associated ConversionService, if any. Boolean isExtractOldValueForEditor () returns an identifier. True indicates that the old property value is extracted when you use the property editor to edit a property value. false indicates that the old value is not extracted. Void setConversionService (ConversionService conversionService) conversionservice is introduced from spring3.0 and can be used as an alternative to the java bean Attribute Editor to change the attribute value. Void setExtractOldValueForEditor (boolean extractOldValueForEditor) sets whether to extract the old attribute value identifier, as described above. In addition, the methods inherited by the callback interface include getPropertyType, struct, getPropertyValue, isReadableProperty, isWritableProperty, setPropertyValue, setPropertyValue, setPropertyValues, setPropertyValues, and attributes; methods inherited from PropertyEditorRegistry include: findCustomEditor, registerCustomEditor, and registerCustomEditor. Methods inherited from TypeConverter include: conver TIfNecessary, convertIfNecessary, and convertIfNecessary. 2.3 BeanWrapper implementation class: BeanWrapperImpl function: You can convert the values of the set and array to the set and array of the corresponding target object as needed. The custom property editor uses the setValue and setAsText methods of the property editor to implement the preceding conversion function. BeanWrapperImpl default PropertyEditor implementation: (PropertyEditorRegistrySupport. java) private void createDefaultEditors () {this. defaultEditors = new HashMap <Class <?>, PropertyEditor> (64); // Simple editors, without parameterization capabilities. // The JDK does not contain a default editor for any of these target types. this. defaultEditors. put (Charset. class, new CharsetEditor (); this. defaultEditors. put (Class. class, new ClassEditor (); this. defaultEditors. put (Class []. class, new ClassArrayEditor (); this. defaultEditors. put (Currency. class, new CurrencyEditor ()); This. defaultEditors. put (File. class, new FileEditor (); this. defaultEditors. put (InputStream. class, new InputStreamEditor (); this. defaultEditors. put (InputSource. class, new InputSourceEditor (); this. defaultEditors. put (Locale. class, new LocaleEditor (); this. defaultEditors. put (Pattern. class, new PatternEditor (); this. defaultEditors. put (Properties. class, new PropertiesEditor (); this. defaultEditors. Put (Resource []. class, new ResourceArrayPropertyEditor (); this. defaultEditors. put (TimeZone. class, new TimeZoneEditor (); this. defaultEditors. put (URI. class, new URIEditor (); this. defaultEditors. put (URL. class, new URLEditor (); this. defaultEditors. put (UUID. class, new UUIDEditor (); if (zoneIdClass! = Null) {this. defaultEditors. put (zoneIdClass, new ZoneIdEditor ();} // Default instances of collection editors. // Can be overridden by registering custom instances of those as custom editors. this. defaultEditors. put (Collection. class, new CustomCollectionEditor (Collection. class); this. defaultEditors. put (Set. class, new CustomCollectionEditor (Set. class); this. defaultEditors. put (SortedSet. class, n Ew CustomCollectionEditor (SortedSet. class); this. defaultEditors. put (List. class, new CustomCollectionEditor (List. class); this. defaultEditors. put (SortedMap. class, new CustomMapEditor (SortedMap. class); // Default editors for primitive arrays. this. defaultEditors. put (byte []. class, new ByteArrayPropertyEditor (); this. defaultEditors. put (char []. class, new CharArrayPropertyEditor (); // The JDK does n Ot contain a default editor for char! This. defaultEditors. put (char. class, new CharacterEditor (false); this. defaultEditors. put (Character. class, new CharacterEditor (true); // Spring's mmbooleaneditor accepts more flag values than the JDK's default editor. this. defaultEditors. put (boolean. class, new CustomBooleanEditor (false); this. defaultEditors. put (Boolean. class, new CustomBooleanEditor (true); // The JDK does not contain default Editors for number wrapper types! // Override JDK primitive number editors with our own CustomNumberEditor. this. defaultEditors. put (byte. class, new CustomNumberEditor (Byte. class, false); this. defaultEditors. put (Byte. class, new CustomNumberEditor (Byte. class, true); this. defaultEditors. put (short. class, new CustomNumberEditor (Short. class, false); this. defaultEditors. put (Short. class, new CustomNumberEditor (Short. class, true); this. DefaultEditors. put (int. class, new CustomNumberEditor (Integer. class, false); this. defaultEditors. put (Integer. class, new CustomNumberEditor (Integer. class, true); this. defaultEditors. put (long. class, new CustomNumberEditor (Long. class, false); this. defaultEditors. put (Long. class, new CustomNumberEditor (Long. class, true); this. defaultEditors. put (float. class, new CustomNumberEditor (Float. class, false ); This. defaultEditors. put (Float. class, new CustomNumberEditor (Float. class, true); this. defaultEditors. put (double. class, new CustomNumberEditor (Double. class, false); this. defaultEditors. put (Double. class, new CustomNumberEditor (Double. class, true); this. defaultEditors. put (BigDecimal. class, new CustomNumberEditor (BigDecimal. class, true); this. defaultEditors. put (BigInteger. class, new CustomNumbe REditor (BigInteger. class, true); // Only register config value editors if explicitly requested. if (this. configValueEditorsActive) {StringArrayPropertyEditor sae = new StringArrayPropertyEditor (); this. defaultEditors. put (String []. class, sae); this. defaultEditors. put (short []. class, sae); this. defaultEditors. put (int []. class, sae); this. defaultEditors. put (long []. class, sae) ;}}many editors are involved, so we will not go into detail here, If you are interested, you can search for it by yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.