Summary and architecture of spring (I)

Personal description:

This link is referenced in the following section: https://www.tutorialspoint.com/spring/spring_architecture.htm The other part adds my personal experience and experience

The reason for reference, first, the review of relevant conceptual knowledge; Second, systematization and organization.

What I want to do is to give beginners a general overview and description of relevant applications, and give intermediate developers a knowledge review and inspiration experience.

As for senior developers, i.e. architects or experts, I won't teach them any more, but I also very much hope that their opinions and opinions will collide with each other to generate knowledge sparks.

This blog post mainly reviews my experience and some profound experience in using spring. In addition, I hope to add some understanding of the source code.

For beginners, it's OK to know how to use it, but for non beginners, you should not only know how to use it, but also know how to use it better (this requires you to know its underlying principle and implementation mechanism).

1、 Brief description

Spring is the most popular application development framework for enterprise Java. Millions of developers around the world use the spring framework to create high-performance, easy to test and reusable code.

The spring framework is an open source Java platform. It was originally written by rod Johnson and first released under the Apache 2.0 license in June 2003.

Springs are lightweight in size and transparency. The basic version of the spring framework is about 2MB.

The core functionality of the spring framework can be used to develop any Java application, but there are some extensions for building web applications on the Java EE platform. The goal of spring framework is to make J2EE development easier to use and promote good programming practice by enabling POJO based programming model.

2、 Benefits of using spring

(1) Spring enables developers to develop enterprise applications using POJOs. The advantage of using POJOs only is that you don't need EJB container products (such as application servers), but you can choose to use only powerful servlet containers (such as Tomcat) or some commercial products.

For beginners, you may wonder, what is POJO?

In short, POJO is equivalent to JavaBeans, also known as entity classes.

What's the difference between an application server and a web server? What are the common application servers and web servers?

For example, Tomcat is a common application server. Of course, some of my friends use JBoss, resin or Weblogic.

Common web servers: such as Apache, nginx, etc

Similarities (commonalities): comply with HTTP protocol

Differences: (the application server can handle both static requests (images, JS, CSS, HTML, etc.) and dynamic requests (can embed server-side scripts, such as JSP, freemaker, volocity, etc.), while the web server can only handle static requests, not static requests. Of course, The efficiency of web server in processing static resources is naturally much higher than that of application server in processing static resources.

(2) Spring is organized in a modular way. Even if the number of packages and classes is large, you only need to worry about what you need and ignore the rest.

(3) Instead of reinventing the wheel, spring really makes use of some existing technologies, such as several ORM frameworks, logging frameworks, Jee, quartz and JDK timers and other view technologies.

Common ORM frameworks: Spring JDBC, mybatis, hibernate, etc;

Common log frameworks: log4j or slif4j;

View technology: JSP + volocity + FreeMarker, etc;

Timing task: quartz, which is easier to understand than JDK timing task;

(4) Testing applications written in spring is simple because environment dependent code is moved into the framework. In addition, it becomes easier to inject test data using dependency injection by using javabeanstyle POJO.

For example, junit4 or juni5 of spring

(5) Spring's web framework is a well-designed web MVC framework, which provides a good alternative to the web framework, such as struts or spring MVC, which is popular through spring's fame and its own lightweight implementation compared with struts 2.

(6) Spring provides a convenient API for converting technology specific exceptions (such as JDBC, hibernate, or JDO throws) into consistent, unchecked exceptions.

The API provided by spring is very easy to understand. Spring is easy for people with a certain Java foundation

(7) Lightweight IOC containers are often lightweight, especially when compared with EJB containers. This facilitates the development and deployment of applications on computers with limited memory and CPU resources.

Speaking of lightweight, what is lightweight? Why lightweight? What benefits will lightweight bring to actual project development?

Lightweight, in short, it means low coupling and weak code interdependence. It follows the principle of "high cohesion and low coupling" in software development.

It explains what lightweight is and why lightweight is better. No one wants to change this code when the requirements change, and there will be problems with that code. In other words, it is not easy to solve a bug, and new bugs appear again. The stronger the code coupling and the lower the cohesion, the more bugs will be changed. Finally, in addition to working hard to change bugs and bugs, This is one of the important reasons why programmers die suddenly, and why they can't find objects. It also takes time to change bugs in the computer every day. The company works overtime all night and has to change them when they come back, This leads to less time for personal improvement (personal improvement mainly lies in reading, summarizing and practicing at ordinary times, plus learning from open source projects and learning from the solutions of predecessors. Although there should be an innovative spirit, taking China's reform and opening up as an example, it is now a socialist road with Chinese characteristics, but here in New China (before 1978) we imitated the Soviet Union and took the socialist road of the Soviet Union. During the Manchu and Qing Dynasties, we learned from the West and set off the Westernization Movement, reform and reform, the revolution of 1911, etc., but in the end, they failed one after another. Why? Because we ignored our own actual situation. One saying is to act according to our ability, and the other is to act according to our ability The cycle is gradual, and the last sentence is accumulated over time).

Finally, what benefits will lightweight bring to actual project development? In short,

First, the code has strong scalability;

Second, the code maintainability is good;

Third, if there are fewer bugs, there will be less overtime. Less overtime is not only conducive to life and health, but also conducive to the improvement of code skills.

Supplementary note: I don't think anyone wants to change it every day because they are usually not serious or coding. They don't want to think about how to do it better, which is more conducive to expansion and maintenance. Instead, they regard it as manual labor. What's the difference between four years and five years and one year and two years? People should have dreams. There is no difference between dreams and salted fish.

(8) Spring provides a consistent transaction management interface, which can be reduced to local transactions (for example, using a single database) and extended to global transactions (for example, using JTA or using spring's XML transactions or annotation transactions).

Transaction, why transaction? The example of a bank withdrawing money can well illustrate this point. I won't repeat it here. There are a lot of examples and blog notes on the Internet.

3、 Dependency injection

The most common technology in spring is the dependency injection (DI) style of inversion of control. This control inversion (IOC) is a general concept and can be expressed in many different ways. Dependency injection is only a specific example of control inversion.

When writing complex Java applications, application classes should be independent of other Java classes as much as possible to increase the possibility of reusing these classes, and test them independently of other classes during unit testing. Dependency injection helps to glue these classes together while maintaining their independence.

What exactly is dependency injection? Let's look at these two words separately. Here, the dependency part is transformed into an association between the two classes. For example, class a depends on class B. Now, let's look at the second part, injection. All this means that class B will be injected into class A by IOC.

Dependency injection can be implemented by passing parameters to constructors or post construction using setter methods.

Explain with the following code:

PostMapper. java

PostService. java

PostServiceImpl. java

The corresponding XML configuration file is:

Through this example, I think you should understand what dependency injection is. If you don't write it like this, you must instantiate it if you want to obtain the corresponding data access layer (Dao layer) data. More than ten or twenty are easy to manage, thousands of them? Then you must have the urge to swear. At the same time, I also understand the benefits of spring managing objects for you. If you don't have spring to manage objects and instantiate them one by one, it will be a very scary thing.

4、 Aspect oriented programming

A key component of spring is the aspect oriented programming (AOP) framework. Functions that span multiple points of an application are called cross domain problems, which are conceptually separated from the business logic of the application. There are various common good examples in various aspects, including logging, declarative transactions, security, caching, etc.

The key unit of modularization in OOP is class, while in AOP, the modular unit is aspect. Di helps you separate application objects from each other, while AOP helps you separate cross cutting issues from the objects they affect.

The AOP module of the spring framework provides an aspect oriented programming implementation that allows you to define method interceptors and pointcuts to cleanly decouple code that implements functions that should be separated.

Take spring's XML annotation configuration as an example:

The above example, such as the configuration section, is equivalent to what you must pass anyway. For example, the TV King Zhongshan often goes to a certain road to rob passers-by of their money.

Of course, the above configuration is mainly about transactions. Those who are familiar with XML transaction configuration are familiar with the above XML code.

It doesn't matter if you are not familiar with it. If you use it more, you will understand it naturally. In addition, the above also represents a rule. For example, my adding method is called useradd, but my transaction configuration must start with add or append or other read only to add data in the corresponding table of the database. Now my adding method is called useradd, It will trigger another rule included in the rule: < TX: method name = "*" propagation = "required" read only = "true" / >, which judges all the items not listed above as queries, and my useradd is new, but does not comply with the above configured business rules, which will lead to an error and fail to insert data.

In this way, you may know its application. If it is traditional Java programming (non spring), we will have to write a lot of judgments.

5、 Architecture diagram of spring

I often use spring related components to develop projects. I think I must be familiar with the above architecture diagram.

Brief overview:

Core container

The core container consists of core, bean, context and expression language modules. Its details are as follows-

Data access / integration

The data access / integration layer consists of JDBC, ORM, oxm, JMS and transaction modules. Its details are as follows-

Web

The web layer consists of web, web MVC, web socket and web portlet modules. Its details are as follows-

Miscellaneous

There are few other important modules, such as AOP, aspects, instrumentation, web and test modules. The details are as follows-

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>