Traditional Culture Encyclopedia - Traditional festivals - What is Spring in java?
What is Spring in java?
1.1.1 what is spring
Spring is an open source lightweight Java SE(Java standard version) /Java EE(Java
enterprise version) development and application framework, which aims to simplify enterprise application development. An application is composed of a set of objects that cooperate with each other. In traditional application development, a complete application is composed of a group of cooperating objects. Therefore, in addition to developing business logic, the most important thing to develop an application is how to make these objects cooperate to complete the required functions, and it should be 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. Some people may have said, for example, "abstract factory, factory method design pattern" can also help us create objects, and "generator pattern" can help us deal with the dependencies between objects, can't it also accomplish these functions? However, these require us to create other factory classes and generator classes, and we have to manage these classes from outside, which increases our burden. If we can create objects and manage the dependencies between objects through configuration, we don't need to create and manage the dependencies between objects through factories and generators. Does this reduce a lot of work, speed up development and save a lot of time to do other things? When the Spring framework first came out, it was mainly to accomplish this function.
Spring framework not only helps us manage objects and their dependencies, but also provides aspects-oriented capabilities such as general logging, performance statistics, security control, exception handling, etc. It can also help me manage the most troublesome database transactions, and provides a simple JDBC access implementation and integration with third-party data access frameworks (such as Hibernate and JPA). Integrate with various Java
EE technologies (such as Java Mail, task scheduling, etc.), provide a set of own web layer framework Spring
MVC, and can also be easily integrated with third-party web frameworks. From this, we can think that Spring is a super bonding platform, which not only provides its own functions, but also provides the ability to bond other technologies and frameworks, so that we can choose what technology to use for development more freely. And both JAVA
SE(C/S architecture) applications and JAVA EE(B/S architecture) applications can be developed using this platform. Let's take a closer look at what Spring can do for us.
1.1.2 What can Spring do for us
Spring can help us simplify development except for writing business logic:
First, in traditional program development, we control the creation of objects and the assembly of dependencies between objects 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 it; However, if Spring is adopted, Spring will create and assemble the dependencies between objects according to the configuration file, and only need to change the configuration file without recompiling. Therefore, Spring can help us create and assemble dependencies between objects according to configuration files.
Second, when we want to do some logging, permission control, performance statistics, etc., in traditional applications, we may do it in the required objects or methods, and most of them, such as permission control and performance statistics, are repetitive, so there are a lot of repetitive codes in the code. Even if someone says that I have extracted the common part, there must be calls or repetitions. For example, we may only do performance statistics when necessary, and delete these codes after diagnosis; There are also log records, such as recording some method access logs, data access logs, etc., which will penetrate into each method to be accessed; There is also permission control, which must be reviewed at the beginning of method execution. Think about how terrible and boring these tasks are. If Spring is adopted, these logging, permission control and performance statistics are separated from the business logic, and these functions are dynamically added where they are needed through the aspect-oriented programming supported by Spring, without infiltrating into all the required methods or objects; Some people may say that we can use "proxy design pattern" or "wrapper design pattern". You can use these, but you still need to create proxy objects through programming, or you need to couple these proxy objects. Spring
aspect-oriented programming can provide a better way to accomplish the above functions, generally through configuration, and you don't need to add any extra code to the existing code, which focuses on business logic. Therefore, Spring
aspect-oriented programming can help us realize logging, performance statistics and security control without coupling.
third, in the traditional application, how do we complete the database transaction management? It requires a series of "getting a connection, executing SQL, committing or rolling back a transaction, and closing the connection", and it is also necessary to ensure that the connection must be closed at the end. What a terrible thing, and it is also boring; If we adopt Spring, we just need to get the connection and execute SQL, and the rest will be managed by Spring. Let's make it simple. Therefore, Spring can help us manage database transactions very simply.
Fourth, Spring also provides seamless integration with third-party data access frameworks (such as Hibernate and JPA), and it also provides a set of JDBC access templates to facilitate database access.
5. Spring also provides seamless integration with third-party web (such as Struts and JSF) frameworks, and it also provides a set of Spring MVC frameworks to facilitate the construction of the Web layer.
sixth, Spring can be easily integrated with Java EE (such as Java Mail and task scheduling) and more technologies (such as caching framework).
Spring can help us to do so many things, provide so many functions and integrate with so many mainstream technologies, and it also helps us to do more headaches and difficulties in development. Then some people may ask, is there only a framework of Spring, and 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 the development of small and medium-sized projects is to kill the chicken and take the bull's knife. Although EJB is relatively easy to use now, it is still cumbersome and needs to rely on the application server. So why use Spring instead of other frameworks? Let's continue to look down.
1.1.3 Why do you need Spring
First, explain several concepts
1. Applications: finished products that can complete the functions we need, such as shopping websites and OA systems.
2. Framework: it is a semi-finished product that can complete certain functions. For example, we can use the framework to develop shopping websites. The framework does some functions, and we do some functions ourselves, so that the application is created. Moreover, the framework stipulates the overall architecture of your application development, provides some basic functions, and also stipulates how to create classes and objects and how to cooperate, thus simplifying our development and allowing us to focus on business logic development.
3. Non-invasive design: It can be understood from the perspective of the framework that this design can be regarded as non-invasive design without inheriting the classes provided by the framework. If these framework classes are inherited, it is invasive design. If you want to replace the framework in the future, the code written before can hardly be reused. If it is non-invasive, the code written before can still be used.
4. Lightweight and heavyweight: Lightweight is relative to heavyweight. Lightweight is generally non-invasive, relies on very few things, occupies very little resources, is simple to deploy, etc. In fact, it is relatively easy to use, while heavyweight is just the opposite.
5. POJO: POJO (plain old Java
objects) is a simple Java object, which can contain business logic or persistence logic, but does not play any special role and does not inherit or implement any other spring mvc classes or interfaces.
6. Container: In daily life, a container is a container for holding things. From the perspective of programming, it is the object for holding objects. Because of the operations of putting in and taking out, the container also needs to manage the life cycle of objects.
7. Control inversion: Inversion of Control, abbreviated as IoC. The other name of control inversion is Dependency
Injection, which means that the relationship between programs is controlled by the container, instead of being directly controlled by the program code in the traditional implementation.
8. Bean: generally refers to container management object, and in Spring refers to Spring IoC container management object.
Second
Why do you need the advantages of Spring and Spring
●
Very lightweight container: create and assemble application objects in a centralized and automated way, take charge of object creation and assembly, manage the life cycle of objects, and can be combined into complex applications. Spring container is non-intrusive (it doesn't need to rely on any Spring-specific classes), and it is completely developed by POJOs, which makes the application easier to test and manage. Moreover, the core JAR package is very small, less than 1M in Spring3..5, and can be deployed in any environment (Java
SE or Java EE) without relying on any application server.
● AOP: AOP is the abbreviation of Aspect Oriented
Programming, which means aspect-oriented programming. It provides a technology to consider the program structure from another angle to improve object-oriented programming (compared with OOP), that is, it can dynamically add functions to the program without modifying the source code during compilation, loading or running. Generally speaking, it is to extract reusable functions and then weave these general functions into the application at the right time; For example, security, diary recording, these are all common functions, we can extract them, and then weave these codes in the appropriate place where the program executes and execute them, so as to complete the required functions and reuse them.
●
Simple database transaction management: In the application that uses the database, it is a headache to manage the database transaction by yourself, and it is easy to make mistakes. Spring supports pluggable transaction management support and does not need JEE environment support. Managing transactions through Spring can free us from transaction management and focus on business logic.
●
JDBC abstraction and ORM framework support: Spring makes JDBC easier to use; Provide DAO (data access object) support, which is very convenient to integrate third-party ORM frameworks, such as Hibernate; And fully support Spring transactions and use the consistent exception system provided by Spring.
● Flexible Web layer support: Spring itself provides a very powerful MVC framework, and it can be easily integrated with third-party MVC frameworks, such as Struts.
● simplify the integration of various technologies: provide the 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, and manage affairs for us. The simple MVC framework can be regarded as a super-bonding platform, which can integrate many technologies into a whole, make the system structure better and the performance more outstanding, thus accelerating our program development. There is no reason why we should not consider using it.
1.1.4 How to learn Spring well
To learn Spring well, we must first make clear what Spring is and what it can do for us. Knowing this, we will make a simple example, so that we can basically know how to use Spring. The core of Spring is IoC container, so we must thoroughly understand what IoC container is and how to configure and use it. All other technologies are based on containers. After understanding IoC well, the next step is aspect-oriented programming. First, the concept, basic configuration, and implementation principle are clear. Next, database transaction management. In fact, Spring management transactions are realized through aspect-oriented programming, so the foundation is very important. After IoC container and aspect-oriented programming are completed, the rest are based on these two things, so it is easier to learn. If you want to learn Spring well, you must lay a solid foundation, which means sharpening the knife and not cutting wood by mistake.
1.2 Spring foundation
1.2.1 Spring architecture diagram
figure 1-1 Spring architecture diagram
Core containers: including core, Beans, Context and EL modules.
●
Core module: encapsulates the lowest part of the framework, including resource access, type conversion and some common tool classes.
●
Beans module: provides the basic part of the framework, including inversion control and dependency injection. Among them, Bean
Factory is the core of the container, and its essence is the realization of "factory design pattern", and it does not need to program to realize "singleton design pattern", and singleton is completely controlled by the container, and it advocates interface-oriented programming instead of implementation-oriented programming; All application objects and the relationships between them are managed by the framework, so that you can really extract the dependencies between the maintenance objects from the program logic, and all these dependencies are maintained by BeanFactory.
● Context module: based on Core and Beans, it integrates the functions of Beans module and adds resource binding, data verification, internationalization, Java
EE support, container life cycle, event propagation, etc. The core interface is ApplicationContext.
●
EL module: provides powerful expression language support, supports accessing and modifying property values, method calls, accessing and modifying arrays, containers and indexers, naming variables, arithmetic and logical operations, and obtaining Bean from Spring
containers. It also supports list projection, selection and general list aggregation.
AOP, Aspects module:
● AOP module: Spring
AOP module provides aspect-oriented programming (aspect-oriented
p) conforming to the AOP Alliance specification.
- Previous article:Are Crayon Shinchan and Doraemon anime or anime, and what does tomato mean?
- Next article:Abstract of China Traditional Guqin Papers
- Related articles
- Find the name of a novel
- Cangzhou Far East Wedding Hall Hotel Address
- What does Sanya Luhuitou mean?
- How to do a new media operation planning from scratch
- How to design the database of house leasing system?
- What month is August?
- Handmade jobs outward processing have really
- The double control switch wants to be used as a single control. How to connect wires.
- Download the complete works of "barbaric growth" txt
- Tips for making beef burgers