Spring Tour, Spring
Recently, I have been stuck in a misunderstanding. I am always looking for some online videos such as SSM fast-forward, and then blindly follow the 'copy' code. After coding with the video and implementing some functions, I feel that I have a good understanding of some frameworks such as Spring. (In fact, I only know it, but I don't know why .) After a while, we could not use Spring in our work. When we needed it one day, we suddenly found that we could not even build a basic Spring Project Environment manually... Suddenly comprehend ~~ The code followed by the video is indeed others @_@. Therefore, I bought the <Spring Action> book and started to systematically understand Spring (@_@). After reading the first chapter, I summarized several knowledge points:
Spring is a hierarchical JavaSE/EE full-stack (one-stop) Lightweight open source framework. One-stop deployment refers to the use of the Spring framework to build a WEB application, the DAO layer Spring JDBC Template, the WEB layer SpringMVC, and the SERVICE layer Spring IoC. Spring's most fundamental mission is to simplify Java development. So how does Spring simplify Java Development?
With this mission, Spring adopts the following 4 key strategies to reduce Java Development complexity:
1. POJO-based lightweight and minimal invasive programming
POJO (Plain Ordinary Java Object): A simple Java Object, actually a common JavaBeans.
Intrusive programming method: the framework forces the application to inherit their classes or implement their interfaces, binds the application to the framework, and is tightly coupled.
Spring does not force you to implement Spring standard interfaces or inherit Spring standard classes. In Spring-based applications, there is no trace of its classes, indicating that you have used Spring, even if you use Spring annotations in the class, it is still POJO, so that the application objects are loosely coupled with each other. So how does Spring achieve this? One way is to assemble them through DI (dependency injection.
2. Loose coupling through dependency injection and interface-oriented
Dependency injection (DI): The dependency between objects is set by third-party components in the system responsible for coordinating objects. objects do not need to be created or managed by themselves.
Any practical application may consist of two or more classes that collaborate to complete specific business logic. In traditional practice, each object is responsible for managing the reference of objects that interact with itself (that is, the objects on which it depends), which will lead to highly coupled and hard-to-test code. As follows:
Package chapter1; import infs. knight; public class DamselRescuingKnight implements Knight {// reference of private RescueDamselQuest quest for objects dependent on the DamselRescuingKnight class;/*** create the dependent object in the constructor */public DamselRescuingKnight () {this. quest = new RescueDamselQuest (); // tightly coupled with RescueDamselQuest} public void embarkOnQuest () {// The server guard can only perform the RescueDamselQuest adventure task quest. embark ();}}
In this Code, DamselRescuingKnight is closely coupled with RescueDamselQuest, which greatly limits the capabilities of the server guard for adventure (that is, it cannot expand the capabilities of the server guard. If the server guard needs to be rescued, you must write another class). Worse, you cannot perform unit tests.
Coupling has two sides. On the one hand, tightly coupled code is hard to test, reusable, and hard to understand. On the other hand, a certain degree of coupling is required, and different classes must interact in an appropriate way.
As follows, Spring achieves loose coupling through DI.
Figure 1.1 Dependency injection automatically delivers the dependency to the target object instead of allowing the object to obtain the dependency.
Package chapter1; import infs. knight; import infs. quest; public class BraveKnight implements Knight {// use the Quest interface private Quest quest; /*** inject Quest through constructor * any class object that implements the Quest interface can be injected * @ param quest */public BraveKnight (Quest quest) {this. quest = quest;} public void embarkOnQuest () {// you do not know which type of Quest is used. embark ();}}
The most important aspect of the above Code is that BraveKnight is not coupled with a specific Quest implementation. For it, only the Quest interface is required for the adventure task. The specific type of adventure is irrelevant. This is the biggest benefit of DI-loose coupling.
One of the most common ways to replace dependencies is to use mock for testing.
Package chapter1; import static org. mockito. mockito. *; import org. junit. test; import infs. quest; public class BraveKnightTest {@ Test public void knightShouldEmbarkOnQuest () {// use mock (class) to create the Quest object Quest mockQuest = mock (Quest. class); // inject Quest BraveKnight knight = new BraveKnight (mockQuest); knight. embarkOnQuest (); // when calling the embarkOnQuest () method, verify requires the Mockito framework to verify that the embark () method implemented by the mock of Quest calls verify (mockQuest, times (1 )). embark ();}}
Now the BraveKnight class can accept any Quest adventure task. If I need the brave knight to destroy a dragon, how can I pass the Quest task specific to the dragon?
Package chapter1; import java. io. printStream; import infs. quest; public class DestoryDragonQuest implements Quest {private PrintStream stream; public DestoryDragonQuest (PrintStream stream) {this. stream = stream;} public void embark () {stream. println ("get rid of evil dragons! ");}}