Spring 6.X IoC container

1. Introduction to Spring IoC container and beans

The following mainly introduces the Spring framework's implementation of the Inversion of Control (IoC) principle.

  • The first thing to note is: IoC is also called dependency injection, which is a process.

  • Secondly, the definition of dependencies: Objects define their dependencies (that is, other objects they use) only through constructor parameters, parameters of factory methods, or properties set on the object instance after the object instance is constructed or returned from the factory method.

  • Dependency injection: The container injects the dependencies of the first two steps when creating the bean. This process is essentially the inverse of the bean itself (hence the name "inversion of control"), controlling the instantiation or location of its dependencies by using direct construction of the class or a mechanism such as the service locator pattern.

  • Involved packages: org.springframework.beansSpring org.springframework.contextFramework is the basis of the IoC container. The BeanFactory interface provides an advanced configuration mechanism that can manage any type of object. ApplicationContext is a sub-interface of BeanFactory. It provides easier integration with Spring's AOP functionality, message resource handling (for internationalization), activity publishing, application layer specific contexts, such as: WebApplicationContext context for Web applications.

    In short, BeanFactory provides the configuration framework and basic functionality, and ApplicationContext adds more enterprise-specific functionality.

  • Bean: BeanFactory In Spring, the objects that form the backbone of the application and are managed by the Spring IoC container are called beans. A bean is an object instantiated, assembled and managed by the Spring IoC container. If not managed by IoC, a bean is just one of many objects in the application. Beans and the dependencies between them are reflected in the configuration metadata used by the container.

1.1. Container overview


  • The main interface of the Spring IoC container is ApplicationContext interface org.springframework.context.ApplicationContext, which is responsible for instantiating, configuring and assembling beans. Containers get instructions on which objects to instantiate, configure, and assemble by reading configuration metadata. Configuration metadata is expressed in XML, Java annotations, or Java code. It enables an application's objects and the rich interdependencies between those objects to

  • ApplicationContextSpring
    ApplicationContextSpring provides multiple implementations of the ApplicationContext interface. In standalone applications, it is common to create instances of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext. Although XML is the traditional format for defining configuration metadata, support for these additional metadata formats can be enabled declaratively by providing a small amount of XML configuration to instruct the container to use Java annotations or code as the metadata format.

  • Instantiation
    In most application scenarios, no explicit user code is required to instantiate one or more Spring IoC container instances.

  • Working View
    Application classes are combined with configuration metadata so that after the ApplicationContext is created and initialized, you have a fully configured and executable system or application.

Insert image description here

1.3. Use

ApplicationContext is an interface to a high-level factory that maintains a registry of different beans and their dependencies. An instance of a bean can be retrieved using the method T getBean(String name, Class T requiredType).

// 创建并配置bean
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

//检索配置的实例
PetStoreService service = context.getBean("petStore", PetStoreService.class);

//使用配置的实例
List<String> userList = service.getUsernameList();

Guess you like

Origin blog.csdn.net/qq_35241329/article/details/132272571