Introduction: Java background development in Spring is an unavoidable obstacle, this article only represents my summary and understanding, if there are mistakes, welcome to correct…

First, spring container understanding

The Spring container can be understood as a place to produce objects, where the container does more than just create them for us; it takes care of their entire life cycle — creation, assembly, and destruction. Control of object creation and management is handed over to the Spring container, so this is a reversal of control called the IOC container, and the IOC container is not just Spring’s, but many frameworks have this technology.

The relationship between BeanFactory and ApplicationContext

Spring’s two core interfaces are BeanFactory and ApplicationContext

ApplicationContext is a subinterface of BeanFactory. The main method is getBean(String beanName).

2) the BeanFactory, literal translation Bean plant (com) springframework. Beans. Factory. The BeanFactory), we generally call the BeanFactory IoC container, and called the ApplicationContext application context.

At the heart of Spring is a container, and containers are not unique in that they fall roughly into two types (BeanFactory and ApplicationContext) : the less commonly used BeanFactory, which is the simplest container that only provides basic DI functionality; One is the ApplicationContext derived from the BeanFactory. It can provide more enterprise-level services, such as parsing configuration text information, which is the most common application scenario of the ApplicationContext instance object.

Summary: The BeanFactory is the heart of Sping, so the ApplicationContext is the complete body.

In addition to providing all the functionality supported by the BeanFactory, ApplicationContext has the following additional functionality:

1. All singletons are initialized by default or can be uninitialized by configuration. Inherit MessageSource, thus supporting internationalization. Resource access, such as access to urls and files. 4. Event mechanism. 5. Load multiple configuration files simultaneously. 6. Start and create the Spring container declaratively.Copy the code

Note: Since the ApplicationContext pre-initializes all Singleton beans, there is a significant overhead during system creation, but once the ApplicationContext is initialized, The program will perform better later when it gets the Singleton Bean instance. You can also set the lazy-init property to true for the bean, meaning that the Spring container will not pre-initialize the bean.

Three ways Spring assembs beans

Note: Assembly and injection are different; the assembly here is to put beans into an IOC container, that is, to scan various beans.

1. Display the configuration in XMl and load the configuration into the IOC container through an XMl file

2. Display the Configuration in Java and load it into the IOC container via Java annotations @Configuration and @Bean

Implicit bean discovery and auto-assembly, @Configuration, @Component, @ComponentScan (Spring automatically discovers beans created in application context).

Four, Spring three injection methods

Spring implements IOC (Inversion of Control) through DI (dependency injection). There are three main injection methods commonly used: constructor injection, setter injection, and annotation injection.

Constructor injection: Configure the parameters to be injected in the Spring XML configuration file.


2. Setter injection


Annotation-based injection: use @AutoWired

5. Common spring annotations

Component: Standard a plain Spring Bean class. @Repository: Annotate a DAO component class. Service: Annotates a business logic component class. Controller: Annotates a Controller component class.Copy the code

See blog: Understand the Spring container, BeanFactory, and ApplicationContext


Conclusion: Ever is to learn to read people’s blog technology, some of them have essence blog also have to play around the CV of solution, so the decided to study the knowledge sharing for everyone, mainly want to go to the sea after less detours, more positive energy blog, if there are any errors, welcome to point out mistakes, only hope that we can learn knowledge in my blog, Solve the problem, then it is enough. Thank you! (Please note the original source for reprint)