Spring (1)

xiaoxiao2021-03-05  23

Looked about the introduction of rod spring articles, post-translated connections:

http://blog.9cbs.net/taoen2002/Archive/2004/05/07/15875.aspx

First, Spring Introduction

Spring features:

1. Spring is different from other Framework, which provides a way to manage your business object.

2, Spring has a hierarchical architecture, meaning that you can choose any of its independent parts, while others still use your related implementation.

3, its design is beginning to help you write easy-to-test code, Spring is an ideal framework for engineering using test-driven development (TDD).

4, Spring will not add to your project to other framework dependencies; simultaneous Spring can also be called a package of solutions, providing a typical application to most infrastructure required.

The benefits of Spring:

1, Spring can effectively organize your intermediate objects; (*)

2, Spring can eliminate excessive use of Singleton in many projects;

3. Treat a configuration file with a different application and project, eliminating the needs of the properties files in various custom formats, just need to see the JavaBean property of the class. The use of Inversion of Control has completed this simplification; (using type2 IOC)

4, it is easy to develop your interface instead of facilitating program habits. (This is also ...)

5, Spring's design will allow the app to use it as little as possible to depend on its API, most of the business objects in the Spring application are not dependent on Spring.

6. Applications built using Spring are easy for unit testing;

7. Spring makes EJB a real selection, not the required choice. You can use Pojos or Local EJBS to implement business interfaces, but will not affect call code.

8. Spring provides alternatives to EJBs on web applications, such as providing declarative transaction management;

9, Spring provides a consistent framework for data access, whether using JDBC or O / R mapping products (Hibernate, JDO).

Spring task description:

The main purpose of Spring is to make J2EE easy to use and promote good programming habits.

Spring will not reinvent the wheel, which makes the existing technology more easily, such as it does not have underlying transaction coordination, but provides an abstract layer covers JTA and any other things strategy. Spring does not compete with other open source projects, but there are new scenarios in some areas, such as its web framework, lightweight IOC container and AOP framework.

Second, Inversion Of Control Container

The design of Spring is org.springframework.beans package to coordinate JavaBeans work. This package is generally not directly used by the user, but is a basic service for other functions.

The next high abstract concept is Bean Factory, a Spring Bean Factory is a general factory that can get Objects by name and manages the relationship between Objects.

Bean Factories support two object modes:

Singleton: The default, most commonly used, an ideal mode for stateless objects. Prototype: Each time you get a separate object, you can make users have their own objects.

Because org.springframework.beans.Factory.BeanFactory is a simple interface, you can easily implement your own beanfactory, but few users need to do this; the commonly used BeanFactory definition is:

XMLBeanFactory: Analysis of the XML structure of the simple and intuitive definition class and named object attribute;

ListableBeanFactoryImpl: Provides the ability to analyze bean definitions stored in the properties file;

With the concept of BeanFactory, Spring became an Inversion of Control container. The concept behind IOC is the so-called HollyWood Principle: Don't call me, i 'll call you. IoC moves control created from the object's code into the framework, using IOC containers simply pointing that components require X objects, and they will provide them to it in the runtime container. The container is to obtain the corresponding information by viewing the parameter table of the method (such as JavaBean's attribute), or by configuring the source (XML) to provide an object to the component.

Spring ApplicationContext is the sub-interface of BeanFactory, providing support for the following things:

Information lookup, support international

Event mechanism, allowing publishing applications and optional registrations have accepted events

Portable documents and resource access

Third, JDBC abstraction and data access abnormal level

JDBC provides a nice database abstraction, but there is also a painful place:

Need a lot of error handling code to ensure that RESULTSET, STATEMENT, CONNECTION are closed after use;

There is no abnormal level, using SQLException to respond to all errors, do not know where the specific fault is.

Spring solves these problems in two ways:

Transferring the lengthual error of the error from the program to the frame by the API, the framework processes all exceptions, and the program code is concentrated to perform SQL and processing results;

Provide meaningful exception level for program code to handle SQLException. When Spring gets a connection from the data source, it checks metadata to determine the database, then it uses this information to map the SQLException to its own anomalies in its anomalous hierarchy, which is your ability to deal with meaningful exceptions.

Spring provides two-layer JDBC API, the first in the org.springframework.jdbc.core package, using the callback mechanism mobile control from the program to the frame, which is a different inversion of control.

In the org.springframework.jdbc.Object package is a higher level of JDBC. This is based on the core JDBC callback function, but provides an API that can operate on the RDBMS, in which the query, update, or stored procedures are modeled with Java objects. This group of APIs have been affected by the JDO query API, which is very straightforward and useful.

Spring Data Access Exception hierarchy is based on Unchecked Exception so that developers can decide whether to capture these exceptions themselves to recover.

Spring JDBC has the benefit of you:

You don't have to write Finally like JDBC;

The code you need to write is less;

You don't have to drive the RDBMS document to solve the understanding, a strange error code returned because of the wrong column name; no matter what kind of persistence technology you use, you can easily implement the DAO mode, so that the business code does not depend on any specific data. Access the API.

Fourth, O / R mapping integration

Spring provides integration support for Hibernate 2.x and JDO, especially for Hibernate.

Why use hibernate Spring instead of using Hibernate?

1. Use the Hibernate code usually requires the same session object for efficiency and appropriate transaction; Spring uses an explicit Template package class or using a declarative, AOP method to intercept (interceptor) mode Solutions make it easier to transparently create and bind session to the current thread.

2, resource management, Spring application Context can handle the location and configuration of Hibernate SessionFactories, JDBC data sources, and other resources; make it easy to manage and change.

3, the integrated transaction management Spring allows you to pack your Hibernate code, through 2 ways: Use an explicit template package class on the code layer, or use a declarative, AOP method intercept (Interceptor).

4, Spring can pack Hibernate exception, switch them from private, Checked exceptions into a set of abnormal runtime; make you can capture and process an exception in any place you need, and JDBC exception is also converted to the same level In mementary, you can perform the same operations in the same programming model.

5, in order to avoid binding Hibernate, flexible, free, still using private API; Spring's transaction and abnormal abnormality of Hibernate, so that you easily isolate the Hibernate-Specific code from your application section, and Will not affect Hibernate's powerful features.

6. Test the simple Spring IoC makes it easy to change the location of the Hibernate Session Factories, data sources, transaction management, and mapping objects, which make it easy to isolate and test each of the persistent relationships.

Five, transaction management

JTA makes a very cumbersome API directly, many J2EE developers feel that EJB CMT makes the only reasonable choice for transaction management.

Spring provides its abstraction of transaction management, provided:

1. Programming management transactions by a callback template similar to JDBCTemplate, is more convenient than directly using JTA;

2, similar to the declarative transaction management of EJB CMT, but does not need EJB containers;

Sixth, AOP

Spring's primary goal is to provide the J2EE service to Pojos, and it has the advantage of porting between application servers, Spring AOP supports Method Interception, key concepts include:

Interception: Custom behavior can be inserted after the call to the interface and class;

Introduction: Specifies Advice to cause an additional interface to confuse the inheritance;

Spring uses a dynamic proxy to implement AOP or use CGLIB to generate a bytecode (this is the agent class). Both methods can be used in any application.

Spring enables the AOP framework for the AOP Alliance interface (speaking over the learning AOP). Spring AOP's most common application makes a declarative transaction management, which is based on the introduction of TransactionTemplate described earlier, and can provide declarative transaction management to any Pojo.

Spring's declarative transaction management is similar to EJB CMT, but different:

l Transaction management can be applied to any Pojo;

l Enable programming back in transactional POJO by using Spring's transaction API. To this end, we offer a static approach, using the Threadlocal variable, so you don't need to pass the object such as EJBCONTEXT to ensure rollback;

l You can declare the "rollback rules";

l Transaction management is not bound to JTA.

Of course, you can also use the Spring AOP to implement procedures unique aspects.

Please modify the above recovery text

TRACKBACK address: http://www.yculblog.com/trackbackback/0/121687

转载请注明原文地址:https://www.9cbs.com/read-37516.html

New Post(0)