[Reprinted] A nice Spring Framework Tutorial

zhaozj2021-02-16  74

[Author]

Benoy Jose [Address] http://javaboutique.internet.com/tutorials/spring_frame/Article.html

[Reviews] Introduction is more comprehensive, but it is slightly thin, is the connection to Rod Johnson in the mailing list, which is not bad.

[original]

Introduction

Officially the spring season might be over but spring still seems to be lingering around. If you did not get the smell of the Spring Framework this summer then you might be missing something. You might be having your fingers crossed skeptically about another run of the mill Framework system that we have been seeing for the last couple of years. Hundreds of open source and propriety frameworks have sprung all over the place making it difficult to identify what each framework does, so the skepticism is understandable, but before we write off spring like One of Those Others It Might Be Worth Briefly Peeking Through It. Spring Proposes A New Paradigm, A Pluggable, Non- Intrusive and Robust Framework.

Summary Unlike other frameworks and APIs, spring does not impose itself wholly on to the design of a project. Spring is modular and has been divided logically into independent packages, which can function independently. The architects of an application have the flexibility to implement just a few spring packages and leave out most of the packages in spring. The "Spring Framework" would not feel bad with this attitude and on the contrary encourages users to introduce Spring into existing applications in a phased manner. So no matter what kind of framework you Are Using Now Spring Will Co-Exist With IT WITHOUT CAUSING You Nightmares and Further More Spring Will Allow You to Choose Specific Packages in Spring.

The "Struts" framework is no doubt a good framework to enhance the ability of the web tier, but the biggest drawback is the fact that it caters only to the web tier and leaves most of the Enterprise tier or middle tier to the fancy of the application architects. The Application architects need to provide an additional framework to deal with the enterprise tier and make sure that the new framework integrates well with the Struts framework. Spring tries to alleviate this problem by providing a comprehensive framework, which includes an MVC framework, an AOP integration framework, a JDBC integration framework, and an EJB integration framework. It also provides integration modules for major O / R mapping tools like Hibernate and JDO. Spring provides all these in a modular fashion without imposing any layer on to the user. Spring is not a take-it-or- leave-it kind of framework. It tries to seamlessly blend INTO The EXISTING FRAMEWORK USERS HAVING WITHOUT HINDRANCES. Spring Also Provides Transac tion management support using Java classes, email support packages using framework classes, web services support through proxies and many more features like the above. As mentioned earlier all these packages are optional and spring does not make any of them mandatory. Spring can seamlessly integrate with existing applications and provide specific functionality that you intend to provide with minimal demands for customization. A user can continue to use Struts for the web tier and toplink O / R for the database and meanwhile hook spring to provide e-mail services and web services support .

Inversion of Control / Dependency InjectionDependency injection is kind of an Inversion of Control pattern. It is proposed as an alternative for the Service Locator. Martin flower discusses the Dependency Injection pattern, Inversion of Control pattern and the differences it has with the service locator in an Article At His Site (See Resources Below).

Dependency Injection proposes separating the implementation of an object and the construction of objects that depend on them. The job of coordinating the implementation and construction is left to the Assembler code. The object that needs to be implemented does not need to instantiate the dependent objects and can rely on the assembler to do the job. The assembler will gather and instantiate, if necessary, all the dependent objects and make them available to the implemented object. Since the assembler does not depend on the code directly changes can be made to the assembler without any changes to the code. The assembler gathers the required classes through configuration files so a change in the assembler only needs changes to the configuration file. in this case the Assembler code would be the spring framework.

.

.

Type 1 IOC also called Interface Injection In Type 1 IOC the injection is done though an interface. The interface will define the injection method and the implementation class has to implement this interface and provide concrete implementation for the injection method. Type 2 IOC also called Setter Injection In Type 2 IOC the injection is done via a setter method. Type 2 IOC uses setter methods to get the dependent classes it needs. Type 3 IOC also called constructor Injection. In Type 3 IOC implementing class defines a constructor to get all its dependents The Dependent Classes Are Defined in The Constructor Arguments. How does Spring Work?

The idea is that beans follow the Dependency Injection pattern. Beans have information on the classes dependent on them. Beans define their own attributes within bean descriptors and also define the beans they depend on in the same descriptors. The Bean does not need to locate and instantiate these classes using service locators or JNDI calls. The Assembler (Spring Framework in this case) takes care of finding these classes based on the information provided in the descriptor and makes them available to the calling class. The service locator pattern does almost the same job, so why do we need another pattern to do it. The class that needs the dependent classes needs to tell the Service Locator as to which classes are required by it and moreover the responsibility of finding these classes and invoking them falls on the calling class THIS MAKES THETETHER MAKING THEM DIFFICULT TO UNIT TEST TEST The. TestLELTLEE. In The Case of The Dependency Injection Pattern T he responsibility is shifted to the Assembler to load these classes. The assembler can make changes to the dependent classes by simply modifying the descriptor file. Martin Flower's article in the resources shows the comparison between Dependency Injection and Service Locator with a good example. Beans, BeanFactory and Application Context The basic package in the spring framework is the org.springframework.beans package. Spring framework uses JavaBeans and this package provides most of the basic functionality to manipulate Java beans and provides the basic infrastructure for the other spring framework classes. This Package Also Provides The Basis for the "Dependency Injection"

pattern that Spring is based on There are two ways in which clients can use the functionality of the Spring Framework -. the BeanFactory and the ApplicationContext The BeanFactory is a generic factory, which stores the information about all the Spring Beans and allows the user. to instantiate beans and manage them. The BeanFactory provides programmers with the facilities to implement the basic features of the Spring Framework. The ApplicationContext builds on top of the BeanFactory and inherits all the basic features of the Spring Framework. Apart from the basic features, ApplicationContext provides additional features like Event management, internationalization support and resource management. The BeanFactory is particularly useful in low memory situations like in an Applet where having the whole API would be overkill. It provides the basic spring framework features and does not bring all the excess baggage That ApplicationContext Has. ApplicationContext Helps The User To Use Sprin g in a framework oriented way while the BeanFactory offers a programmatic approach. BeanFactory A BeanFactory is like a factory class that contains a collection of beans. The BeanFactory holds BeanDefinitions of multiple beans within itself and then instantiates the bean whenever asked for by clients. XMLBeanFactory is a BeanFactory implementation provided within the Spring Framework. The XMLBeanFactory can read BeanDefinitions from a XML file directly. The XMLBeanFactory validates the XML using a DTD file called beans.dtd and checks for inconsistencies in the XML. Bean Class The Bean, which is stored In The Beanfactory, Is The Actual Class That Would Carry The Logic for the Bean.

Spring does not define any standards on how the bean needs to be structured. Any J2EE complaint bean structure is acceptable to Spring. Unlike Struts and other frameworks the beans do not need to implement any special Spring interfaces to make them work in the Spring Framework. Depending upon the kind of Inversion required the bean might have to follow the rules of the corresponding Inversion Dependency pattern. Spring supports only constructor based injection and setter based injection. So a bean that used constructor-based injection should have to define constructors accordingly. Spring recommends setter-based injection over constructor-based injection as multiple constructors can make the bean huge and unmanageable. A bean has one or more IDs associated with it. The ID should be unique within the BeanFactory it is contained in so that the BeanFactory can look Up the bean using the id. if a bean has multiple IDs the bean. The Spring Framework Takes care of how the beans can be created and sent back to clients. Beans can be deployed as singletons or as non-singletons. If a bean is defined as a singleton then only one instance of the bean is created by the BeanFactory and returned when a request for the bean is made. All subsequent requests get the same instance which was created first. Non- singleton beans or Prototype beans can have multiple instances created. So each time a request is made for a bean to the BeanFactory a new instance is created And Returned.

The BeanFactory can not do Lifecycle management of a prototype bean, as a new instance is created for each client who requests it (Lifecycle management of a bean is discussed in a subsequent section). BeanDefinition The BeanDefinition contains all the information required by the BeanFactory to instantiate the bean. This includes any dependencies the bean may have on other beans and how the bean needs to be configured in the container. When a request is made for a bean, the BeanFactory loads the corresponding BeanDefinition and instantiates the bean. The BeanDefinition has information on how the bean would be instantiated (Singleton or prototype) and callback methods defined in the Lifecycle section. Lifecycle of a bean After the bean is initialized the BeanFactory can use callback methods to change the behavior of the bean in the BeanFactory. One of the Callback Methods Is The INIT-METHOD. The InitializingBean Interface of The Factory Package Gives The Ability To Do Initialization work after properties are set up for a bean in the BeanFactory. The afterPropertiesSet () method in the InitializingBean interface allows the user to check if all the properties are set properly or if some operation has to be done after the properties have been set. All bean classes that need to use this callback method need to implement the InitializingBean interface and implement the afterPropertiesSet () method. If the initialization work is minimal the user has the flexibility to designate a method within the bean, which will be invoked after bean properties have BEEN SET. The Designated Method, Which Would Do The Initialization, Can Be The Be Specified In The init-method attribute of the bean.

Designating the init-method attribute can help to avoid implementing the InitializingBean interface. In a similar fashion a DisposableBean interface is provided to do cleanup operations after a bean is destroyed. The bean has a destroy-method attribute that can used to designate a method within the bean, which will do the clean up work when the BeanFactory destroys the bean Other Features:.. Spring can automatically resolve the dependent beans with the help of the BeanFactory This facility can reduce the need to specify properties or constructor arguments Spring can check. dependencies on a bean. It can check for values ​​that should be set when a bean is initialized. If the check is not done the BeanFactory will set the default value. This behavior is not mandatory and can be turned on when required. Spring can also Manage Multiple Resource Files for You. Sample Lets Go Through a Sample To Illustrate A Few of The Important Ideas Outlined Above. Shown Below Is A Snippet of XML THA T defines some beans of an actual bean.

My famous Hello World Program My famous Hello World Program needs me The sample above shows a BeanDefinition for a HelloWorld class, which depends upon the BootStrapClass class. The property 'myString' would translate into a field into the bean with a default value specified within it. Since the BeanFactory knows that this bean requires the class FirstDependentClass, it would instantiate that class before it Sets the reference for tryings in the main bean. The property "depend entClassId "would also become a field in the bean with a reference to the FirstDependentClass. The loadProertiesFromFile method which is specified as a init method will be invoked after properties have been set to the BootStrapClass bean. Application Context The ApplicationContext provides a framework type of API to Spring. The framework allows the user to use a ContextLoader to load beans. The ContextLoader does the job of the BeanFactory. The ApplicationContext provides all the features offered by BeanFactory and allows the user to add some additional features.

Some of the features offered by the ApplicationContext are MessageSources, Resource management and Event Propagation. MessageSource offers an i18n type messaging facility. Event Propagation allows Spring to have an event handling mechanism. The Event handling is done via the ApplicationEvent Class and the ApplicationListener interface. Every bean that's wants to be notified of any events would implement the ApplicationListener Interface. Whenever an ApplicationEvent takes place the ApplicationContext knows about it. The ApplicationContext would notify the beans that are listening to this event. Additional Support Spring provides a lot of additional modules, which are optional to the framework. The User has the flexibility to use them, as they need them in their application. Some of these features are briefly discussed below. Spring AOP Spring does not need Aspect Oriented Programming (AOP) to function as a framework AOP ENHANCES The Spring Middleware Support by Providing Declar ative services. The declarative services can be used as replacements for the EJB declarative services used in CMTs. The EJB "declarative services" are provided by Spring using the "declarative transaction management service." Transaction management services are based on the spring transaction management, which will be discussed next. Transaction Management Spring provides a simple programmatic transaction management API. Spring provides support for both declarative transaction management and programmatic transaction management. Declarative transaction management is used in EJB container managed transactions (CMT) and programmatic transaction management is used in EJB Bean Managed Persistence.

Additionally Spring can use transaction management to avoid the use of EJBs where not required. Data Access using JDBC The org.springframework.jdbc package provides all the JDBC related support required by the application. One of the interesting interfaces within this package is the SQLExceptionTranslator. The translator can help in providing translations from the very common and generic SQLException in to something more specific and informative. SQLExceptions are too generic and can not provide any great information that can be passed back to the user. Spring's wrapper (SQLExceptionTranslator) can implemented specific to a vendor and can provide informative error messages, which can be sent back to the user. O / R Mapping Spring provides integration with OR mapping tools like Hibernate, JDO and iBATIS. (For information on Hibernate see resources below). Spring provides very good Support for hibernate. Spring augments the power of hibernate by providing support for hibernate sessions, h ibernate transaction management and other features in Hibernate Spring Web Framework Spring provides a pluggable MVC architecture The users have a choice to use the web framework or continue to use their existing web framework Spring separates the roles of the controller;... the model object, the dispatcher and the handler object which makes it easier to customize them. Spring web framework is view agnostic and does not push the user to use only JSPs for the view. The user has the flexibility to use JSPs, XSLT, velocity templates etc to provide .

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

New Post(0)