Introduction to the
1. Spring is an open source and lightweight application development framework, which aims to simplify enterprise application development and reduce the development difficulty of developers; (Simplify development: 2. The IoC and AOP applications provided by Spring can reduce the coupling degree of components to a minimum (decoupling). Facilitate system maintenance and upgrade in the future; 3. Spring provides a holistic solution for the system, allowing developers to leverage its own capabilities and integrate applications with third-party frameworks and technologies. (such as Spring with SpringMVC, Spring with MyBatis, Spring with Struts2, Spring with Hibernate, Spring with Quartz[timed task processing])
Why use Spring?
Support for AOP programming 3). Support for declarative transactions 4). Convenient integration of various excellent frameworks 5). Reduce the difficulty of using Java EE API, such as JDBC, JavaMail, remote call and so on to provide a simple package
Spring architecture diagram
The module specification
- BeanFactory Spring uses it internally to create a factory for beans
- ApplicationContext External application calls, which also become the context of the Spring container
- User = new User(); User = new User(); Create object User User = context.getBean(User); Container creation object
- Loosely coupled DI Dependency Injection implements direct Dependency on objects
- AOP faceted programming complements Java object-oriented programming
Spring IOC Inversion of control
What is inversion of control
Inversion of Control refers to the transfer of object creation, object storage (MAP), and object management (dependency lookup, dependency injection) to the Spring container
coupling
Coupling refers to a tight relationship between layers in software development. This relationship can cause changes or substitutions in one layer to affect other layers, which we call layer to layer coupling.
The test code
New) Hello Hello = (Hello) ac.getBean(" Hello "); //1. //2. Call hello.sayhi ();Copy the code
The IOC summary
This is the IOC of the Spring framework — inversion of control. Before we create our own new object, for example:
User u = new User();
Copy the code
Now, it is created by an initialized XML configuration file, that is, by the Spring container.
Hello hello = (Hello) ac.getBean("hello");
Copy the code
Singletons and multiinstances of Bean objects
A singleton and multi-case overview of Bean objects
- Singleton: single instance, default. Objects identified by this scope are globally unique. When a bean definition is set to scope to the Singleton scope, the Spring IOC container creates only a unique instance of that bean definition. That is, only one object of the current class will be created in the entire Spring IOC container. The singleton instance is stored in the Singleton cache, and all subsequent requests and references to the bean return a cached, unique instance of the object. Singleton is responsible for creating, initializing, and destroying objects.
- Prototype: multi-instance. A new object is created each time the scoped object is fetched. When setting the scope of a bean definition to the Singleton scope, the Spring IOC container generates a new bean instance (the equivalent of new) every time the current bean is fetched. Prototype is responsible for object creation and initialization, not destruction.
Why single or multi-instance?
The reason for using a single instance is that without thread safety issues, there is no need to create an object for each request, which wastes BOTH CPU and memory. The reason for using multiple cases is to prevent concurrency problems; That is, one request changes the state of the object (for example, a mutable member variable), and the object then processes another request, and the previous request changes the state of the object causing the object to incorrectly process the other request. There is only one criterion for singletons and multiinstances: when an object has a state that can change (more precisely, in practice that state can change), use multiinstances, singletons otherwise;
To configure whether a Bean instance is singleton or multi-instance in Spring, do the following: singleton:
<bean id="hello" scope="singleton" class="com.tedu.spring.Hello"></bean>
Copy the code
Many cases:
<bean id="hello" scope="prototype" class="com.tedu.spring.Hello"></bean>
Copy the code
Spring DI dependency injection
Two injection modes are introduced
DI(Dependecy Injection) Dependency Injection. Dependency injection and dependencies between components are determined by the container at application runtime, that is, the container dynamically injects the target object instances of a dependency into the associated components of the application system.
In simple terms, dependency injection is about assigning values to objects at the same time or after they are created.
Spring assigns values to properties in two ways
(1).set injection (2). Constructor injection (spring mostly uses this method)
The last
Thank you for reading here, after reading what do not understand, you can ask me in the comments section, if you think the article is helpful to you, remember to give me a thumbs up, every day we will share Java related technical articles or industry information, welcome to pay attention to and forward the article!