Gossip – learn more about spring
WHY
At the beginning of its birth, the main purpose of creating spring was to replace more heavyweight enterprise Java technology, especially EJB. Compared with EJB, spring provides a more lightweight and simple programming model.
WHAT
Spring is an open source framework, first created by rod Johnson. Spring is created to solve the complexity of enterprise application development. Using spring can enable simple JavaBeans to achieve things that only EJBs can do before. Spring is not limited to server-side development. Any Java application can benefit from spring in terms of simplicity, testability and loose coupling.
Now spring is involved in mobile development, social API integration, NoSQL database, cloud computing and big data. Over time, EJB also adopted the concepts of dependency injection (DI) and aspect oriented programming (AOP). In short, spring's fundamental mission is to simplify java development
HOW
In order to reduce the complexity of java development, spring has adopted four key strategies
Lightweight and minimally intrusive programming based on POJO realizes loose coupling through dependency injection and interface orientation
Declarative programming based on facets and conventions reduces style code through facets and templates
POJO potential
Many frameworks force applications to inherit their classes or implement their interfaces, resulting in the binding of applications and frameworks. This is intrusive programming, resulting in the inability to reuse code blocks. Spring tries its best to avoid confusing your application code with its own API. In applications built based on spring, there is usually no trace of its classes that you use spring
The above example code represents a very simple and ordinary Java class (POJO). You can't see that it is a spring component. The non-invasive programming of spring is reflected in that this class can play a role in both spring applications and non spring applications. Only such a piece of code can not actually reflect the spring function, and the following knowledge is needed. Dependency injection (injecting self dependent classes into itself)
Dependency injection is not so big in spring, although it has evolved into a complex programming skill or design pattern concept. It can be understood in spring that dependency injection. A practical application requires multiple classes to cooperate with each other to complete specific business logic. The traditional approach is that each object is responsible for managing the references of its own related objects (the related objects are the dependent objects expressed in spring), but this will lead to high coupling and difficult code testing
Consider the following codes
Coupling has two sides. On the one hand, closely coupled code is difficult to test, reuse and understand, and there will be "ground mouse" bugs. On the other hand, a certain degree of coupling is necessary, and different classes must interact in an appropriate way.
The problem arises, so how does spring solve it
In spring, through dependency injection (DI), the dependencies between objects are set by the third-party component responsible for coordinating each object in the system when creating the object. In other words, objects only need to manage their internal properties without creating or managing their dependencies. Dependencies will be automatically injected into the objects that need them.
Braveknight is not coupled with any specific quest implementation. As long as a task implements the quest interface, it doesn't matter what type of adventure it is, which achieves the purpose of di - loose coupling
If an object only indicates dependencies through interfaces, such dependencies can be replaced with different concrete implementations without the knowledge of the object itself.
Now let's inject practical significance
For the above code, let's inject an adventure mission with specific implementation into the brave knight
So now the question is how to inject the printstream object it depends on in slaydragonquest and how to inject the quest object it depends on in braveknight. As mentioned earlier, spring will centrally manage these dependencies, and the behavior of creating collaboration between application components is usually called wiring, that is, injection. Spring provides a variety of assembly methods, which will be described in more detail later. Here we briefly introduce XML based assembly and Java annotation based assembly
Spring provides Java based configuration as an alternative to XML
The effect is the same, and the specific explanation is described in detail in the next chapter. Now let's review that spring has always said that it will automatically manage the dependencies between objects, so what is this manager. The answer is application context, which is a spring container that can load bean definitions and assemble them. The spring application context is fully responsible for the creation and assembly of objects. In fact, there are many implementations of this context. The only difference between them is the way of loading configuration. Let's look at a way of loading configuration
Application section
Di can keep the software components that cooperate with each other loosely coupled, while aspect oriented programming allows you to separate the functions throughout the application to form reusable components. In more detail, it is a technology to promote the focus of software system implementation. What are concerns? System services such as logging, transaction management and security management often need to be integrated into other components with business logic, so these system services are usually called crosscutting concerns, because they will be reused in multiple places and span multiple components of the system. To put it simply, you need to separate the components that often need to be reused from other components, but how to use the extracted components is actually to insert the methods where they need to be used. However, according to the term "facet", it should be expressed as pulling out the reused components as a facet, and cutting the facet into the components when it needs to be used. Therefore, the core application does not need to know the existence of these aspects, and the aspects will not integrate the business logic into the core application.
At this time, the brave knight began to perform, but he found that he was not only taking risks in his duties. Now he even managed the singer to sing praises for him, but this itself should not belong to this category. Therefore, using the idea of aspect, we need to separate the singer's singing behavior into a aspect. Before the knight adventure, this aspect will cross cut into and execute the singbeforequest method, and execute the singafterquest method after the adventure. So does this realize the code that does not need to be praised in the knight, and the singer does not exist in the knight object. He will not only praise the knight, but also praise anyone, as long as others use this aspect
The current situation is that minstrel is still an independent POJO, and the spring context has turned it into a facet. The most important thing is that the knight doesn't know the existence of this section at this time. It's just a small chestnut, which can actually do many important things.
Eliminate style codes using templates
In this case, when we use JDBC to access the database and query data, the complete process needs to establish connections, create statement objects, process result sets, query and close various connections. In addition, it also needs to capture various exceptions, and then the queries in various scenarios need to be repeated with such effort. JDBC is not the only case where there will be a lot of style code. Spring aims to eliminate style code through template encapsulation, such as spring's JDBC template.
Accommodate your bean
In spring based applications, your application objects exist in the spring container, which is responsible for creating objects, assembling objects and managing their life cycle. What is spring's container? There is not only one container. Spring comes with multiple container implementations. It is divided into two categories: bean factory, which is the simplest container and provides basic Di support. The application context is relatively advanced and provides application framework level services. In most cases, the application context is more popular.
Application contexts are also divided into many types. The significant difference is that the loading configuration methods are different
Various functions of spring
summary
Spring is a framework technology that can simplify development. The core content is di and AOP.
The above is the gossip in this article - gradually understand the whole content of spring, and I hope it will be helpful to you. Interested friends can continue to refer to this website:
Springmvc getting started example
Spring uses the configuration file and @ value to inject attribute value code
Spring integrated redis detailed code example