Dry goods have quality, hydrology has feelings, wechat search [program control], pay attention to this interesting soul
preface
There’s too much in the Sprig family to write all at once. Let’s start with the basics and the most important: IOC and AOP.
An understanding of AOP and IOC is essential for both entry and interview purposes. During the interview, framework, such as Mybatis/Hibernate/struts 2 q is relatively less, and Spring is often asked.
IOC and DI overview
IoC(Inversion of Control) is to turn over the creation of class objects to Spring management.
DI (Dependency Injection) is implemented on the basis of DI.
We can simply say:
- IOC is like a factory in that we have the right to create class objects, so we don’t have to go to new objects every time.
- DI is a dependency on an interface implementation class injected by a third party to remove the dependency of the calling class on an interface implementation class.
We can now clearly see that the so-called IOC container is a big factory [third party container].
Not only do we write code to implement features, but we also have to “maintain” the code once we’ve implemented it. If our code is poorly written, it’s expensive to maintain.
Faced with repetitive/cumbersome non-business code:
- If there is a problem with the program, we need to look at the code and find the problem
- If we want to add a new function to the original function, we need to modify the original code, more trouble… (AOP solution)
We use the ORM framework, so we can be more”focus“To implement its own business, ORM framework”repeat“Code is shielded, the code is easier to maintain than JDBC.Spring IOC solves this problemObject management and object dependency issues. When we use Spring, we give our objects to”The IOC container“To manage.
IOC container: The IOC container is a “factory” that we hand over to manage objects, including object creation, dependencies between objects, etc. When we need objects, we pull them out of the factory.
Inversion of control: Objects that were originally new by ourselves are now handed over to the IOC container. Giving “control” of this object to another party. Inversion of control is more of a thought or design pattern in which “someone else” takes care of things that are in your control.
Dependency injection: Refers to an implementation of the idea of “inversion of control” in which objects do not need to create or manage their dependencies themselves; dependencies are “automatically injected” into objects that need them.
I am not very sensitive to these nouns, I think there is no need to tangle with the concept of many nouns, just understand their internal meaning.
The IOC use
The first thing we need to learn when using Spring IOC is how to hand objects over to the IOC container to manage them and create them by the container.
Spring provides four approaches:
- annotations
- XML
- JavaConfig
- Groovy DSL-based configuration
In summary: we assemble beans with XML configuration + annotations, and get more objects, most of which are annotations.
When we put objects into the IOC container, we need to tell Spring about their dependencies so that it can help us resolve them.
“Relationships between objects“Don’t think too complicated, in the daily development of A object in fact, A lot of times there are properties of B object.Generally we passConstructor or property (setting method)To inject object dependencies
For example, in daily development, we often use the @Component annotation identifier to put objects in the “IOC container” and the @AutoWired annotation to inject objects
The following diagram nicely summarizes the various ways in which beans are defined and injected.
The IOC container assembles beans
Having said that, the IOC container is really a big factory that manages all of our objects and dependencies.
Dependency injection can be done in three ways:
- Property injection –> via setter() method injection
- Annotation injection –> @autowired and all that
- Constructor injection
The situations I encounter are generally annotation injection and attribute injection, and generally annotation injection is a little bit more
There are five scopes for beans:
- Singleton Singleton
- Many cases of the prototype
- Bean scopes that are relevant to the Web application environment
- reqeust
- session
For Bean scoping on the Web, we need toManually configuring the agentThe ~
Understand the Bean
Lazy-init in Spring defaults to false, meaning non-lazy loading. Once the container is initialized, all Bean objects are loaded for initialization, which is usually a good thing because errors in configuration (object creation failures, dependency issues, etc.) immediately occur.
When to create the Bean object:
- Under non-lazy loading, all Bean objects are created when the container is initialized;
- Under lazy loading, the object is created when getBean is called to get the object instance.
- Under lazy loading, it is created as A dependency of other objects (A depends on B, and B object is created when A object is acquired);
We mentioned above that non-lazy loading has the advantage of checking for errors, but it also has its drawbacks
- It takes up a lot of memory because you create a lot of objects that you don’t need at the moment.
- The slow startup time of the service is also due to the creation of many objects;
A quick look at the Bean instantiation process:
Bean life cycle:
- Creating a bean object
- Set bean properties and initialize
- Using bean
- Execute destruction method
omg
The more you know, the more you don’t know.
Advice: Read more books, watch more videos, eat less snacks, and code more
It’s important not to be”noun“To frighten, too many nouns too many, if not heard of the noun may think very severe, in fact, such as real contact down, after learning, found that it is just so.
If you feel good about it, you can follow my wechat public account [Program control]. Brother Shih has always insisted on sharing technical articles with everyone, so that everyone can be quietly top of the list and then surprise everyone