Introducing the Spring Framework

xiaoxiao2021-03-06  107

October 2003

Discuss this article

You May Have Heard The Buzz This Summer Around The Spring Framework. In this Article, I'll Try to Explain What Spring Sets Out to Achieve, And How I Believe It Can Help You To Develop J2EE Applications.

YET ANOTHER FRAMEWORK?

YOU MAY BEINKING "Not Another Framework." WHY SHOULD You BOTHER TO Read this Article, or Download The Spring Framework

I believe That Spring is Unique, for Several Reasons:

It addresses areas that many other popular frameworks do not. Spring focuses around providing a way to manage your business objects. Spring is both comprehensive and modular. Spring has a layered architecture, meaning that you can choose to use just about any part of it in isolation, yet its architecture is internally consistent. So you get maximum value from your learning curve. you might choose to use Spring only to simplify use of JDBC, for example, or you might choose to use Spring to manage all your business objects. IT's Designed from the Ground Up to Help You Write Code That's Easy to Test. Spring is an ideal framework for test driven projects.

Spring is not necessarily one more framework dependency for your project. Spring is potentially a one-stop shop, addressing most infrastructure concerns of typical applications. It also goes places other frameworks do not.

Although it has been an open source project only since February 2003, Spring has a long heritage. The open source project started from infrastructure code published with my book, Expert One-on-One J2EE Design and Development, in late 2002. Expert One- on-One J2EE also laid out the basic architectural thinking behind Spring. However, the architectural concepts go back to early 2000, and reflect my experience in developing infrastructure for a series of successful commercial projects.Since January 2003, Spring has been hosted on SourceForge . There areow Ten Developers, Of Whom Six Are Highly Active.

Architectural Benefits of Spring

Before get down to specifics, let's look at some of the benefits spring can bring to a project:

Spring can effectively organize your middle tier objects, whether or not you choose to use EJB. Spring takes care of plumbing that would be left up to you if you use only Struts or other frameworks geared to particular J2EE APIs. Spring can eliminate the proliferation of singletons seen on many projects. in my experience, this is a major problem, reducing testability and object orientation. Spring can eliminate the need to use a variety of custom properties file formats, by handling configuration in a consistent way throughout applications and projects. Ever wondered what magic property keys or system properties a particular class looks for, and had to read the Javadoc or even source code? With Spring you simply look at the class's JavaBean properties. The use of Inversion of Control (discussed below) helps achieve this simplification Spring Can Facilitate Good Programming Practice by Reducing The Cost of Programming To Interfaces, Rather Than Classes, Almost To Zero. Spring Is Design ed so that applications built with it depend on as few of its APIs as possible. Most business objects in Spring applications have no dependency on Spring. Applications built using Spring are very easy to unit test. Spring can make the use of EJB an implementation choice , rather than the determinant of application architecture. you can choose to implement business interfaces as POJOs or local EJBs without affecting calling code. Spring helps you solve many problems without using EJB. Spring can provide an alternative to EJB that's appropriate for many web applications. For Example, Spring Can Use Aop to Deliver Declarative Transaction Management WITHOUT USING AN EJB Container

even without a JTA implementation, if you only need to work with a single database. Spring provides a consistent framework for data access, whether using JDBC or an O / R mapping product such as Hibernate.Spring really can enable you to implement the simplest possible Solution to Your Problems. And That's Worth a LOT.

What does Spring do?

Spring Provides a Lot of FunctionAlity, SO I'll Quickly Review Each Major Area in Turn.

Mission Statement

Firstly, Let's Be Clear On Spring's Scope. Alth Spring Covers a Lot of Ground, We do have clear vision as to what it will and shouldn't address.

Spring's main aim is to make j2ee easier to use and promote good programing phactice.

Spring does not reinvent the wheel. Thus you'll find no logging packages in Spring, no connection pools, no distributed transaction coordinator. All these things are provided by open source projects (such as Commons Logging, which we use for all our log output , or by your application server. for the Same Reason, We don't provider an O / R Mapping Layer. There Are Good Solutions to this Problem Such as Hibernate and JDO.

Spring does aim to make existing technologies easier to use. For example, although we are not in the business of low-level transaction coordination, we do provide an abstraction layer over JTA or any other transaction strategy.

Spring does not directly compete with other open source projects unless we feel we can provide something new. For example, like many developers, we have never been happy with Struts, and feel that there's room for improvement in MVC web frameworks. In some areas , such as its lightweight IoC container and AOP framework, Spring does have direct competition, but these are areas in which no solution has become popular. (Spring was a pioneer in these areas.) Spring also benefits from internal consistency. All the developers are singing from the same hymn sheet, the fundamental ideas remaining faithful to those of Expert One-on-One J2EE Design and Development. and we've been able to use some central concepts, such as Inversion of Control, across multiple areas.

Spring is portable between application servers. Of course ensuring portability is always a challenge, but we avoid anything platform-specific or non-standard, and support users on WebLogic, Tomcat, Resin, JBoss, WebSphere and other application servers.

Inversion of Control Container

The core of Spring's design is the org.springframework.beans package, designed for working with JavaBeans. This package typically is not used directly by users, but serves as the underpinning of much of the other functionality.

................... ..

Bean Factories Support Two Modes of Objects:

"Singleton":. In this case, there's one shared instance of the object with a particular name, which will be retrieved on lookup This is the default, and most often used, mode It's ideal for stateless service objects "Prototype"..: in this case, each retrieval will result in the creation of an independent object. For example, this could be used to allow each user to have their own object.As org.springframework.beans.factory.BeanFactory is a simple interface, it can Be imported for a Range of underlying storage method your ing, although few users Find They need to. The Most Commonly Used BeanFactory Definitions Are:

.. The XmlBeanFactory This parses a simple, intuitive XML structure defining the classes and properties of named objects We provide a DTD to make authoring easier ListableBeanFactoryImpl:. This provides the ability to parse bean definitions in properties files, and create BeanFactories programmatically.

. Each bean definition can be a POJO (defined by class name and JavaBean initialisation properties), or a FactoryBean The FactoryBean interface adds a level of indirection Typically this is used to create proxied objects using AOP or other approaches:. For example, proxies that Add Declarative Transaction Management. (This Is Similar To EJB Interception In Concept, But Works Out Much Simpler In Practice.)

BeanFactories can optionally participate in a hierarchy, "inheriting" definitions from their ancestors. This enables the sharing of common configuration across a whole application, while individual resources such as controller servlets also have their own independent set of objects.

This motivation for the use of JavaBeans is described in Chapter 4 of Expert One-on-One J2EE Design and Development, which is available on the ServerSide as a free PDF (/articles/article.tss?l=RodJohnsonInterview).Through its BeanFactory concept, Spring is an Inversion of Control container. (I do not much like the term container, as it conjures up visions of heavyweight containers such as EJB containers. A Spring BeanFactory is a container that can be created in a single line of code And Requires No Spectial Deployment Steps.)

The concept behind Inversion of Control is often expressed in the Hollywood Principle: ". Do not call me, I'll call you" IoC moves the responsibility for making things happen into the framework, and away from application code Where configuration is concerned. this means that while in traditional container architectures such as EJB, a component might call the container to say "where's object X, which I need to do my work", with IoC the container figures out that the component needs an X object, and provides IT to It at runtime. The Container Does This Figuring Out Based On Method Signatures and, Possibly, Configuration Data Such As XML.

IOC HAS Several Important Benefits. For example:

Because components do not need to look up collaborators at runtime, they're much simpler to write and maintain. In Spring's version of IoC, components express their dependency on components via other exposing JavaBean setter methods. The EJB equivalent would be a JNDI lookup , which requires the developer to write code For the same reasons, application code is much easier to test JavaBean properties are simple, core Java and easy to test:.. just write a self-contained JUnit test method that creates the object and sets the relevant properties. A good IoC implementation preserves strong typing. If you need to use a generic factory to look up collaborators, you have to cast the results to the desired type. This is not a major problem, but it is inelegant. With IoC . you express strongly typed dependencies in your code and the framework is responsible for type casts This means that type mismatches will be raised as errors when the framework configures the application; you do not have to worry a bout class cast exceptions in your code. Most business objects do not depend on IoC container APIs. This makes it easy to use legacy code, and easy to use objects either inside or outside the IoC container. For example, Spring users often configure the jakarta Commons DBCP DataSource as a Spring bean: there's no need to write any custom code to do this We say that an IoC container is not invasive:.. using it will not invade your code with dependency on its APIs Any JavaBean can become .

. This last point deserves emphasis IoC is unlike traditional container architectures, such as EJB, in this minimization of dependency of application code on container This means that your business objects can potentially be run in different IoC frameworks -. Or outside any framework - without code .

IoC is not a new concept, although it's only just made prime time in the J2EE community There are alternative IoC containers:.. Notably, Apache Avalon, PicoContainer and HiveMind Avalon has never become particularly popular, although it is powerful and has a long history . Avalon is fairly heavyweight and complex, and seems more invasive than newer IoC solutions. PicoContainer is lightweight and emphasizes the expression of dependencies through constructors rather than JavaBean properties. Unlike Spring, its design allows the definition of only one object of each type (possibly . a limitation resulting from its rejection of any metadata outside Java code) For a comparison between Spring and PicoContainer and other IoC frameworks, see the article "The Spring Framework - A Lightweight Container" on the Spring website at http: //www.springframework .org / docs / lightweight_container.html. This page incduDes a link to the picocontainer website.

Spring BeanFactories are very lightweight. Users have successfully used them inside applets, as well as standalone Swing applications. (They also work fine within an EJB container.) There are no special deployment steps and no detectable startup time. This ability to instantiate a container almost instantly in any tier of an application can be very valuable.The Spring BeanFactory concept is used throughout Spring, and is a key reason that Spring is so internally consistent. Spring is also unique among IoC containers in that it uses IoC as a basic concept THROUGHOUT A FULL-FEATURED FRAMEWORK.

Most importantly for application developers, one or more BeanFactories provide a well-defined layer of business objects. This is analogous to, but much simpler than, a layer of local session beans. Unlike EJBs, the objects in this layer can be interrelated, and .

A Spring ApplicationContext IS A Subinterface of BeanFactory, Which Provides Support for:

Message Lookup, Supporting InternationalizationAn Eventing Mechanism, Allowing Application Objects To Publish And Optionally Register To Be Notified of Events Portable File and Resource Access

XMLBeanFactory Example

Spring users normally configure their applications in XML "bean definition" files. The root of a Spring XML bean definition document is a element. The element contains one or more definitions. We normally specify the class and ....................

Let's look at a simple example, which configures three application objects with relationships commonly seen in J2EE applications: A J2EE DataSource A DAO that uses the DataSource A business object that uses the DAO in the course of its work

In the following example, we use a BasicDataSource from the Jakarta Commons DBCP project. This class (like many other existing classes) can easily be used in a Spring bean factory, as it offers JavaBean-style configuration. The close method that needs to be Called On Shutdown Can Be Registered Via Spring's "Destroy-Method" Attribute, To Avoid The Need for BasicDataSource TO IMPLEMENT Any Spring Interface.

Class = "org.apache.commons.dbcp.basicdataasource"

Destroy-method = "close">

com.mysql.jdbc.driver

jdbc: mysql: // localhost: 3306 / mydb

root

All the properties of BasicDataSource we're interested in are Strings, so we specify their values ​​with the element. Spring uses the standard JavaBean PropertyEditor mechanism to convert String representations to other types if necessary.

Now We define the Dao, Which Has A Bean Reference to the Datasource. RELATIONSHIPS BETWEEN Beans Are Specified Using The ELEMENT:

Class = "example.exampledataaccessObject">

The Business Object Has A Reference To The Dao, And An Int Property (ExampleParam):

Class = "Example.examplebusinessObject">

10

Relationships between objects are normally set explicitly in configuration, as in this example. We consider this to be a Good Thing. However, Spring also provides what we call "autowire" support, a la PicoContainer, where it figures out the dependencies between beans. The limitation with this - as with PicoContainer -. is that if there are multiple beans of a particular type it's impossible to work out which instance a dependency of that type should be resolved to On the positive side, unsatisfied dependencies can be caught when the factory IS initialized. (Spring Also Offers An Optional Dependency Check for Explicit Configuration, Which Can Achieve this Goal.)

We Could Use the Autowire Feature As Follows in The Above Example, IF We Didn't Want To Code these Relationships Explicitly:

Class = "eXample.examplebusinessObject"

AutoWire = "ByType">

10

With this usage, Spring will work out that the dataSource property of exampleBusinessObject should be set to the implementation of DataSource it finds in the present BeanFactory. It's an error if there is none, or more than one, bean of the required type in the present BeanFactory. We still need to set the exampleParam property, as it's not a reference.Autowire support and dependency checking is presently in CVS and will be available in Spring 1.0 M2 (due October 20, 2003). All other features discussed in this article are In The Current 1.0 M1 Release.

Externalizing relationships from Java code has an enormous benefit over hard coding it, as it's possible to change the XML file without changing a line of Java code. For example, we could simply change the myDataSource bean definition to refer to a different bean class to use An Alternative Connection Pool, OR A Test Data Source. We Could Use Spring's JNDI Location FactoryBean To Get A DataSource from an Application Server IN A Single Alternative XML Stanza.

Now let's look at the Java code for the example business object Note that there are no Spring dependencies in the code listing below Unlike an EJB container, a Spring BeanFactory is not invasive:.. You do not normally need to code awareness of it into Application Objects.

Public Class ExamplebusinessObject IMplements mybusinessObject {

Private ExampleDataAccessObject DAO;

Private int exampleparam;

Public void setDataAccessObject (ExampleDataAccessObject Dao) {

THIS.DAO = DAO;

}

Public void setxampleparam (int exampleparam) {

THIS.ExampleParam = ExampleParam;

}

Public void mybusinessMethod () {

// Do Stuff for Dao

}

}

Note the property setters, which correspond to the XML references in the bean definition document These are invoked by Spring before the object is used.Such application beans do not need to depend on Spring:. They do not need to implement any Spring interfaces or extend Spring classes: they just need to observe JavaBeans naming convention Reusing one outside of a Spring application context is easy, for example in a test environment Just instantiate it with its default constructor, and set its properties manually, via setDataSource () and.. setExampleParam () calls. So long as you have a no-args constructor, you're free to define other constructors taking multiple properties if you want to support programmatic construction in a single line of code.

Note that the JavaBean properties are not declared on the business interface callers will work with. They're an implementation detail. We could "plug in" different implementing classes that have different bean properties without affecting connected objects or calling code.

Of course Spring XML bean factories have many more capabilities than described here, but this should give you a feel for the basic approach. As well as simple properties, and properties for which you have a JavaBeans PropertyEditor, Spring can automatically handle lists, maps and Java.util.properties.

Bean factoryies and application contexts are usually associated with a scope defined by the j2ee server, such as:

The Servlet context. In the Spring MVC framework, an application context is defined for each web application containing common objects. Spring provides the ability to instantiate such a context through a listener or servlet without dependence on the Spring MVC framework, so it can also be used in Struts, WebWork or other web frameworks A Servlet:.. Each controller servlet in the Spring MVC framework has its own application context, derived from the root (application-wide) application context It's also easy to accomplish this with Struts or another MVC framework EJB:. Spring provides convenience superclasses for EJB that simplify EJB authoring and provide a BeanFactory loaded from an XML document in the EJB Jar file.These hooks provided by the J2EE specification generally avoid the need to use a Singleton to bootstrap a bean factory.

. However, it's trivial to instantiate a BeanFactory programmatically if we wish For example, we could create the bean factory and get a reference to the business object defined above in the following three lines of code:

InputStream is = getClass (). GetResourceAsStream ("myfile.xml");

XMLBeanFactory BF = New XMLBeanFactory (IS);

MybusinessObject Mbo = (MyBusinessObject) bf.getbean ("ExamplebusinessObject");

This Code Will Work Outside An Application Server: IT Doesn't Even Depend On J2EE, As The Spring IoC Container IS Pure Java.

JDBC Abstract and Data Access Exception Hierarchy

Data Access Is Another Area in Which Spring SHINES.

JDBC Offers Fairly Good Abstract from The Underlying Database, But IS A Painful API To Use. Some of The Problems Include:

The need for verbose error handling to ensure that ResultSets, Statements and (most importantly) Connections are closed after use. This means that correct use of JDBC can quickly result in a lot of code. It's also a common source of errors. Connection leaks can quickly bring applications down under load The relatively uninformative SQLException JDBC does not offer an exception hierarchy, but throws SQLException in response to all errors Finding out what actually went wrong -... for example, was the problem a deadlock or invalid SQL -? involves Examining The SqlState and Error Code. The meaning of these values ​​Varies Between Databases.Spring Addresses these Problems in Two Ways:

By providing APIs that move tedious and error-prone exception handling out of application code into the framework The framework takes care of all exception handling;.. Application code can concentrate on issuing the appropriate SQL and extracting results By providing a meaningful exception hierarchy for your application code to work with in place of SQLException. When Spring first obtains a connection from a DataSource it examines the metadata to ascertain the database. It uses this knowledge to map SQLExceptions to the correct exception in its own hierarchy descended from org.springframework.dao .DataAccessException. Thus your code can work with meaningful exceptions, and need not worry about proprietary SQLState or error codes. Spring's data access exceptions are not JDBC-specific, so your DAOs are not necessarily tied to JDBC because of the exceptions they may throw.

Spring provides two levels of JDBC API The first, in the org.springframework.jdbc.core package, uses callbacks to move control -. And hence error handling and connection acquisition and release -. From application code inside the framework This is a different type of Inversion of Control, but equally valuable to that used for configuration management.Spring uses a similar callback approach to address several other APIs that involve special steps to acquire and cleanup resources, such as JDO (acquiring and relinquishing a PersistenceManager), transaction management ( Using JTA) and JNDI. Spring Classes That Perform Such Callbacks Are Called Templates.

For Example, The Spring JDBCTemplate Object Can Be Used to Perform A SQL Query and Save The Results in A List as Follows:

JDBCTemplate Template = New JDBCTEMPLATE (Datasource);

Final List names = new linkedlist ();

Template.query ("Select User.name from User",

New rowcallbackhandler () {

Public Void ProcessRow (ResultSet RS) throws sqlexception {

Names.Add (rs.getstring (1));

}

});

Note that application code within the callback is free to throw SQLException: Spring will catch any exceptions and rethrow them in its own hierarchy The application developer can choose which exceptions, if any, to catch and handle..

The JdbcTemplate provides many methods to support different scenarios including prepared statements and batch updates. The Spring JDBC abstraction has a very low performance overhead beyond standard JDBC, even in applications cases involving huge result sets.

The higher level JDBC abstraction is in the org.springframework.jdbc.object package This is built on the core JDBC callback functionality, but provides an API in which an RDBMS operation -. Whether query, update or stored procedure - is modelled as a Java Object. This API WAS Partly Inspired by the JDO Query API, Which i Found Intuitive and Highly Usable.a Query Object To Return User Objects Might Look Like THIS:

Class UserQuery Extends MappingsqlQuery {

Public UserQuery (DataSource Datasource) {

Super (DataSource, "SELECT * FROM PUB_USER_ADDRESS WHERE User_ID =?");

DeclareParameter (New SqlParameter (Types.Numeric);

COMPILE ();

}

// map a result set row to a java object

Protected Object MapRow (ResultSet RS, INT Rownum) throws sqlexception {

User User = new user ();

User.setId (rs.getlong ("user_id");

User.setForeName (RS.GetString ("forename");

Return User;

}

Public user finduser (long id) {

// use superclass convenience method method to provide strong type

Return (User) FindObject (ID);

}

}

This Class Can Be Used As Follows:

User User = UserQuery.Finduser (25);

Such Objects Are Off, Unless The Subclass Does Something Unusual.

Another important class in the org.springframework.jdbc.object package is the StoredProcedure class. Spring enables a stored procedure to be proxied by a Java class with a single business method. If you like, you can define an interface that the stored procedure implements .

The Spring data access exception hierarchy is based on unchecked (runtime) exceptions. Having worked with Spring on several projects I'm more and more convinced that this was the right decision.Data access exceptions not usually recoverable. For example, if we can ' t connect to the database, a particular business object is unlikely to be able to work around the problem. One potential exception is optimistic locking violations, but not all applications use optimistic locking. It's usually bad to be forced to write code to catch fatal exceptions that can not be sensibly handled. Letting them propagate to top-level handlers like the servlet or EJB container is usually more appropriate. All Spring data access exceptions are subclasses of DataAccessException, so if we do choose to catch all Spring data access exceptions, We can Easily do so.

Note that if we do want to recover from an unchecked data access exception, we can still do so. We can write code to handle only the recoverable condition. For example, if we consider that only an optimistic locking violation is recoverable, we can write Code in a Spring Dao As Follows:

Try {

// Do Work

}

Catch (OptimisticLockingFailureException ex) {

// I'm interested in this

}

IF Spring Data Access Exceptions Were Checked, We'd. Note That We Could Choose to Write this Anyway:

Try {

// Do Work

}

Catch (OptimisticLockingFailureException ex) {

// I'm interested in this

}

Catch (DataAccessException EX) {

// Fatal; Just Rethrow IT

}

One potential objection to the first example - that the compiler can not enforce handling the potentially recoverable exception - applies also to the second Because we're forced to catch the base exception (DataAccessException), the compiler will not enforce a check for. . a subclass (OptimisticLockingFailureException) So the compiler would force us to write code to handle an unrecoverable problem, but provide no help in forcing us to deal with the recoverable problem.Spring's use of unchecked data access exceptions is consistent with that of many - probably most -. successful persistence frameworks (Indeed, it was partly inspired by JDO.) JDBC is one of the few data access APIs to use checked exceptions TopLink and JDO, for example, use unchecked exceptions exclusively Gavin King now believes that Hibernate should.. Also Have Opted for Unchecked Exceptions.

Spring JDBC CAN Help You in Several Ways:

You'll never need to write a finally block again to use JDBC You'll need to write much less code overall You'll never need to dig through your RDBMS documentation to work out what obscure error code it returns for a bad column name. Your application will not be dependent on RDBMS-specific error handling code. Whatever persistence technology use, you'll find it easy to implement the DAO pattern without business logic depending on any particular data access API.

In practice we find that all this amounts to substantial productivity gains and fewer bugs I used to loathe writing JDBC code;. Now I find that I can focus on the SQL I want to execute, rather than the incidentals of JDBC resource management.

Spring's JDBC Abstract CAN Be Used Standalone If Desired - You Are Not Forced to Use The Other Parts of Spring.o / R Mapping Integration

Of course often you want to use O / R mapping, rather than use relational data access. Your overall application framework must support this also. Thus Spring integrates out of the box with Hibernate 2.x and JDO. Its data access architecture allows it to INTEGRATE WITH UN Underlying Data Access Technology. Spring and hibernate Integrate Particularly Well.

Why Would You Use Hibernate Plus Spring, INSTEAD OF HIBERNATE DIRECTLY? Spring Adds Significant Value In The Following Areas:

Session management. Spring offers efficient, easy, and safe handling of Hibernate Sessions. Related code using Hibernate generally needs to use the same Hibernate "Session" object for efficiency and proper transaction handling. Spring makes it easy to transparently create and bind a session to the current thread, using either a declarative, AOP method interceptor approach, or by using an explicit, "template" wrapper class at the Java code level. Thus Spring solves many of the usage issues that repeatedly arise on the Hibernate forums. Resource management. Spring application contexts can handle the location and configuration of Hibernate SessionFactories, JDBC datasources, and other related resources. This makes these values ​​easy to manage and change. Integrated transaction management. Spring allows you to wrap your Hibernate code with either a declarative, AOP style Method interceptor, or an expedition 'template'

wrapper class at the Java code level. In either case, transaction semantics are handled for you, and proper transaction handling (rollback, etc.) in case of exceptions is taken care of. As discussed below, you also get the benefit of being able to use and swap various transaction managers, without your Hibernate related code being affected. As an added benefit, JDBC-related code can fully integrate transactionally with Hibernate code. This is useful for handling functionality not implemented in Hibernate. Exception wrapping, as described above . Spring can wrap Hibernate exceptions, converting them from proprietary, checked exceptions, to a set of abstracted runtime exceptions. This allows you to handle most persistence exceptions, which are non-recoverable, only in the appropriate layers, without annoying boilerplate catches / throws And Exception Declarations. You can Still Trap and Handle Excetions Anywhere You Need To. Remember That JDBC Exceptions (Including DB Specific Dialects) A re also converted to the same hierarchy, meaning that you can perform some operations with JDBC within a consistent programming model. To avoid vendor lock-in. While Hibernate is powerful, flexible, open source and free, it still uses a proprietary API. Given the choice, it's usually desirable to implement major application functionality using standard or abstracted APIs, in case you need to switch to another implementation for reasons of functionality, performance, or any other concerns. Spring '

s abstraction of Hibernate Transactions and Exceptions, along with its IoC approach which allow you to easily swap in mapper / DAO objects implementing data-access functionality, make it easy to isolate all Hibernate-specific code in one area of ​​your application, without sacrificing any of the power of Hibernate. Ease of testing. Spring's inversion of control approach makes it easy to swap the implementations and locations of Hibernate session factories, datasources, transaction managers, and mapper object implementations (if needed). This makes it much easier to isolate And Test Each Piece of Persistence-Related Code in ISOLATION.TRANSACTION Management

Abstracting a data access API is not enough; we also need to consider transaction management JTA is the obvious solution, but it's a cumbersome API to use directly, and as a result many J2EE developers feel that EJB CMT is the only rational option for transaction. .

Spring Provides ITS OWN Abstract for Transaction Management. Spring Uses this to Deliver:

Programmatic transaction management via a callback template analogous to the JdbcTemplate, which is much easier to use than straight JTA Declarative transaction management analogous to EJB CMT, but without the need for an EJB container

Spring's transaction abstraction is unique in that it's not tied to JTA or any other transaction management technology. Spring uses the concept of a transaction strategy that decouples application code from the underlying transaction infrastructure (such as JDBC).

Why should you care about this? Is not JTA the best answer for all transaction management? If you're writing an application that uses only a single database, you do not need the complexity of JTA. You're not interested in XA transactions or two phase commit. you may not even need a high-end application server that provides these things. But, on the other hand, you do not want to have to rewrite your code should you ever have to work with multiple data sources .Imagine you decide to avoid the overhead of JTA by using JDBC or Hibernate transactions directly. If you ever need to work with multiple data sources, you'll have to rip out all that transaction management code and replace it with JTA transactions. This isn 't very attractive and led most writers on J2EE, including myself, to recommend using global JTA transactions exclusively. Using the Spring transaction abstraction, however, you only have to reconfigure Spring to use a JTA, rather than JDBC or Hibernate, transaction strategy and YO U'Re Done. This is a configuration change, not a code change. thus, spring enables you to write application. can scale down as well.

AOP

.

The first goal of Spring's AOP support is to provide J2EE services to POJOs. This is similar to the aims of JBoss 4. However, Spring AOP has the advantage that it's portable between application servers, so there's no risk of vendor lock in. It works in either web or EJB container, and has been used successfully in WebLogic, Tomcat, JBoss, Resin, Jetty, Orion and many other application servers and web container.Spring AOP supports method interception Key AOP concepts supported include.:

Interception: Custom behaviour can be inserted before or after method invocations against any interface or class This is similar to "around advice" in AspectJ terminology Introduction:.. Specifying that an advice should cause an object to implement additional interfaces This can amount to mixin. inheritance Static and dynamic pointcuts:... Specifying the points in program execution at which interception should take place Static pointcuts concern method signatures; dynamic pointcuts may also consider method arguments at the point where they are evaluated pointcuts are defined separately from interceptors, enabling a Standard Interceptor to Be Applied In Different Applications and Code Contexts.

Spring Supports Both Stateful (One Instance Per Advised Object) and stateless interceptors (One Instance for All Advice).

Spring does not support field interception. This is a deliberate design decision. I have always felt that field interception violates encapsulation. I prefer to think of AOP as complementing, rather than conflicting with, OOP. I would not be surprised if in five or ten years we have travelled a lot farther on the AOP learning curve and feel comfortable giving AOP a seat at the top table of application design. (However at that point language-based solutions such as AspectJ may be far more attractive than they are today. Spring Implements AOP Using Dynamic PROXIES (where an interface exists) or cglib Byte Code Generation At Runtime (Which Enables Proxying of classes). Both these approaches work in Any Application Server.

Spring is the first AOP framework to implement the AOP Alliance interfaces (www.sourceforge.net/projects/aopalliance). These represent an attempt to define interfaces allowing interoperability of interceptors between AOP frameworks.

". True AOP" There's an ongoing and not very enlightening debate on TheServerSide and elsewhere as to whether this kind of interception is I do not particularly care what it's called; merely whether it can be useful in practice I'd be equally happy. to call it "declarative middleware." Think of Spring AOP as a simple, lightweight alternative to local stateless session beans, which does away with the need for a monolithic EJB container, and which enables you to build your own container with just those services you need. We do not recommend advising any and every POJO, so the analogy with local SLSBs helps to clarify the recommended granularity. (However, unlike with EJB, you're free to apply Spring AOP to finer-grained objects in rarer scenarios where it's appropriate.) Because Spring advises objects at instance, rather than class loader, level, it is possible to use multiple instances of the same class with different advice, or use unadvised instances along with advised instances.

Perhaps the commonest use of Spring AOP is for declarative transaction management. This builds on the TransactionTemplate abstraction described above, and can deliver declarative transaction management on any POJO. Depending on the transaction strategy, the underlying mechanism can be JTA, JDBC, Hibernate or any Other API Offering Transaction Management.

Spring's Declarative Transaction Management Is Similar To EJB CMT with the FOLLOWING DIFFERENCES:

Transaction management can be applied to any POJO. We recommend that business objects implement interfaces, but this is a matter of good programming practice, and is not enforced by the framework. Programmatic rollback can be achieved within a transactional POJO through using the Spring transaction API . We provide static methods for this, using ThreadLocal variables, so you do not need to propagate a context object such as an EJBContext to ensure rollback. you can define "rollback rules" declaratively. Whereas EJB will not automatically roll back a transaction on an uncaught application exception (only on unchecked exceptions and other Throwables), application developers often want a transaction to roll back on any exception. Spring transaction management allows you to specify declaratively which exceptions and subclasses should cause automatic rollback. Default behaviour is as with EJB , But you can specify Automatic Rollback on Checked, As Well as unchecked Exceptions. this Has the i mportant benefit of minimizing the need for programmatic rollback, which creates a dependence on the Spring transaction API (as EJB programmatic rollback does on the EJBContext). Transaction management is not tied to JTA. As explained above, Spring transaction management can work with different transaction strategies.Of course it's also possible to use Spring AOP to implement application-specific aspects. Whether or not you choose to do this depends on your level of comfort with AOP concepts, rather than Spring's capabilities, but it can be very useful. Successful examples We've Seen Include:

Custom security interception, where the complexity of security checks required is beyond the capability of the standard J2EE security infrastructure Debugging and profiling aspects for use during development Interceptors that send emails to alert administrators or users of unusual scenariosApplication-specific aspects can be a powerful way of Removing the need for boilerplate code code across.

Spring AOP integrates transparently with the Spring BeanFactory concept. Code obtaining an object from a Spring BeanFactory does not need to know whether or not it is advised. As with any object, the contract will be defined by the interfaces the object implements.

The Following XML Stanza Illustrates How To Define An AOP Proxy:

Class = "Org.SpringFramework.Aop.Framework.ProxyFactoryBean">

Org.springframework.beans.itestbean

txinterceptor

Target

Note that the class of the bean definition is always the AOP framework's ProxyFactoryBean, although the type of the bean as used in references or returned by the BeanFactory getBean () method will depend on the proxy interfaces. (Multiple proxy methods are supported.) The "interceptorNames" property of the ProxyFactoryBean takes a list of String. (bean names must be used rather than bean references, as new instances of stateful interceptors may need to be created if the proxy is a "prototype", rather than a singleton bean definition .) The names in this list can be interceptors or pointcuts (interceptors and information about when they should apply). The "target" value in the list above automatically creates an "invoker interceptor" wrapping the target object. It is the name of a Bean in the factory..................................... ESE References Will Be Set by Spring Ioc.it's Also Possible To Construct Aop Proxies ProgramMAAMATILY WITHOUT USING A BeanFactory, Although this is more rarely use:

TestBean Target = New TestBean ();

Debuginterceptor di = new debuginterceptor ();

Myinterceptor mi = new myinterceptor ();

ProxyFactory Factory = New ProxyFactory (Target);

Factory.AddInterceptor (0, di);

Factory.AddInterceptor (1, mi);

// an "invoker interceptor" is automatically added to wrap the target

ITESTBEAN TB = (ITESTBEAN) Factory.getProxy ();

We believe That it's generally best to externalize the Wiring of Applications from Java Code, and AOP is no exception.

Spring's most direct competitor in terms of its AOP capabilities is Jon Tirsen's Nanning Aspects (http://nanning.codehaus.org) .I feel that the use of AOP as an alternative to EJB for delivering enterprise services is growing in importance. This will BE An Important Focus of Spring, Moving Forward.

MVC Web Framework

Spring Includes a Powerful and Highly Configurable MVC Web Framework.

Spring's MVC model is most similar to that of Struts. A Spring Controller is similar to a Struts Action in that it is a multithreaded service object, with a single instance executing on behalf of all clients. However, we believe that Spring MVC has some significant Advantages over Struts. for example:

Spring provides a very clean division between controllers, JavaBean models, and views. Spring's MVC is very flexible. Unlike Struts, which forces your Action and Form objects into concrete inheritance (thus taking away your single shot at concrete inheritance in Java), Spring MVC is entirely based on interfaces. Furthermore, just about every part of the Spring MVC framework is configurable via plugging in your own interface. of course we also provide convenience classes as an implementation option. Spring MVC is truly view-agnostic. You do not get pushed to use JSP if you do not want to; you can use Velocity, XLST or other view technologies If you want to use a custom view mechanism - for example, your own templating language - you can easily implement the Spring View interface. to integrate it. Spring Controllers are configured via IoC like any other objects. This makes them easy to test, and beautifully integrated with other objects managed by Spring. The web tier becomes a thin lay . Er on top of a business object layer This encourages good practice Struts and other dedicated web frameworks leave you on your own in implementing your business objects;. Spring provides an integrated framework for all tiers of your application.As in Struts 1.1, you can Have As Many Dispatcher Servlets As You NEED IN A Spring MVC Application.

The Following Example Shows How A Simple Spring Controller CAN Access Business Objects Defined in The Same Application Context. This Controller Performs A Google Search In Its HandleRequest () Method:

Public Class GooglesearchController

IMPLEMENTS controller {

Private IgogleSearchport Google;

PRIVATE STRING GoogleKey;

Public void setgoogle (IgooglesearchPort Google) {

THIS.GOOGLE = Google;}

Public void setgooglekey (string googlekey) {

THIS.GOOGLEKEY = GoogleKey;

}

Public ModlandView HandleRequest

HTTPSERVLETREQUEST REQUEST, HTTPSERVLETRESPONSE RESPONSE

Throws servletexception, ioException {

String query = Request.getParameter ("query");

Googlesearch Result RESULT =

// Google Property Definitions Omitted ...

// Use Google Business Object

Google.Dogooglesearch (this.googleKey, Query,

Start, MaxResults, Filter, Restrict,

Safesearch, LR, IE, OE;

Return New ModelandView ("GoogleResults", "Result", Result

}

}

In the prototype this code is taken from, IGoogleSearchPort is a GLUE web services proxy, returned by a Spring FactoryBean. However, Spring IoC isolates this controller from the underlying web services library. The interface could equally be implemented by a plain Java object, test Stub, Mock Object, OR EJB Proxy, AS Discussed Below. This Controller Contains No Resource Lookup; Nothing Except Code Necessary To Support ITS Web Intertion.

Spring Also Provides Support for Data Binding, Forms, Wizards and More Complex Workflow.

A good introduction to the Spring MVC framework is Thomas Risberg's Spring MVC tutorial (http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step.html). See also "Web MVC with the Spring Framework" (http://www.springframework.org/docs/web_mvc.html).

If you're happy with your favourite MVC framework, Spring's layered infrastructure allows you to use the rest of Spring without our MVC layer. We have Spring users who use Spring for middle tier management and data access but use Struts, WebWork or Tapestry in the Web tier.

Implementing EJBS

If you choose to use EJB, Spring can provide important benefits in both EJB implementation and client-side access to EJBs.It's now widely regarded as a best practice to refactor business logic into POJOs behind EJB facades. (Among other things, this makes it much easier to unit test business logic, as EJBs depend heavily on the container and are hard to test in isolation.) Spring provides convenient superclasses for session beans and message driven beans that make this very easy, by automatically loading a BeanFactory based on an XML Document Included in The EJB JAR FILE.

This Means That A Stateless Session EJB Might Obtain and Use a Collaborator Like this:

Import org.springframework.ejb.support.AbstractStateLessSessionBean;

Public Class Myejb Extends AbstractStateLessSessionBean

Implements mybusinessinterface {

Private mypojo mypojo;

protected void onejbcreate () {

THIS.MYPOJO = GetBeanFactory (). getBean ("MyPojo");

}

Public void mybusinessMethod () {

this.mypojo.invokemethod ();

}

}

Assuming That Mypojo Is An Interface, The Implementing Class - And Any Configuration IT Requires, Such As Primitive Properties and Further Collaboction. - Is Hidden In The XML Bean Factory Definition.

WE Tell Spring Where to Load The XML Document Via An Environment Variable Definition Named EJB / BeanFactoryPath In The Standard EJB-JAR.XML Deployment Descriptor, As Follows:

mycomponent

com.test.ejb.myejbbenelocalhome

com.mycom.mycomponentlocal

com.mycom.mycomponentejb

stateless

Container

EJB / BeanFactoryPath

java.lang.String

/mycomponent-ejb-beans.xml

The myComponent-ejb-beans.xml file will be loaded from the classpath:. In this case, in the root of the EJB Jar file Each EJB can specify its own XML document, so this mechanism can be used multiple times per EJB Jar file .

...................................

Using EJBS

Spring also makes it much easier to use, as well as implement EJBs Many EJB applications use the Service Locator and Business Delegate patterns These are better than spraying JNDI lookups throughout client code, but their usual implementations have significant disadvantages For example...:

Typically code using EJBs depends on Service Locator or Business Delegate singletons, making it hard to test. In the case of the Service Locator pattern used without a Business Delegate, application code still ends up having to invoke the create () method on an EJB home , and deal with the resulting exceptions. Thus it remains tied to the EJB API and the complexity of the EJB programming model. Implementing the Business Delegate pattern typically results in significant code duplication, where we have to write numerous methods that simply call the same method On the ejb.

For these and other reasons, traditional EJB access, as demonstrated in applications such as the Sun Adventure Builder and OTN J2EE Virtual Shopping Mall, can reduce productivity and result in significant complexity.Spring steps beyond this by introducing codeless business delegates. With Spring you ' LL NEVER NEED TO WRITE ANOTHER Service Locator, Another JNDI Lookup, or Duplicate Methods in a Hand-Code Business Delegate Unless,.

For example, imagine that we have a web controller that uses a local EJB. We'll follow best practice and use the EJB Business Methods Interface pattern, so that the EJB's local interface extends a non EJB-specific business methods interface. (One of the main reasons to do this is to ensure that synchronization between method signatures in local interface and bean implementation class is automatic.) Let's call this business methods interface MyComponent. Of course we'll also need to implement the local home interface and provide a bean Implementation Class That Implements SessionBean and The MyComponent Business Methods Interface.

With Spring EJB access, the only Java coding we'll need to do to hook up our web tier controller to the EJB implementation is to expose a setter method of type MyComponent on our controller. This will save the reference as an instance variable like this :

Private mycomponent mycomponent;

Public void setmycomponent (mycomponent mycomponent) {

THIS.MYComponent = mycomponent;

}

We can subssequently use this instance variable in any business method.

Spring does the rest of the work automatically, via XML bean definition entries like this. LocalStatelessSessionProxyFactoryBean is a generic factory bean that can be used for any EJB. The object it creates can be cast by Spring to the MyComponent type automatically.

Class = "org.springframework.ejb.access.localStatelessSession ProxyFactoryBean">

mycomponent

com.mycom.mycomponent

Class = "com.mycom.myController"

>

There's a lot of magic happening behind the scenes, courtesy of the Spring AOP framework, although you are not forced to work with AOP concepts to enjoy the results. The "myComponent" bean definition creates a proxy for the EJB, which implements the business method interface. The EJB local home is cached on startup, so there's only a single JNDI lookup. Each time the EJB is invoked, the proxy invokes the create () method on the local EJB and invokes the corresponding business method on the EJB.

The MyController Bean Definition Sets The MyController Property of the Controller Class To this proxy.

This EJB Access Mechanism Delivers HUGE SIMPLIFICATION OF Application Code:

The web tier code has no dependence on the use of EJB. If we want to replace this EJB reference with a POJO or a mock object or other test stub, we could simply change the myComponent bean definition without changing a line of Java code We haven 't had to write a single line of JNDI lookup or other EJB plumbing code as part of our application.Benchmarks and experience in real applications indicate that the performance overhead of this approach (which involves reflective invocation of the target EJB) is minimal, and .

We can also apply the same approach to remote EJBs, via the similar org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean factory bean. However, we can not conceal the RemoteExceptions on the business methods interface of a remote EJB.

Testing

As you've probably gathered, I and the other Spring developers are firm believers in the importance of comprehensive unit testing. We believe that it's essential that frameworks are thoroughly unit tested, and that a prime goal of framework design should be to make applications built On the framework easy to unit test.

Spring itself has an excellent unit test suite. Our unit test coverage as of 1.0 M1 is 75% over, and we're hoping to go above 80% unit test coverage for 1.0 RC1. We've found the benefits of test first development to .

We believe that applications built on Spring are very easy to test, for the following reasons: IoC facilitates unit testing Applications do not contain plumbing code directly using J2EE services such as JNDI, which is typically hard to test Spring bean factories or contexts can be SET UP OUTSIDE A Container

The ability to set up a Spring bean factory outside a container offers interesting options for the development process. In several web application projects using Spring, work has started by defining the business interfaces and integration testing their implementation outside a web container. Only after business functionality IS Substantially Complete Is A Thin Layer Added to Provide A Web Interface.

WHO's use spring?

Although Spring is a relatively new project, we already have an impressive and growing user community. There are already many production applications using Spring. Users include a major global investment bank (in a large-scale project), well-known dotcoms, several web Development Consultancies, A Health Care Company, And Academic Institutions.

Many Users Use All Parts in Isolation. For Example, a Number of Users Begin by Using Our JDBC or Other Data Access FunctionAlity.

Roadmap

Our main priority is getting Spring to Release 1.0, Later this year. However, We Also Have Some Longer-Term Goals.

One major enhancement scheduled for 1.0 final is source-level metadata support, which will be primarily intended for (but not restricted to) use with our AOP framework. This will enable C # -style attribute-driven transaction management, and make declarative enterprise services very easy to configure for typical usage Attribute support will be included in the Spring 1.0 final release, and will be designed to integrate with JSR-175, when it is released.Beyond 1.0, some likely areas of enhancement include.:

JMS Support Via a Comparable Abstract To Our JDBC AND Transaction Support Support for Dynamic Reconfiguration of Bean Factories The Ability To Expose Web Services IDE AND Other Tool Support

As an Agile Project, We are primarily driven by user requests. So we don't develop features That no one has a use for, and we listen carefully to our user community.

Summary

Spring is a Powerful Framework That Solves Many Common Problems in j2ee.

Spring provides a consistent way of managing business objects and encourages good practices such as programming to interfaces, rather than classes. The architectural basis of Spring is an Inversion of Control container based around the use of JavaBean properties. However, this is only part of the Overall Picture: Spring is unique in That Uses ITS IOC Container As The Basic Building The Addresses All Architectural Tiers.

Spring provides a unique data access abstraction, including a simple and productive JDBC framework that greatly improves productivity and reduces the likelihood of errors. Spring's data access architecture also integrates with Hibernate and other O / R mapping solutions.

Spring also provides a unique transaction management abstraction, which enables a consistent programming model over a variety of underlying transaction technologies, such as JTA or JDBC.Spring provides an AOP framework written in standard Java, which provides declarative transaction management and other enterprise services to be applied to POJOs or - if you wish -. the ability to implement your own custom aspects This framework is powerful enough to enable many applications to dispense with the complexity of EJB, while enjoying some of the key services traditionally associated with EJB.

Spring Also Provides a Powerful and Flexible MVC Web Framework That Can Be Integrated Into The Overall IoC Container.

More information

See The Following Resources for more information about spring:

. Expert One-on-One J2EE Design and Development (Rod Johnson, Wrox, 2002) Although Spring has evolved and improved significantly since the book's publication, it's still an excellent place to go to understand Spring's motivation Spring home page:. Http: / /www.springframework.org. This Includes Javadoc and several tutorials. Forums and Downloads on SourceForge. Spring-user and spring-develops.

We're doing our best to improve the Spring documentation and examples. We also pride ourselves on excellent response rates to queries on the forms and mailing lists. We hope to welcome you into our community soon!

About the Author

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

New Post(0)