Spring core learning (5) inject beans into Bean-Parse dependencies, beanbean-

Source: Internet
Author: User

Spring core learning (5) inject beans into Bean-Parse dependencies, beanbean-

Preface: start to learn the core idea of Spring and learn it through a simplified version of Spring code.


Content: 1. BeanReference-save Bean reference. 2. Call createBean ()-lazy-init in getBean. This time we used Bean injection in Bean. Here we have rewritten AbstractBeanFactory again. After rewriting, AbstractBeanFactory will have an additional List to save all registered object names, in addition, we no longer create beans during registration, and inject beanDefinition beans to specific implementations to implement lazy loading, because we know that this bean will not be used after registration, therefore, when we put getBean (), we will instantiate the bean. In AbstractBeanFactory, preInstantiateSingletons () can initialize all beans. In the AutowireCapableBeanFactory class, the applyPropertyValues () function should be noted, here we need more injection referenced in the class.


BeanReference:

public class BeanReference {private String name;private Object bean;public BeanReference(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Object getBean() {return bean;}public void setBean(Object bean) {this.bean = bean;}}
BeanFactory:

public interface BeanFactory {Object getBean(String name) throws Exception;void registerBeanDefinition(String name, BeanDefinition beanDefinition) throws Exception;}
AbstractBeanFactory:

Public abstract class AbstractBeanFactory implements BeanFactory {private Map <String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap <String, BeanDefinition> (); private final List <String> beanDefinitionNames = new ArrayList <String> (); @ Overridepublic Object getBean (String name) throws Exception {BeanDefinition beanDefinition = beanDefinitionMap. get (name); if (beanDefinition = null) throw new IllegalArgumentException ("No bean named" + name + "is defined"); Object bean = beanDefinition. getBean (); if (bean = null) bean = doCreateBean (beanDefinition); return bean;} @ Overridepublic void registerBeanDefinition (String name, BeanDefinition beanDefinition) throws Exception {// TODO no longer creates beans during registration, and injects beanDefinition beans to specific implementations to implement lazy beanDefinitionMap loading. put (name, beanDefinition); beanDefinitionNames. add (name);} public void preInstantiateSingletons () throws Exception {for (Iterator <String> it = this. beanDefinitionNames. iterator (); it. hasNext ();) {String beanName = (String) it. next (); getBean (beanName) ;}}/*** initialize bean * @ param beanDefinition * @ return */protected abstract Object doCreateBean (BeanDefinition beanDefinition) throws Exception ;}

AutowireCapableBeanFactory:

/*** BeanFactory * @ author acer **/public class AutowireCapableBeanFactory extends AbstractBeanFactory {@ Overrideprotected Object doCreateBean (BeanDefinition beanDefinition) throws Exception {Object bean = createBeanInstance (beanDefinition); // notice, we will complete bean injection beanDefinition here. setBean (bean); applyPropertyValues (bean, beanDefinition); return bean;} protected Object createBeanInstance (BeanDefinition beanDefinition) throws Exception {return beanDefinition. getBeanClass (). newInstance ();} protected void applyPropertyValues (Object bean, BeanDefinition mbd) throws Exception {for (PropertyValue propertyValue: mbd. getPropertyValues (). getPropertyValueList () {Field declaredField = bean. getClass (). getDeclaredField (propertyValue. getName (); declaredField. setAccessible (true); Object value = propertyValue. getValue (); if (value instanceof BeanReference) {BeanReference beanReference = (BeanReference) value; value = getBean (beanReference. getName ();} declaredField. set (bean, value );}}}




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.