Spring loaded bean of learning

Following last a long time did not update registration bean, two days Available data also own search of the investigation looked getBean see the spring BeanFactory (beanName); this method. Due to the limited time that can not be like before copying the code line by line and write notes, this is just to copy some of the ways out of the process of annotating the code.

Spring version is 5.1.3.RELEASE

The first is getBean (String beanName);

Person person = (Person) context.getBean("person");

Object the getBean 1.public (String name);
1.1.doGetBean (name, null, null, to false);
1.1.1.final the beanName String = transformedBeanName (name); corresponding to first acquire the beanName
1.1.2.Object sharedInstance = getSingleton (the beanName); check whether the cache exists inside the created bean involves several Map
private final Map <String, Object> = new new singletonObjects of ConcurrentHashMap <> (256); Key: the beanName value: bean instance
private final Map < String, Object> earlySingletonObjects = new HashMap <> (16); Key: beanName value: bean instance for storing difference singletonObjects ObjectFactory.getObject (); bean instance returned
private final Map <String, ObjectFactory < >> singletonFactories? = new HashMap <> (16) ; Key: beanName value: ObjectFactory single cache factories
1.1.3.if (isPrototypeCurrentlyInCreation (beanName)); if the current bean is being created directly thrown new new BeanCurrentlyInCreationException the throw (beanName);
1.1.4.BeanFactory parentBeanFactory = getParentBeanFactory (); Get the parent container parentBeanFactory current bean definitions exist For example, spring springmvc father and container
1.1.5.markBeanAsCreated (beanName); mark the current bean is creating branded identity
private final Set alreadyCreated = Collections.newSetFromMap (new ConcurrentHashMap <> (256)); storage is already created in beanName
Private Final the Map <String, RootBeanDefinition> mergedBeanDefinitions = new ConcurrentHashMap <> (256); Key: beanName value: beanDefinition storage Yes Yes beanName already created and the bean definition
1.1.6.final RootBeanDefinition mbd = getMergedLocalBeanDefinition (beanName) ; beanName obtained the corresponding beanDefinition obtained from mergedBeanDefinitions, for the first time is to return a new RootBeanDefinition
1.1.7.checkMergedBeanDefinition (mbd, beanName, args) ; Check if the bean is an abstract class thrown new new BeanIsAbstractException the throw (the beanName);
1.1.8.String [] The dependsOn mbd.getDependsOn = (); acquisition process whether If you need to rely on bean dependsOn elements traversal calls getBean (DEP);
1.1.9.if (mbd.isSingleton ()) if the bean is a singleton, then directly create
1.2.0.getSingleton (String beanName, ObjectFactory singletonFactory < ?> ); calls getSingleton method to create bean, the main core in createBean second parameter (beanName, mbd, args); method

public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory);

Object getSingleton 2.public (String beanName, the ObjectFactory singletonFactory <?>);
2.1.Object singletonObject = this.singletonObjects.get (beanName); attempts to obtain if the acquisition is less than the destruction of the bean and the exception is thrown throw from the cache BeanCreationNotAllowedException new new (beanName, the Message);
Private singletonsCurrentlyInDestruction boolean = false; the destruction of identity
2.2.beforeSingletonCreation (beanName); join to create the bean bean cache singleton
2.3.singletonObject = singletonFactory.getObject (); to focus on the direct call ObjectFactory .createBean (beanName, mbd, args) ; method to create bean
2.4.addSingleton (beanName, singletonObject); and finally turn will create good bean into the cache
private final Set registeredSingletons = new LinkedHashSet < > (256); storage is created the beanName

protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args);

3.1.Class resolvedClass = resolveBeanClass (mbd, beanName ) <?>; Bean obtain the corresponding class
3.2.mbdToUse.prepareMethodOverrides (); if the bean has set an override method
3.3.Object bean = resolveBeforeInstantiation (beanName, mbdToUse ); in before the bean is instantiated, check whether you need to return proxy proxy object.
3.4.Object beanInstance = doCreateBean (beanName, mbdToUse , args); instantiate bean

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args);

4.1.instanceWrapper = createBeanInstance (beanName, mbd, args); bean is created which is used to call the constructor creates an instance of the reflector bean
4.2.applyMergedBeanDefinitionPostProcessors (MBD, BeanType, the beanName); get all the elements which are not determined BeanPostProcessor MergedBeanDefinitionPostProcessor, then call MergedBeanDefinitionPostProcessor .postProcessMergedBeanDefinition (MBD, BeanType, the beanName);
4.3.populateBean (the beanName, MBD, instanceWrapper); bean to function initializes data
4.4.initializeBean (beanName, exposedObject, mbd) ; bean perform some post-processing (BeanNameAware, BeanClassLoaderAware, BeanFactoryAware , the BeanPostProcessor);
4.5.registerDisposableBeanIfNecessary (beanName, bean, MBD); registration destroyed bean callback

protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw);

(! Mbd.isSynthetic () && hasInstantiationAwareBeanPostProcessors ( )) 5.1.if to when this step is complete, but not yet bean instance Begin attribute value set, taking all InstantiationAwareBeanPostProcessor implementation class calls postProcessAfterInstantiation (bw.getWrappedInstance (), beanName) ; InstantiationAwareBeanPostProcessor bean implementation class may modify the state where
5.2.PropertyValues pvs = (mbd.hasPropertyValues () mbd.getPropertyValues ():? null); all of the properties of the bean extracted
5.3.autowireByName (beanName, mbd, bw, newPvs ); find all property values by name
5.4.autowireByType (beanName, MBD, BW, newPvs); find all the property's value of the type
5.5.for (the BeanPostProcessor BP: getBeanPostProcessors ()) acquired all of the BeanPostProcessor
5.6.if (BP instanceof InstantiationAwareBeanPostProcessor) find all types of InstantiationAwareBeanPostProcessor
5.7.pvsToUse = ibp.postProcessPropertyValues (pvs, filteredPds, bw.getWrappedInstance (), beanName); @Autowired dependent annotation is provided using the value field.set (the bean, value);
5.8.if (! = Null PVS) applyPropertyValues (beanName, mbd, bw, pvs ); set attribute values of the bean

protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd);

6.1.invokeAwareMethods (beanName, bean); if the bean implements the Aware (BeanNameAware, BeanClassLoaderAware, BeanFactoryAware) an interface corresponding to the call set assignment method
6.2.wrappedBean = applyBeanPostProcessorsBeforeInitialization (wrappedBean, beanName) ; all of the acquired call the corresponding BeanPostProcessor Current = processor.postProcessBeforeInitialization Object (Result, the beanName);
6.3.invokeInitMethods (the beanName, wrappedBean, MBD); if the bean implements the interface to call the corresponding InitializingBean ((InitializingBean) bean) .afterPropertiesSet ();
6.4.invokeCustomInitMethod (the beanName , bean, mbd); call init-method from bean methods defined
6.5.wrappedBean = applyBeanPostProcessorsAfterInitialization (wrappedBean, beanName) ; all of the acquired call the corresponding BeanPostProcessor Object current = processor.postProcessAfterInitialization (result, beanName );

The six-step implementation of End after a spring loaded bean has ended up layer by layer to return. Help their memory consolidation, if you write deficiencies pointed Thank you hope to watch!

Note

id and name

When the configuration of each bean has a unique name and multiple aliases, when you can get bean can also alias by beanName

beanFactory.getBean("beanName or alias");

The result is the following configuration: beanName as a person, there are three aliases, respectively, p1, p2, p3.

<bean id="person" name="p1, p2, p3" class="com.feil.springtest.ioc.test01.Person">

The result is the following configuration: beanName as p1, there are two aliases, respectively, p2, p3.

<bean name="p1, p2, p3" class="com.javadoop.example.MessageServiceImpl" />

The result is the following configuration: beanName as: com.feil.springtest.ioc.test01.Person # 0,

<bean class="com.feil.springtest.ioc.test01.Person" />

The result is the following configuration: beanName as a person, not an alias.

<bean id="person" class="com.feil.springtest.ioc.test01.Person" />
profile

We can configure different environments are configured into separate files

<beans profile="dev" ....>
	<bean ... />
	<bean ... />
	<bean ... />
</beans>
<beans profile="test" ....>
	<bean ... />
	<bean ... />
	<bean ... />
</beans>

Of course, we can also use a configuration file:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="...">

    <beans profile="dev">
        ...
    </beans>

    <beans profile="test">
        ...
    </beans>
</beans>

So spring is how to identify different environment? Spring in the process of starting, will look for "spring.profiles.active" property value, according to the value of the property. How to configure this value that it?
Spring will look for property values spring.profiles.active in a few places: the operating system environment variables, JVM system variables, parameters defined in web.xml, JNDI.
The easiest way to be sure is to specify when the program starts:

-Dspring.spring.profiles.active="dev"
BeanPostProcessor

BeanPostProcessor interface role:
if we want to complete bean is instantiated, configured in the Spring container and other initialization methods before and want to add some of your own logic. We need to define one or more classes BeanPostProcessor interface, and then to register Spring IoC container.
BeanPostProcessor concept is more important in Spring. We look interface definition:

public interface BeanPostProcessor {
    @Nullable
    default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Nullable
    default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}

1: postProcessorBeforeInitailization method postprocessor before initialization is performed bean postProcessBeforeInitialization this method.
2: postProcessorAfterInitailization method postprocessor that after completion of the initialization bean postProcessAfterInitialization executes this method.
Look at the first method, the first parameter of this method accepts a bean instance in the return value will be the focus as the new bean instance, What does that mean? It is easy to think of, we can do something here for some bean instances we want modified. But for the Spring framework, it will decide whether to return the bean instance in this method agent.

Guess you like

Origin blog.csdn.net/qq_16830879/article/details/89921794
Recommended