A description

Now the development of springboot is more popular, but many developers have ignored l Springboot is based on spring and derived from the sub-project, do not understand spring how to play SpringBoot? Can you just rely on a few days of training and articles to play Springboot? The answer is definitely not, to understand springBoot must be able to spring, the foundation is very important, important words can not say too much; The author wrote the basic article and conceptual understanding of Spring is also a very important purpose, that is to prepare for the subsequent analysis of the source code, if all of a sudden on the source code, I believe that many people will be involved in, do not know what is going on, so honest learning is the only way to improve technology. Interested developers or students can follow my Sping column.

The second bean profile

Since the beans package and the core concepts have been covered in previous articles, there is no need to go over them. Spring IOC Containers can hold one or more Beans. Beans are created with configuration Metadata, which was mentioned in the previous article. The original configuration is XML.

These beans are defined to correspond to BeanDefinition Objects. The following source word; A BeanDefinition (interface) is an instance of a bean that has attribute values, constructor parameter values, and deeper information provided by its concrete implementer.

A BeanDefinition describes a bean instance, which has property values,constructor argument values, and further information supplied by concrete implementations.

Example of BeanDefinition metadata:

  1. Declaration cycle and scope of beans (SCOP,lifecycle Callbacks)
  2. Package-qualified class name: Usually the full class name of the real class
  3. In containers, there are direct cooperative dependencies.
  4. Configure additional information at object creation time, such as using beans to manage connection pools, limiting the size of connection pools, and so on.

Metadata is usually converted to attributes:

  1. Class (bean full class name)
  2. Name (bean name)
  3. Scope (scope)
  4. Constructor arguments
  5. Properties
  6. Autowiring Mode (Automatic configuration mode)
  7. Lazy-initialization mode
  8. Initialization Method (Bean initialization method)
  9. Destruction Method (bean)

You can also use ApplicationContext Implementers (such as using getBeanFactory () to obtain DefaultListableBeanFactory) through registerSingleton method and registerBeanDefinition method to configuration metadata, but this way is not common, Typically, XML is used

Three bean names

Each bean usually has one or more identities, using bean aliases, and so on, but containers must have a unique identity when managing beans. Usually based on XML configuration metadata, the id of the

element, and the name attribute usually represent the unique identity of a bean. The naming rules are that the first letter is lowercase and then the letters or numbers are humped like userDao, userService. Spring Container automatically generates a unique identity for the bean. However, if you want to refer to the bean by name, you must look up the provided name using the ref element or Service Locator style.

Four alias

Aliases are commonly used in large projects, such as when the main system and subsystem are integrated, a bean called fromName in the same container points to toName, as shown in the following example:

<alias name="fromName" alias="toName"/>
Copy the code

Instantiation of five beans

There are three methods of instantiating a bean. The first method is to use the bean element directly. After the Container is initialized, the bean instance is fetched directly. The second option is to use static factory-method. The third is factory-method (instance factory pattern). The second and third methods, which are less common, call fatory method to instantiate the bean during initialization. Fatory method calls createInstance in the factory.

The difference is whether inner classes are commonly used$dog`

5.1 Instantiate beans using constructors

The following example, which will be familiar to anyone who uses the Spring framework, is often used to instantiate beans in this way

	<bean id="iocService" class="com.youku1327.ioc.service.IocService"> <! -- additional collaborators and configurationfor this bean go here -->
    </bean>
Copy the code

5.2 Instantiate beans using the Static Factory Method

The static factory of the fatory method in this example is the static factory itself (you can also put other instances into the static factory), but its declaration method type must be static.

/**
 * @Author lsc
 * @Description <p> </p>
 * @Date 2019/10/31 13:55
 */
public class StaticBeanService {

    private static StaticBeanService staticBeanService = new StaticBeanService();

    // other properties

    private StaticBeanService() {}

    public static StaticBeanService createInstance() {
        // add your properties
        returnstaticBeanService; }}Copy the code

The configuration is as follows:

<? xml version="1.0" encoding="UTF-8"? > <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! -- static factory method services --> <bean id="staticBeanService" class="com.youku1327.bean.entity.StaticBeanService" factory-method="createInstance">

    </bean>
    
</beans>
Copy the code

5.3 Instantiating beans using the Instance Factory Method

The instance-factory mode is similar to the static factory mode, except that the method used to create the instance uses the static keyword. The Car of this example should actually be the interface, and then implement the interface through the concrete implementation class (such as Lamborghini Car, BMW Car, etc.), then use the corresponding createCarInstance method in Carfactory, and configure the corresponding bean in XML. A simple implementation example is as follows:

The entities are as follows:

/**
 * @Author lsc
 * @Description <p> </p>
 * @Date 2019/10/31 14:58
 */
public class Car {

    private String carName;
    private String color;

    public String getCarName() {
        return carName;
    }

    public void setCarName(String carName) {
        this.carName = carName;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) { this.color = color; }}Copy the code

Example chemical plant is as follows:

/** * @Author lsc * @Description <p> </p> * @Date 2019/10/31 15:00 */ public class CarFactory { private static Car car =  new Car(); privateCarFactory(){

    }

    public Car createCarInstance() {returncar; }}Copy the code

The configuration is as follows:

<? xml version="1.0" encoding="UTF-8"? > <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! -- the factory bean,which contains a method called createInstance() -->
    <bean id="carFactory" class="com.youku1327.bean.factory.CarFactory"> <! -- inject any dependencies required by this locator bean --> </bean> <! -- the bean to be created via the factory bean --> <bean id="car" factory-bean="carFactory" factory-method="createCarInstance"/>


</beans>
Copy the code

Vi Reference Documents

IOC Container

[This article github source at the end of the public account link].