preface
Spring everyone must be familiar with, involving Java development, there are many packages for us to choose, such as: SSM (Spring + SpringMVC + Mybatis), SSH(Struts2 + Spring + Hibernate), SpringBoot, SpringCloud distributed solution, The older Servlet, JSP.
This article is mainly about 3 minutes to clean up Spring and see what is in Spring
The emergence of the Spring
The emergence of Spring has replaced other, more heavyweight, enterprise-level Java technologies and simplified Java development.
Spring is a non-invasive framework
An example of an intrusive framework is the Struts2 framework, which changes the structure of Java itself by implementing specific interfaces and inheriting specific classes during development.
Compared to Struts2, Spring is non-invasive in that you can enhance Javabeans by making changes to the class structure, and Hibernate is also non-invasive.
Spring provides loose coupling to the interface through dependency injection (DI)
If you are teaching at school or have your own online class, you will find that the basic pattern of development is to inject the Service layer through @AutoWired, and then inject the Dao layer into the Service layer for DB operation. But have you thought about how we would do that without the @Autowired annotation?
For example, if I want to call the Dao layer from the Service layer, I need to new the Dao layer object from the Service layer. In this way, the Service and Dao become tightly coupled, and the dependency injection provided by Spring, Inversion of control (IOC) solves these problems perfectly.
Spring’s AOP is faceted programming
Spring can add permissions to methods via dynamic proxy + annotations, which can be interpreted as executing code before method execution, executing code after method execution, executing code when method exceptions occur, and so on.
With AOP we can reduce a lot of redundant code writing, but also can easily complete a lot of seemingly complex operations.
Common concepts literacy
The IOC and DI
IOC – Inversion of Control
From the name, we can simply ask a few questions, who controls whom? What do you mean reversed? What reversed?
-
Who controls whom?
Through the above example, we can find that in traditional new objects, it is the program to create dependent objects, while IOC is to entrust the process of creating objects to the external container. Then whoever controls the object has the answer, that is, IOC controls the creation and management rights of beans. Control the life cycle of beans
-
What do you mean reversed? What reversed?
The so-called reverse then must be changed the original positive thinking, what is positive thinking? Without the Spring framework, it is up to us to have active control in the object to get the dependent object; In Spring, instead of creating and injecting objects, the container helps us find and inject objects. Objects passively accept dependencies, so we can deduce that the reverse is dependent on the acquired objects
DI – Dependency Injection (DI)
Again from the name point of view, we ask a couple of questions, who depends on whom? Why do we have dependencies? Who injected whom? What does it inject?
-
Who depends on whom?
From the IOC explanation above, we can see that applications depend on IOC
-
Why do we have dependencies?
The application needs the IOC container to provide the external resources that the objects need
-
Who injected whom?
The IOC container injects objects that an application depends on
-
What does it inject?
Inject the external resources needed for this object
Relationship between IOC and DI
In fact, this is often asked in interviews. IOC and DI are different descriptions of the same concept. Since the concept of inversion of control was vague at the beginning (it may only be understood as the level of container control objects), It’s hard to think of anyone who maintains object relationships), so Martin Fowler, the 2004 guru, gave it a new name: “dependency injection.” In contrast to IOC, dependency injection explicitly describes how “the injected object depends on the IOC container to configure dependencies.”
Why adopt the IOC idea?
The goal is decoupling, and it turns dependencies between objects into configuration files to be managed by the Spring IOC Container.
In order to achieve business logic, they are like cogs. They have to establish a highly coupled relationship in the code. If one gear fails, it will have an impact on the others.
After being managed by IOC container, the original A, B, C and D are related to each other. Now each object is only associated with A third party container, without any other connection and coupling relationship, which is called decoupling.
Some excellent blog posts on IOC and DI
There are many articles about IOC and DI on the web, including analysis of some ideas and benefits associated with other features… Since this article is only for the basic literacy of the following Spring chapter, there is not much space to expend some knowledge. Here are some excellent blog posts for those who are interested:
-
What are the benefits of Spring IOC? – zhihu
www.zhihu.com/question/23…
-
Spring dependency Injection — Xin Liu, Fangfen Code
Mp.weixin.qq.com/s?__biz=MzA…
Spring modules
- Spring Core: Basic, it can be said that all of Spring’s other functions depend on this library. It mainly provides IoC dependency injection function.
- Spring Aspects: This module provides support for integration with AspectJ.
- Spring AOP: Provides a section-oriented programming implementation.
- Spring JDBC: Java database connection.
- Spring JMS: Java messaging service.
- Spring ORM: Used to support ORM tools such as Hibernate.
- Spring Web: Provides support for creating Web applications.
- Spring Test: Provides support for JUnit and TestNG tests.
Design patterns that Spring applies to
- Factory mode: Spring uses factory mode to pass
BeanFactory
,ApplicationContext
Creating a bean object - Proxy pattern: Implementation of Spring AOP functionality
- Singleton pattern: Spring’s beans are singleton
- Template method pattern: in Spring
jdbcTemplate
,hibernateTemplate
Classes that operate on databases that end in Template use the Template pattern - Observer Pattern: The Spring event-driven model is the Observer pattern
- Adapter pattern: The adapter pattern is used in Spring AOP enhancements or Advice, and adapter pattern adaptation is used in Spring MVC
Controller
- .
How does Spring relate to Spring MVC?
So the first thing I want to do here is introduce the concept of a parent-child container
The whole core idea of the Spring framework is that containers are used to manage the life cycle of beans. A project may contain many containers, and they may have a hierarchical relationship. Spring and Spring MVC are two containers, and are typically parent-child containers.
Spring is the parent, SpringMVC is the child, and beans registered in the Spring parent are visible to the SpringMVC child, not vice versa
What is SpringBoot?
If you’ve ever used the SSM suite, you know that the configuration is pretty cumbersome, and I can probably create other projects with a lot of the same configuration, so you can skip this part.
Based on Spring 4.0 design, SpringBoot not only inherits the original excellent features of the Spring framework, but also simplifies configuration, making it easier to build and develop. Spring has the following features:
- You can create a standalone Spring application with Tomcat embedded in it that can be published directly as a JAR package using Maven plug-ins
- Provides an auto-configured ‘Starter’ project object model (POMS) to simplify Maven configuration
- Automatically configure the Spring container as much as possible to avoid tedious configuration
- Provides prepared features such as metrics, health checks, and externalized configurations
- No code generation, no XML configuration required
At the end
Because IDEA is now very convenient, basically a Spring project is completed in a few clicks, so I will not give the specific configuration to explain the process from the beginning of the project, recommend interested partners, or to configure Spring by yourself, there are many things that can not be understood by one-click deployment.
Loger will enter the Spring chapter starting today. The Spring chapter will cover a lot of things, such as: The bean lifecycle, the concept of a parent-child container, is cyclic dependent on……