A global view of core interfaces and classes

The key problem is solved: shifting the relationship between objects to configuration management

  • Dependency Injection — Dependencies are managed in Spring’s loC container

  • You can manage objects and perform additional operations by wrapping them in beans

Beans with BeanDefinition

Bean is a first-class citizen of Spring:

  • Beans are essentially Java objects whose life cycle is managed by the container
  • There is no need to add any additional restrictions on the original Java class in order to create beans
  • The control over Java objects is reflected in configuration

BeanDefinition is the definition of a Bean

Depending on the configuration, generate a BeanDefinition that describes the Bean, with the following common properties:

The JDK uses java.lang.class to describe this object, and Spring uses BeanDefinition to describe beans

  • Scope scope (@Scope)
  • Lazy loading lazy-init (@Lazy) : Determines whether the Bean instance is lazily loaded
  • The preferred primary (@Primary) : Beans set to true will be the preferred implementation class
    • In the case of multiple implementation beans for an interface, the interface that uses this annotation will be implemented first
  • The factory – bean and factory – method (@Configurationand@Bean)

A demonstration of the no-argument constructor to create a bean: Create an Entity package in the project and create a User class

package com.wjw.entity;

public class User {}Copy the code

Defining the corresponding bean in the XML at this point can be done using a no-argument constructor

<bean id="user1" class="com.wjw.entity.User" scope="singleton" lazy-init="true" primary="true"/>
Copy the code

Create a bean with a static factory demo: Create another static factory class

package com.wjw.entity.factory;

import com.wjw.entity.User;

public class StaticFactory {

   public static User getUser(a){
      return newUser(); }}Copy the code

Defining the corresponding beans in the XML at this point can be done using the static factory method

<! --classThe value of is not writeUserObject full path, but write static factory full path --> <! --factory-methodWrite the method to call --> <bean id="user2" class="com.wjw.entity.factory.StaticFactory" factory-method="getUser" scope="singleton"/>
Copy the code

Example of creating a bean using an example factory:

package com.wjw.entity.factory;

import com.wjw.entity.User;

public class UserFactory {

   public User getUser(a){
      return newUser(); }}Copy the code

Because methods are not static, they cannot be called directly, but instead create a factory object and call it through the object

<! You need to create a factoryBean object and call it from the factoryBean object --> <bean id="userFactory" class="com.wjw.entity.factory.UserFactory"/>
<bean id="user3" factory-bean="userFactory" factory-method="getUser" scope="singleton" />
Copy the code

Testing:

package com.wjw;

import com.wjw.controller.WelcomeController;
import com.wjw.entity.User;
import com.wjw.service.WelcomeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.FileSystemXmlApplicationContext;

@Configuration
@ComponentScan("com.wjw")
public class Entrance {

   public static void main(String[] args) {
      System.out.println("Hello World!");
      String xmlPath = "F: \ \ Java \ \ spring - framework - 5.2.0. RELEASE \ \ springdemo \ \ SRC \ \ the main \ \ resources \ \ spring \ \ spring - config. XML";
      ApplicationContext applicationContext = new FileSystemXmlApplicationContext(xmlPath);
      WelcomeService welcomeService = (WelcomeService) applicationContext.getBean("welcomeService");
      welcomeService.sayHello("The Powerful Spring Framework");

      // Get the object created by the no-argument constructor:
      User user1a = (User) applicationContext.getBean("user1");
      User user1b = (User) applicationContext.getBean("user1");
      // Get the static factory-created object:
      User user2a = (User) applicationContext.getBean("user2");
      User user2c = (User) applicationContext.getBean("user2");
      // Get the object created by the instance factory:
      User user3a = (User) applicationContext.getBean("user3");
      User user3b = (User) applicationContext.getBean("user3");


      System.out.println("Object created by the no-argument constructor :" + user1a);
      System.out.println("Object created by the no-argument constructor :" + user1b);
      System.out.println("Static factory created objects:" + user2a);
      System.out.println("Static factory created objects:" + user2c);
      System.out.println("Object created by instance Factory:" + user3a);
      System.out.println("Object created by instance Factory:"+ user3b); }}Copy the code

The main thing container initialization does (main context)

BeanDefinition source

Inheriting two interfaces. Spring is full of interfaces, and inheriting an interface means having a function.

  • AttributeAccessor defines the most basic way to modify or retrieve any object metadata, and is used to retrieve BeanDefinition attributes and manipulate them.

  • The BeanMetadataElement basically defines a getSource method that returns a configurable source object, and is used here primarily to return the BeanDefinition object itself.

  • AttributeAccessorSupport is an implementation class for AttributeAccessor.

  • AbstractBeanDefinition is a base class for the BeanDefinition implementation class, which defines constructors for initializing generic properties, getters and setters, and generic methods for operations. Spring implements some special purpose BeanDefinitions based on the AbstractBeanDefinition abstract class.

  • RootBeanDefinition can stand alone as a BeanDefinition or as a parent of other BeanDefinitions, but not as another subclass. (Usually used to receive information at runtime when multiple BeanDefinitions are combined. You can receive multiple BeanDefinitions that have an inheritance relationship, taking over their combined properties other than the parent property.) Normally, bean tags in configuration files would be resolved to RootBeanDefinition, but spring2.5 uses GenericBeanDefinition instead, but because of the previous root, So when you merge beanDefinitions, you still receive them with rootBeanDefinitions.

Ps: Inheritance in Spring is determined by setting the parent property, not the extends property

  • ChildBeanDefinitionIt’s been replaced.
  • GenericBeanDefinition(Bean file configuration attribute definition class) in addition to the BeanDefinition attribute has the parent attribute, convenient program to set the parent BeanDefinition, will not report an exception, is a better scheme.