1,Spring
1.1 INTRODUCTION
Spring origin and interface21
Spring is an open source framework, which is developed by [Rod Johnson]( https://baike.baidu.com/item/Rod Johnson). It is created to solve the complexity of enterprise application development. Spring uses basic JavaBean s to do things that previously could only be done by EJB s. However, the use of spring is not limited to server-side development. From the perspective of simplicity, testability and loose coupling, any Java application can benefit from spring.
Spring is a lightweight inversion of control (IoC) and aspect oriented (AOP) container framework.
spring's philosophy: making existing technologies easier to use is a hodgepodge in itself
Official website: https://spring.io/projects/spring-framework
Source address: https://repo.spring.io/ui/native/release/org/springframework/spring
Github address: https://github.com/spring-projects/spring-boot
maven core dependency
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.14</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.14</version> </dependency>
1.2 advantages of spring
- Open source free framework (container)
- Lightweight (very small dependent packages) and non intrusive (little impact on previous frameworks)
- Inversion of control (IOC), aspect oriented (AOP)
- Support transaction processing and framework integration
Spring is a lightweight framework for inversion of control (IOC) and aspect oriented programming (AOP)
1.3 composition
1.4, expansion
Modern java development -- development based on spring
- SpringBoot
- A rapid development of scaffolding
- Based on SpringBoot, you can quickly develop a single microservice
- Contract greater than configuration
- SpringCloud
- Spring cloud is developed based on SpringBoot
Most companies use springboot for rapid development, and larger companies use SpringCloud for cluster development. The premise of learning springboot is to learn Spring and spingMvc!
Disadvantages: after too long development, it violates the original intention (making development easier), there are too many mixed things, and the configuration is very cumbersome (known as "configuration hell").
2. IOC theoretical derivation
The original code programming process: daoImpl → dao → serviceImpl → service
Disadvantages: each modification will involve the reference modification of the underlying layer. If the amount of code modification is large (expensive), the gain is not worth the loss!
public class UserServiceImpl implements UserService { // private UserDao userDao = new UserDaoImpl(); // private UserDao userDao = new UserDaoPgImpl(); private UserDao userDao; // Dynamic injection using set method @Override public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void getUser() { userDao.getUser(); } }
- Before, the program was actively creating objects! Control is in the hands of programmers
- After using set injection, the program is no longer active, but becomes a passive receiving object
This idea essentially solves the problem: programmers no longer need to manage the creation of objects, the coupling of the system is greatly reduced, and they can focus more on the implementation of business! (prototype of IOC)
Inversion of control (IoC) is a design idea. DI (dependency injection) is a method to realize IoC. Some people think that DI is just another way of saying IoC. In programs without IoC, we use object-oriented programming. The creation of objects and the dependencies between objects are completely hard coded. In programs, the creation of objects is controlled by the program itself. After the control is reversed, the creation of objects is transferred to a third party. Personally, I think the so-called control reversal is the reversal of the way to obtain dependent objects.
3,HelloSpring
Use beans XML injection entity class
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- use spring create object Type variable name = new type(); Hello hello = new Hello(); id = Object name class = new Object of (essence and import (consistent) properties Set values for properties --> <bean id="hello" class="com.example.springboothelloworld.bean.pojo.Hello"> <property name="str" value="wanyu"></property> </bean> </beans>
Most basic container acquisition
public class XmlIocTest { public static void main(String [] args) { // Gets the context object for spring ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); // Our objects are now managed in Spring and can be taken out directly when using Hello hello = (Hello) context.getBean("hello"); System.out.println(hello); } }
Of course, there are other injection methods, such as annotation injection and other file injection.
IOC can be summarized as follows: spring creates, manages and assembles objects!
Assemble directly using spring 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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userDaoPg" class="com.example.springboothelloworld.web.dao.impl.UserDaoPgImpl"/> <bean id="userDaoMysql" class="com.example.springboothelloworld.web.dao.impl.UserDaoMysqlImpl"/> <bean id="userDao" class="com.example.springboothelloworld.web.dao.impl.UserDaoImpl"/> <bean id="userService" class="com.example.springboothelloworld.web.service.impl.UserServiceImpl"> <!-- ref:introduce spring Objects already created in --> <property name="userDao" ref="userDaoPg"/> </bean> </beans>
public class IocTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-properties/user.xml"); UserService userService = (UserService) context.getBean("userService"); userService.getUser(); } }
Reference link
1,Baidu Encyclopedia spring framework
3,Spring source download address