Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of Spring?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the role of Spring". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is Spring?

Spring is an open source lightweight Java SE (Java Standard Edition) / Java EE (Java Enterprise Edition) development application framework designed to simplify enterprise application development. An application is made up of a set of objects that cooperate with each other. In traditional application development, a complete application is composed of a set of objects that cooperate with each other. Therefore, in addition to developing business logic, the most important thing to develop an application is to focus on how to make these objects cooperate to complete the required functions, with low coupling and high cohesion. Business logic development is inevitable, so if there is a framework to help us create objects and manage the dependencies between these objects. Someone may have said, for example, "Abstract Factory, Factory method Design pattern" can also help us create objects, "Generator pattern" can help us deal with the dependencies between objects, can not also accomplish these functions? However, these require us to create other factory classes and generator classes, and we have to manage these classes outside, which increases our burden. If there is a way to create objects and manage dependencies between objects through configuration, we do not need to create and manage dependencies between objects through factories and generators, so do we reduce a lot of work and speed up development? It can save a lot of time to do other things. When the Spring framework first came out, it was mainly to complete this function.

The Spring framework not only helps us manage objects and their dependencies, but also provides aspect-oriented capabilities such as general logging, performance statistics, security control, exception handling, etc., and also helps me manage the most troublesome database transactions. It provides a set of simple JDBC access implementation, provides integration with third-party data access frameworks (such as Hibernate, JPA), and integrates with various Java EE technologies (such as Java Mail, task scheduling, etc.) Provide a set of own web layer framework Spring MVC, but also can be very easy to integrate with third-party web framework. From here, we can think of Spring as a super adhesive platform, which not only provides its own functions, but also provides the ability to glue other technologies and frameworks, so that we can more freely choose which technologies to use for development. And both JAVA SE (Cramp S architecture) applications and JAVA EE (Bhand S architecture) applications can be developed using this platform. Let's take a closer look at what Spring can do for us.

What can Spring do for us?

Except that Spring can't help us write business logic, almost everything else can help us simplify development:

First, traditional program development, creating objects and assembling dependencies between objects are controlled by us within the program, which will increase the coupling between objects. If we want to modify the dependencies between objects, we must modify the source code, recompile and deploy; and if we use Spring, then Spring will create and assemble dependencies between objects according to the configuration file, only need to change the configuration file, without recompilation. So, Spring can help us create and assemble dependencies between objects based on configuration files.

Second, when we want to do some logging, access control, performance statistics, etc., in traditional applications, we may do it in the objects or methods we need, and for example, access control and performance statistics are mostly duplicate. in this way, there is a lot of duplicate code in the code, even if some people say that I extract the common part, there must be calls or duplications. Like performance statistics, we may only do it when necessary, and we need to delete the code after diagnosis. There are also logging records, such as recording method access logs, data access logs, and so on, which infiltrate each method to be accessed; there is also permission control, which must be audited at the beginning of method execution. think about how scary and boring this is. If you use Spring, these logging, access control, and performance statistics are separated from the business logic, and through aspect-oriented programming supported by Spring, these functions are dynamically added where they are needed without infiltrating into the required methods or objects Some people may say that we can use "proxy design pattern" or "wrapper design pattern", and you can use these, but you still need to create proxy objects programmatically or to couple them, and aspect-oriented programming with Spring provides a better way to do this, usually through configuration, without adding any additional code to existing code. The existing code focuses on business logic. Therefore, Spring aspect-oriented programming can help us achieve logging, performance statistics, and security control without coupling.

Third, in traditional applications, how do we complete database transaction management? Need a series of "get the connection, execute the SQL, commit or roll back the transaction, close the connection", and make sure that the connection is closed at the end, which is terrible and boring; if we use Spring, we just need to get the connection, execute SQL, and leave the rest to Spring to manage, simple. Therefore, Spring can easily help us manage database transactions.

Fourth, Spring also provides seamless integration with third-party data access frameworks (such as Hibernate, JPA), and also provides a set of JDBC access templates to facilitate database access.

Fifth, Spring also provides seamless integration with third-party Web frameworks (such as Struts, JSF), and it also provides a set of Spring MVC framework to facilitate the construction of web layer.

6. Spring can be easily integrated with Java EE (such as Java Mail, task scheduling) and with more technologies (such as caching framework).

Spring can help us do so many things, provide so many functions and integrate with so many mainstream technologies, and help us do more headache and difficult things in development, then some people may ask, is there only Spring framework, there is no other choice? Of course, there are, for example, EJB needs to rely on the application server, the development efficiency is low, and in the development of small and medium-sized projects, although EJB is easier to use now, it is still relatively bulky and needs to rely on the application server. So why do you need to use Spring instead of other frameworks? Let's move on.

Why do you need Spring

First of all, explain several concepts.

1. Application: it is the finished product that can complete the functions we need, such as shopping website and OA system.

2, framework: can complete a certain function of the semi-finished product, for example, we can use the framework for shopping website development; framework to do part of the function, we do part of the function, so that the application is created. And the framework defines the overall architecture of your application, provides some basic functions, and defines how classes and objects are created, how to collaborate, etc., thus simplifying our development and allowing us to focus on business logic development.

3, non-intrusive design: from the perspective of the framework, it can be understood that this kind of design can be regarded as non-intrusive design without inheriting the classes provided by the framework. If you inherit these framework classes, it is an intrusive design. If you want to change the framework before the code can hardly be reused, if the non-intrusive design, the previously written code can still be used.

4. Lightweight and heavyweight: lightweight is relative to heavyweight. Lightweight is generally non-intrusive, relies on very few things, takes up very few resources, is easy to deploy, and so on. Heavyweight is just the opposite.

5. POJO:POJO (Plain Old Java Objects) is a simple Java object that can contain business logic or persistence logic, but does not play any special role and does not inherit or implement classes or interfaces of any other Java framework.

6. Container: in daily life, a container is a kind of instrument for holding things. From a programming point of view, it is an object that holds an object. Because there are operations such as putting in and taking out, the container also manages the life cycle of the object.

7. Control inversion: Inversion of Control, abbreviated as IoC, and control inversion is also called dependency injection (Dependency Injection), which is controlled by the container to control the relationship between programs, rather than the traditional implementation, which is directly controlled by program code.

8. Bean: generally, it refers to container-managed objects. In Spring, it refers to Spring IoC container-managed objects.

Second, why do you need the advantages of Spring and Spring

● is a very lightweight container: creates and assembles application objects in a centralized and automated way, is responsible for object creation and assembly, manages the object life cycle, and can be combined into complex applications. The Spring container is non-intrusive (does not rely on any Spring-specific classes) and is developed entirely in POJOs, making applications easier to test and manage. And the core JAR package is very small, less than 1m Spring3.0.5, and does not need to rely on any application server, can be deployed in any environment (Java SE or Java EE).

● AOP:AOP is an acronym for Aspect Oriented Programming, meaning aspect-oriented programming, which provides another perspective to consider the program structure to improve object-oriented programming (as opposed to OOP), that is, a technology that can dynamically add functionality to a program without modifying the source code during compilation, loading, or running. The popular point is to extract the reusable functions, and then weave these common functions into the application at the right time; such as security, diary, these are all general functions, we can extract them, and then weave the code and execute them in the right place where the program is executed, so as to complete the required functions and reuse them.

● simple database transaction management: in applications that use the database, managing database transactions by ourselves is a headache and prone to errors. Spring supports pluggable transaction management support, and there is no need for JEE environment support. Spring transaction management can liberate us from transaction management to focus on business logic.

● JDBC abstraction and ORM framework support: Spring makes JDBC easier to use; provides DAO (data access object) support, which is very convenient to integrate third-party ORM frameworks, such as Hibernate; and fully supports Spring transactions and uses the consistent exception architecture provided by Spring.

Flexible Web layer support for ●: Spring itself provides a very powerful MVC framework and can be easily integrated with third-party MVC frameworks, such as Struts.

● simplifies the integration of various technologies: providing integration of Java Mail, task scheduling, JMX, JMS, JNDI, EJB, dynamic language, remote access, Web Service, etc.

Spring can help us simplify application development, help us create and assemble objects, manage transactions for us, simple MVC framework, can regard Spring as a super adhesive platform, can integrate a lot of technologies together to form a whole, so that the system structure is better, the performance is better, thus accelerate our program development, like the above advantages, we have no reason not to consider using it.

How to learn Spring well

To learn Spring well, we must first make clear what Spring is and what it can do for us, know this, and then make a simple example, so that we basically know how to use Spring. The core of Spring is IoC container, so you must thoroughly understand what IoC container is and how to configure and use it. All other technologies are based on container. After a good understanding of IoC, the next step is aspect-oriented programming, first of all, clear concepts, basic configuration, and finally implementation principles, and then database transaction management. In fact, Spring management transactions are achieved through aspect-oriented programming, so the foundation is very important. After the IoC container and aspect-oriented programming are done, the rest are based on the implementation of these two things, which makes it easier to learn. If you want to learn Spring well, you can't be in a hurry. You must lay a solid foundation. This is to sharpen a knife without mistakenly cutting wood.

Spring basic Spring architecture diagram

Figure 1-1 Spring architecture diagram

Core container: including Core, Beans, Context, EL modules.

● Core module: encapsulates the lowest part of the framework dependency, including resource access, type conversion, and some common utility classes.

● Beans module: provides the basics of the framework, including inversion control and dependency injection. Among them, Bean Factory is the core of the container, which is essentially the implementation of the "factory design pattern", and there is no need to program to realize the "singleton design pattern". The singleton is completely controlled by the container, and it advocates interface-oriented programming rather than implementation-oriented programming. All application objects and their relationships are managed by the framework, so you can really extract the dependencies between maintenance objects from the program logic, all of which are maintained by BeanFactory.

● Context module: based on Core and Beans, integrates Beans module functions and adds resource binding, data validation, internationalization, Java EE support, container life cycle, event propagation, etc. The core interface is ApplicationContext.

● EL module: provides powerful expression language support, support to access and modify property values, method calls, support to access and modify arrays, containers and indexers, named variables, support for arithmetic and logical operations, support to get Bean from the Spring container, it also supports list projection, selection and general list aggregation.

AOP, Aspects modules:

● AOP module: the Spring AOP module provides aspect-oriented programming (aspect-oriented programming) implementation that conforms to the AOP Alliance specification, provides general functions such as logging, access control, performance statistics, and technology that separate business logic, and can dynamically add these functions to the required code; in this way, each dedicated role reduces the coupling of business logic and general functions.

● Aspects module: provides integration with AspectJ, and AspectJ provides more powerful features than Spring ASP.

Data access / integration module: this module includes JDBC, ORM, OXM, JMS and transaction management.

● transaction module: this module is used for Spring management transactions, as long as the Spring management objects can get the benefits of Spring management transactions, there is no need for transaction control in the code, and supports programming and declarative transaction management.

● JDBC module: provides a sample template for JBDC that eliminates traditional lengthy JDBC coding and necessary transaction control, and enjoys the benefits of Spring managing transactions.

● ORM module: provides seamless integration with popular object-relational mapping frameworks, including Hibernate, JPA, Ibatiss, etc. And you can use Spring transaction management without additional control over the transaction.

● OXM module: provides an implementation of mapping Object/XML, mapping java objects to XML data, or mapping XML data to java objects. The Object/XML mapping implementation includes JAXB, Castor, XMLBeans and XStream.

● JMS module: for JMS (Java Messaging Service), provides a set of "message producer, message consumer" templates for easier use of JMS,JMS for sending messages between two applications or in distributed systems for asynchronous communication.

● Web/Remoting module: the Web/Remoting module includes Web, Web-Servlet, Web-Struts, and Web-Porlet modules.

● Web module: provides basic web functionality. Examples include multiple file uploads, integrated IoC containers, remote procedure access (RMI, Hessian, Burlap), and Web Service support, and a RestTemplate class is provided to provide convenient Restful services access.

● Web-Servlet module: provides an implementation of the Spring MVC Web framework. The Spring MVC framework provides annotation-based request resource injection, simpler data binding, data validation, and a set of very easy-to-use JSP tags that work seamlessly with other Spring technologies.

● Web-Struts module: provides seamless integration with Struts, both Struts1.x and Struts2.x support

Test module: Spring supports Junit and TestNG testing frameworks, and also provides some additional Spring-based testing capabilities, such as the ability to simulate Http requests when testing the Web framework.

Typical application scenario

Spring can be applied to many scenarios, from the simplest standard Java SE programs to enterprise applications that can be built using Spring. Here are some popular application scenarios:

● typical Web application scenarios:

Figure 1-2 web application scenario

In the Web application scenario, a typical three-tier architecture: data model layer implements domain objects; data access layer implements data access; logic layer implements business logic; web layer provides page display; all these layer components are managed by Spring and enjoy the benefits of Spring transaction management and AOP, and the only entrance to the request is DispachterServlet, which maps the request to the corresponding web layer components to achieve the corresponding request function.

● remote access application scenarios:

Spring can easily provide exposed RMI services, remote access services such as Hessian, Burlap, etc., which can be easily implemented by configuring the corresponding address in Spring and the services that need to be exposed.

● EJB application scenarios:

Spring can also be easily integrated with EJB.

This is the end of the content of "what is the use of Spring"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report