Entry – When is the bean instance object created?

The first time you use it, you create an instance object of the bean by calling the method of the container class that gets the bean. BeanDefinition is just a mapping of XML beans, but no actual bean object instance has been created yet.

Call the get bean method of the container class

Create an instance object of the bean

In general, they are based on reflection, which can be verified at source break points, and create bean instance objects based on reflection.www.cnblogs.com/javastack/p…

Blog.csdn.net/dhfzhishi/a…

What is the source implementation of creating bean instance objects and injecting data?


Which method is it?

1. Create an instance of the bean using createBean().

2. The method for injecting data is populateBean()

Core classes SimpleInstantiationStrategy

The core steps are

1. Entrance is called container classes get bean method 2. But at the end of the specific method of creating bean instance objects in SimpleInstantiationStrategy class

The source code

/** * Simple object instantiation strategy for use in a BeanFactory. * * <p>Does not support Method Injection, although it provides hooks for subclasses * to override to add Method Injection support, For example by primary methods. * * @author Rod Johnson * @author Juergen Hoeller * @since 1.1 */ public class SimpleInstantiationStrategy implements InstantiationStrategy { @Override public Object instantiate(RootBeanDefinition bd //BeanDefinition @nullable String beanName //Bean name, BeanFactory owner) { // Don't override the class with CGLIB if no overrides. if (! bd.hasMethodOverrides()) { Constructor<? > constructorToUse; synchronized (bd.constructorArgumentLock) { constructorToUse = (Constructor<? >) bd.resolvedConstructorOrFactoryMethod; if (constructorToUse == null) { final Class<? > clazz = bd.getBeanClass(); if (clazz.isInterface()) { throw new BeanInstantiationException(clazz, "Specified class is an interface"); } try { if (System.getSecurityManager() ! = null) { constructorToUse = AccessController.doPrivileged( (PrivilegedExceptionAction<Constructor<? >>) clazz::getDeclaredConstructor); } else { constructorToUse = clazz.getDeclaredConstructor(); } bd.resolvedConstructorOrFactoryMethod = constructorToUse; } catch (Throwable ex) { throw new BeanInstantiationException(clazz, "No default constructor found", ex); } } } return BeanUtils.instantiateClass(constructorToUse); / / based on the reflection to create bean instance object} else {/ / Must generate additional ttf_subclass. Return instantiateWithMethodInjection (bd, beanName, owner); // Create bean object based on dynamic proxy cglib}}Copy the code
/** * Static convenience methods for JavaBeans: for instantiating beans, * checking bean property types, copying bean properties, etc. * * <p>Mainly for use within the framework, but to some degree also * useful for application classes. * * @author Rod Johnson * @author Juergen Hoeller * @author Rob Harrop * @author Sam Brannen * @author Sebastien Deleuze */ public abstract class BeanUtils { /** * Convenience method to instantiate a class using the given constructor. * <p>Note that this method tries to set the constructor accessible if given a * non-accessible (that is, non-public) constructor, and supports Kotlin classes * with optional parameters and default values. * @param ctor the constructor to instantiate * @param args the constructor arguments to apply (use {@code null} for an unspecified * parameter if needed for Kotlin classes with optional parameters and default values) * @return the new instance * @throws BeanInstantiationException if the bean cannot be instantiated * @see Constructor#newInstance */ public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException { Assert.notNull(ctor, "Constructor must not be null"); try { ReflectionUtils.makeAccessible(ctor); return (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(ctor.getDeclaringClass()) ? KotlinDelegate.instantiateClass(ctor, args) : ctor.newInstance(args)); / / ctor. NewInstance (args), In the constructor of the class instance is created in the reflection object} catch (InstantiationException ex) {throw new BeanInstantiationException (ctor, "Is it an abstract class?" , ex); } catch (IllegalAccessException ex) { throw new BeanInstantiationException(ctor, "Is the constructor accessible?" , ex); } catch (IllegalArgumentException ex) { throw new BeanInstantiationException(ctor, "Illegal arguments for constructor", ex); } catch (InvocationTargetException ex) { throw new BeanInstantiationException(ctor, "Constructor threw exception", ex.getTargetException()); }}Copy the code

Class diagram

One of the core steps of the container is to create beans

When to use additional?

1. In most cases, create objects based on reflection using reflection constructor classes to create instance objects.

2. Special cases such as interceptors create instance objects based on the dynamic proxy cglib. Why? Because only interceptors need to enhance the bean’s functionality, enhancements are common non-business functions performed before and after the bean. Dynamic proxies are designed to do just that, intercepting business methods and performing common non-business functions before and after business methods. To put it bluntly, an interceptor is a dynamic proxy, and a dynamic proxy is an interceptor. The word proxy for dynamic proxy does exactly the same thing as the word interception.

The interview

1. Inject data based on reflection

2. Interceptors are based on dynamic proxies cglib

The essence of that is because of what we’re going to talk about today.