What is Spring?

Source: Internet
Author: User
Tags aop java se

What is 1.1.1.Spring?

Spring is an open source lightweight Java SE (Java Standard Version)/java EE (Java Enterprise Edition) Development application framework designed to simplify enterprise application development. An application consists of a set of objects that work together. In traditional application development, a complete application consists of a set of mutually collaborative objects. So, in addition to developing business logic, the most common way to develop an application is to focus on how to make these objects work together to accomplish the required functionality, and to be low-coupled and high cohesion. Business logic development is unavoidable, if there is a framework to help us create objects and manage the dependencies between those objects. It may be said that, for example, "abstract factory, factory method design pattern" Can not help us to create objects, "generator mode" to help us to deal with the dependencies between objects, can not do these functions? But these need us to create another factory class, generator class, we have to manage these classes, add our burden, if we can have a configuration to create objects, to manage the dependencies between objects, we do not need to through the factory and generator to create and manage the dependencies between objects, So do we reduce a lot of work, speed up development, and save a lot of time to do other things. The spring framework came out primarily to complete this function.

In addition to helping us manage objects and their dependencies, the spring framework also provides facets-oriented capabilities like general-purpose logging, performance statistics, security controls, exception handling, and also helps me manage the most headaches of database transactions, providing a simple JDBC access implementation. Provides integration with third-party data access frameworks (such as Hibernate, JPA), integration with various Java EE technologies (such as Java Mail, task scheduling, and so on), provides a set of its own web-tier framework, spring MVC, and is also very simple to integrate with third-party web frameworks. From here we can think of spring as a super-bonding platform that, in addition to its own functionality, provides the ability to glue other technologies and frameworks, giving us a freer choice of what technology to use for development. 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 spring can do for us
Not only does spring help us write business logic, but almost anything else can help us simplify our development:

First, the traditional program development, the creation of objects and the dependencies between the assembled objects are controlled by us within the program, which will increase the coupling between the various objects, if we want to modify the dependencies between objects must modify the source code, recompile, deployment, and if the use of spring, By spring, the dependencies between objects are created and assembled based on the configuration file, and only the configuration files need to be changed without recompiling. So, spring can help us create and assemble dependencies between objects based on the configuration file.

Second, when we want to do some logging, permission control, performance statistics, etc., in the traditional application we may be in need of objects or methods, and such as permission control, performance statistics are mostly duplicated, so there is a lot of code duplication code, even if some people say I extract the generic part, There must be a call or a repetition, like performance statistics we may only do when necessary, after the diagnosis to remove the code, as well as log records, such as logging some methods access logs, data access logs, and so on, which will permeate the various access methods, as well as permission control, It must be reviewed at the beginning of the execution of the method to think about how horrible and boring the work is. With spring, these logging, permission control, and performance statistics are separated from the business logic, and are dynamically added to the required features by spring-supported aspect-oriented programming, without penetrating into the various methods or objects required; One might say that we can use the " Proxy design mode "or" Wrapper design pattern "You can use these, but still need to programmatically create proxy objects, or to coupling these proxy objects, and the use of spring-oriented programming can provide a better way to accomplish the above functions, generally through configuration, and without adding any extra code to the existing code, the existing code focuses on the business logic. Therefore, Spring aspect-oriented programming can help us to realize the non-coupled implementation of logging, performance statistics, security control.


Third, in traditional applications, how can we complete database transaction management? Requires a series of "Get the connection, execute SQL, commit or rollback the transaction, close the connection", but also make sure to close the connection at the end, what a horrible thing, but also very boring; if we take spring, we just need to get the connection, execute the SQL, and all the rest to spring to manage, simple. So, spring can help us manage database transactions very simply.

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

Spring also provides seamless integration with third-party web frameworks such as struts, JSF, and provides a spring MVC framework to facilitate web-tier building.

Six, spring can be easily integrated with Java EE (such as Java Mail, task scheduling), and more technology integration (such as caching framework).

Spring can help us do so many things, to provide so many functions and integration with so many mainstream technology, but also to help us do the development of more headaches and difficult things, it may be asked, there is only spring this framework, there is no other choice? Of course, such as EJB needs to rely on application server, development efficiency is low, in the development of small and medium-sized projects are slaughter chicken sledgehammer, although the development of the EJB is now more useful, but still more cumbersome to rely on application server and so on. So why do you need to use spring instead of other frameworks? Let's take a look down.


Why 1.1.3 Need Spring

First elaborated several concepts

1, the application: is able to complete the functions we need the finished product, such as shopping site, OA system.
2, Frame: is able to complete a certain function of semi-finished products, such as we can use the framework for shopping site development; The framework does part of the function, we do some of the functions ourselves, so that the application is created. And the framework prescribes the overall architecture of your application as you develop it, provides some basic functionality, establishes how classes and objects are created, collaborates, and so on, simplifying our development and allowing us to focus on business logic development.
3, non-intrusive design: From a framework perspective can be understood, without inheriting the framework provides classes, this design can be seen as a non-intrusive design, if the inheritance of these framework classes, is intrusive design, if you want to change the framework before the code is almost impossible to re-usable, If the non-intrusive design, the previously written code can still continue to be used.
4, lightweight and heavyweight: lightweight is relative to the heavyweight, lightweight is generally non-invasive, rely on very little, very little resources, deployment simple and so on, in fact, is relatively easy to use, and the weight of the exact opposite.
5, Pojo:pojo (Plain old Java Objects) A simple Java object that can contain business logic or persistent logic, but does not assume any special role and does not inherit or implement any other Java framework classes or interfaces.
6, Containers: In the daily life container is a kind of equipment to put things, from the point of view of program design is the object of the object, because there are put, take out and other operations, so the container also to manage the object's life cycle.
7, control reversal: that is, inversion of the control, abbreviation for the IOC, inversion of controls and a name called Dependency injection (Dependency injection), is the relationship between the container control program, rather than the traditional implementation, by the program code directly manipulated.
8, Bean: Generally refers to the container management object, in spring, the spring IOC container management objects.

Why do I need the advantages of spring and spring

Very lightweight containers: Create and assemble application objects in a centralized, automated manner, responsible for object creation and assembly, managing object lifecycles, and assembling complex applications. Spring containers are non-intrusive (no need to rely on any spring-specific classes) and are developed entirely with POJOs, making applications easier to test and easier to manage. And the core jar package is very small, Spring3.0.5 less than 1M, and does not need to rely on any application server, can be deployed in any environment (Java SE or Java EE).
AOP:AOP is the abbreviation for Aspect oriented programming, meaning aspect-oriented programming, which provides a different perspective to consider the program structure to improve object-oriented programming (as opposed to OOP), which can be achieved by the time of compilation, A technique for dynamically adding functionality to a program without modifying the source code during a load or during a run. The popular point is to extract the reusable functions, and then weave them into the application at the right time, such as security, journaling, these are common functions, we can extract them, and then weave the code and execute them in the proper place of the program execution. This completes the required functionality and re-uses the functionality.
Simple database Transaction management: In applications that use a database, managing database transactions yourself is a headache and is prone to errors, spring supports pluggable transaction management support, and requires no JEE environment support. Managing transactions through spring frees 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, and facilitates the integration of third-party ORM frameworks, such as Hibernate , and fully supports spring transactions and uses the consistent exception system provided by spring.
Flexible Web layer Support: Spring itself provides a very powerful MVC framework and can be easily integrated with third-party MVC frameworks, such as struts.
Simplifies various technology integrations: provides integration into Java Mail, task scheduling, JMX, JMS, JNDI, EJB, dynamic language, remote access, WEB service, and more.
Spring can help us simplify application development, help us to create and assemble objects, manage transactions for us, simple MVC framework, can think of spring as a super bonding platform, can integrate many technologies together to form a whole, so that the system structure better, better performance, Thus accelerating the development of our program, like the advantages, we have no reason not to consider using it.

1.1.4 How to learn spring well
To learn spring, it is essential to know what spring is, what it can do for us, what to do with it, and then make a simple example of how to use spring. Spring Core is an IOC container, so be sure to understand what the IOC container is, and how to configure and use the container, all of the other technologies are based on the container, and after understanding the IOC, the next is the aspect-oriented programming, first of all the concept, the basic configuration, and finally the realization principle, The next is the database transaction management, in fact, the spring management transaction is implemented by aspect-oriented programming, so the foundation is very important, the IOC container and face-oriented programming, the rest is based on the implementation of these two things, it is easier to learn. To learn spring can not be urgent, we must lay the foundation firmly, the foundation is solid, this is ax.


1.2 Spring Basics

1.2.1 Spring Frame composition
Waiting to be mended


Core container: Includes core, Beans, Context, El module.

Core module: Encapsulates the bottom-most part of a framework dependency, including resource access, type conversions, and some common tool classes.
Beans module: Provides a basic part of the framework, including inversion control and dependency injection. The Bean Factory is the container core, the essence is the implementation of "Factory design pattern", and no need to implement "single case design mode", single case is completely controlled by the container, and advocate interface-oriented programming, rather than implementation-oriented programming; All application objects and inter-object relationships are managed by the framework, This actually extracts the dependencies between the maintenance objects from the logic of the program, and all these dependencies are maintained by Beanfactory.
Context module: Based on core and beans, integrating beans module functions and adding resource bindings, data validation, internationalization, Java EE support, container life cycle, event propagation, etc., the core interface is ApplicationContext.
El Module: Provides powerful expression language support, supports access and modification of property values, method invocation, supports access and modification of arrays, containers and indexers, names variables, supports arithmetic and logical operations, supports fetching beans from the spring container, and supports list projection, selection, and general list aggregation.

AOP, aspects modules:
AOP module: The Spring AOP module provides an aspect-oriented programming (aspect-oriented programming) implementation that conforms to the AOP Alliance specification, providing technologies such as logging, privilege control, performance statistics, and separation of business logic and the ability to dynamically add these functions to the required code, so that the professional level, reduce the business logic and common functions of the coupling.
Aspects module: Provides the integration of ASPECTJ, 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 object can get the benefits of spring management transactions, do not need to do transaction control in code, and support programmatic and declarative management of things.
JDBC Module: Provides a JBDC sample template that uses these templates to eliminate traditional lengthy JDBC coding and the necessary transaction control, and to enjoy the benefits of spring management transactions.
ORM Module: Provides seamless integration with the popular "object-relational" mapping framework, including Hibernate, JPA, Ibatiss, and more. And you can use Spring Transaction management without additional control of transactions.
OXM module: Provides a object/xml mapping implementation, mapping Java objects to XML data, or mapping XML data to Java objects, Object/xml mapping implementations include JAXB, Castor, XMLBeans, and XStream.
JMS module: For JMS (Java Messaging Service), a set of "message producers, message consumers" templates are used to make it easier to use JMS,JMS to send messages between two applications, or distributed systems, for asynchronous communication.
Web/remoting module: The Web/remoting module contains the Web, Web-servlet, web-struts, web-porlet modules.
Web module: Provides the underlying web functionality. Examples include multi-file uploads, integrated IOC containers, remote process access (RMI, Hessian, Burlap), and Web service support, and a resttemplate class to provide convenient restful services access.
Web-servlet module: Provides a spring MVC WEB framework implementation. 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 seamlessly collaborate with other spring technologies.
Web-struts module: Provides seamless integration with struts, both struts1.x and struts2.x support
The test module: Spring supports the JUnit and testng test frameworks, and provides additional spring-based testing capabilities, such as simulating the functionality of an HTTP request when testing a Web framework.


1.2.2 Typical application scenarios
Spring can be applied to many scenarios, from the simplest standard Java SE programs to enterprise-class applications that can be built using spring.
In the Web application scenario, the typical three-tier architecture: The data Model layer implements the domain object, the data access layer implements the data access, the logic layer implements the business logic, the Web layer provides the page presentation, all these layer components are managed by spring, and enjoy the benefits of spring transaction management, AOP, etc. And the only entrance to the request is Dispachterservlet, which implements the request function by mapping the request to the corresponding web-tier component.

What is Spring?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.