Spring framework
1. Introduction
1.1 what is spring
-
An open source framework is the Java EE open source framework
-
Spring is a lightweight open source framework for layered Java SE/EE application full stack, with IoC (Inverse Of Control) and AOP (Aspect Oriented Programming) as the kernel.
It provides many enterprise application technologies such as presentation layer SpringMVC, persistence layer spring JDBC template and business layer transaction management. It can also integrate many famous third-party frameworks and class libraries in the world, and gradually become the most used open source framework for Java EE enterprise applications. -
Control flipping and dependency injection
-
Advantages of framework
-
-
-
Architecture
-
2. Supplement: Java's three-tier structure
-
Business layer (logic layer, service layer)
- Adopt transaction script mode. Encapsulate all operations in a business into one method, and ensure all database update operations in the method, that is, ensure success or failure at the same time. Avoid data confusion caused by partial success and partial failure.
-
Presentation layer (JSP)
- Adopt MVC mode
- M is called model, that is, entity class. It is used for data encapsulation and data transmission.
- V is the view, that is, the GUI component, which is used for the display of data (now usually HTML+CSS).
- C is control, that is, event, which is used for process control.
-
Persistence layer (DA0)
- DAO mode is adopted to establish entity class and database table mapping (ORM mapping). That is, which class corresponds to which table and which attribute corresponds to which column. The purpose of persistence layer is to complete the conversion of object data and relational data
-
Illustration
-
SSM framework
- Business layer - Spring
- Presentation layer - spring MVC
- Persistent layer - MyBatis
3. Supplement: high cohesion and low coupling
-
reference: https://blog.csdn.net/u012862227/article/details/47296899
-
reference: https://zhuanlan.zhihu.com/p/265143336
-
What is high cohesion and low coupling
- The closeness of the relationship between each module. The closer the relationship between modules, the higher the coupling and the worse the independence of the module! On the contrary, the same is true;
- The degree of closeness between the elements in a module. The higher the degree of connection between the elements (statements, program segments), the higher the cohesion, that is, 'high cohesion'
-
for example
-
For example, 20 methods in a project call well, but one of them needs to be modified, and the other 19 need to be modified. This is high coupling! Poor independence!
The current software structure design will require "high cohesion and low coupling" to ensure the high quality of software! mark!
-
4. What is CRUD supplement
-
CRUD refers to the acronyms of create, retrieve, update, and delete words in calculation processing. It is mainly used to describe the basic operation functions of DataBase or persistence layer in software system.
-
CRUD is about adding, modifying and deleting
- C: Create adds the corresponding CREATE TBL; ADD TBL IN (...) VALUES (...)
- R: Retrieve query SELECT * from TBL
- U: Update modify update TBL.. set
- D: DELETE FROM TBL WHERE
5. Build Spring project
5.1 rapid construction (based on Maven)
-
Create project
Modify Maven's specified path;
If there is no resource file, add it manually;
5.2 configuring package coordinates in pom.xml file
-
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.9</version> </dependency>
5.3 creating interfaces and interface implementation classes
-
//Interface package com.one.dao; public interface UserDao { public void save(); } //Implementation class package com.one.dao.impl; import com.one.dao.UserDao; public class UserDaoImpl implements UserDao { @Override public void save() { System.out.println("save() runing....."); } }
5.4 creating and writing configuration files
-
Create a spring config type configuration file in the resource folder; The name is generally called applicationContext.xml;
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="UserDao" class="com.one.dao.impl.UserDaoImpl"></bean> Only this line is not automatically generated, but written by yourself; Later, it will be written with the increase of functions </beans>
5.5 preparation of startup documents
-
Create a class containing the main method in the startup class;
-
package com.one.demo; import com.one.dao.UserDao; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserDaoDemo { public static void main(String[] args) { ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml"); //Object userDao = app.getBean("UserDao");// Polymorphic writing requires strong type conversion UserDao userDao =(UserDao) app.getBean("UserDao");//Polymorphic writing requires strong type conversion userDao.save(); } }
Successful implementation;
4. Detailed description of configuration file
4.1Bean label basic configuration
-
<bean id="UserDao" class="com.one.dao.impl.UserDaoImpl"></bean>
-
Write test code
package com.one.test; import com.one.dao.UserDao; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { @Test public void test1(){ ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml"); //Object userDao = app.getBean("UserDao");// Polymorphic writing requires strong type conversion UserDao userDao =(UserDao) app.getBean("UserDao");//Polymorphic writing requires strong type conversion UserDao userDao1 =(UserDao) app.getBean("UserDao");//Polymorphic writing requires strong type conversion userDao.save(); System.out.println(userDao1); System.out.println(userDao); } }
-
The configuration object is created by Spring.
-
By default, it calls the parameterless constructor in the class. If there is no parameterless constructor, it cannot be created successfully.
- id: the unique identifier of the Bean instance in the Spring container
- class: the fully qualified name of the Bean
4.2 Bean tag range configuration
-
-
When the value of scope is singleton
- Number of instantiations of Bean: 1
Bean instantiation timing: instantiate the configured bean instance when the Spring core file is loaded
Bean lifecycle:
object creation: when the application loads and creates a container, the object is created
object running: as long as the container is, the object is always alive
object destruction: when the application unloads and destroys the container, the object is destroyed
- Number of instantiations of Bean: 1
-
<bean id="UserDao" class="com.one.dao.impl.UserDaoImpl" scope="singleton"></bean>
-
There is only one instance of an object with the same address value;
-
When the value of scope is prototype
- Number of Bean instantiations: multiple
- Instantiation timing of Bean: instantiate Bean when calling getBean() method
object creation: when using an object, create a new object instance
object operation: as long as the object is in use, it will always be alive
object destruction: when the object is not used for a long time, it is recycled by the Java garbage collector
-
<bean id="UserDao" class="com.one.dao.impl.UserDaoImpl" scope="prototype"></bean>
-
4.3 Bean lifecycle configuration
-
Init method: Specifies the name of the initialization method in the class
-
Destroy method: Specifies the name of the destroy method in the class
-
<bean id="UserDao" class="com.one.dao.impl.UserDaoImpl" scope="prototype"></bean>
-
package com.one.test; import com.one.dao.UserDao; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { @Test public void test1(){ ClassPathXmlApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml"); //Object userDao = app.getBean("UserDao");// Polymorphic writing requires strong type conversion UserDao userDao =(UserDao) app.getBean("UserDao");//Polymorphic writing requires strong type conversion UserDao userDao1 =(UserDao) app.getBean("UserDao");//Polymorphic writing requires strong type conversion userDao.save(); System.out.println(userDao1); System.out.println(userDao); app.close(); } }
4.4 three methods of bean instantiation
- Parameterless construction method instantiation
- Factory static method instantiation
- Factory instance method instantiation
1) Instantiate using the parameterless construction method
It will create class objects according to the default parameterless constructor. If there is no default parameterless constructor in the bean, the creation will fail
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/>
Supplement: factory mode
-
Reference documents: https://www.cnblogs.com/JuanF/p/9275833.html
-
Design principles
- Opening and closing principle: open to extension and close to modification
- Richter substitution principle: only when the derived class can replace the base class and the function of the software unit is not affected, can the base class be truly overridden. Derived classes can also add new behaviors to the base class
- Dependency Inversion Principle: the basis of the opening and closing principle. For interface programming, it depends on abstraction rather than concrete
- Interface isolation principle: use multiple isolated interfaces to reduce coupling
- Dimitri law principle: the principle of least knowledge. An entity should interact with other entities as little as possible to make the system functional modules relatively independent
- Composition Reuse Principle: try to use composition / aggregation instead of inheritance. Inheritance actually breaks the encapsulation of classes, and the methods of superclasses may be modified by subclasses.
-
Factory mode
-
In the above mode, all mice are produced in the same MouseFactory factory, and there is a unified static create method. When using a factory, you do not need to instantiate the factory. You only need to call the static method to get the corresponding product.
However, if the user needs to add a new type of product, such as an ASUS mouse, the factory needs to change the create function to produce the product, which is contrary to the opening and closing principle of the design principle.
-
-
In this mode, products of different brands are produced by different factories. There is a unified factory interface, and all factories producing the product should implement the interface.
Which product to produce is no longer determined by parameters, but by the factory when creating the factory. For example, HP's factory will only produce HP's mouse, while Dell's factory will only produce Dell's mouse.
-
See the reference documents for more details;