[Author] Benoy Jose [Address] http://javaboutique.Internet.com/tutorials/spring_frame/Article.html [Review] The more comprehensive, but also thin, is the connection to Rod Johnson in the mailing list, The instruction is not bad.
[original]
IntroductionOfficially 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.
SummaryUnlike 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 WITHOUSING 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 inteout hindrances. Spring Also Provides Transaction Manageme nt 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 change. 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.
Since the classes area independent and are integrated through the assembler, Independent Testing of each class can be done without affecting. Other Application Codes.
.
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 ..............
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 The Classes Tightly Coupled with each other making Themity. in The Case of the Dependenc y Injection pattern the 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 ContextThe 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 ....................... ..
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 Spring in a framework oriented Way While TH E Beanfactory Offers a Programmatic Approach.
Beanfactorya Beanfactory Is Like A Factory Class That Contains A Collection of Beans. The BeanFactory Holds Beandefinitions of Multiple Beans 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 ClassThe 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 kind be unique with it An using the id. if a bean has multiple ids theies.
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) .BeanDefinitionThe BeanDefinition contains all the information Required by the beanfactory to instantiate the bean. this incrudes any dependencies the bean may have other beans and how the bean neseds to be configured 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 BeanAfter 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 Then Be Specified in The init-method attribute of the bean. desig nating 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.SampleLets go through a sample to illustrate a few of the important ideas Outlined Above. Shown Below Is A Snippet of Xml That Defines Some Beans of An Actual Bean.
Application ContextThe 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 SupportSpring 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 AOPSpring does not need Aspect Oriented Programming (AOP) to function as a framework. AOP enhances the Spring middleware support by providing declarative services. The declarative services can be used as replacements for the EJB declarative services used in CMTs. The EJB "declarative services "" Transaction Management Service Management Service. "Transaction Management Services Are Based, Which Will Be Discussed Next.
Transaction ManagementSpring 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 JDBCThe 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 vendo R and can provide Informative Error Messages, Which Can Be Sent Back to The User.
O / R MappingSpring 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, Hibernate transaction . management and other features in Hibernate Spring Web FrameworkSpring 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 the view. Spring and ejbin a Usual EJB Application EJBS Are Located Using Service Locators and Managed Business Business Delegates. Thase Results in EJB JNDI LOOK UPS Scatter ed all over the code. Spring can provide proxy objects, which can act as business delegates do the same job. These proxies can be configured into the BeanFactory and the ApplicationContext. ConclusionSpring is very new to the market and provides a new lease of life to Framework development. Spring does not try to duplicate existing solution;
it tries to integrate with exiting solutions like JDO, AOP and Hibernate. However spring creates a new web framework to replace Struts, as the creators of Spring feel that Struts is not robust enough. Spring would generate great interest in the coming months as more people become aware of it and find it worthwhile to implement. Spring is portable across application servers, as it does not include any application server or platform specific requirements. This makes it compatible on major application servers like Websphere, Weblogic, Tomcat, Jboss and others like them. As of now spring can be integrated and used in a eclipse IDE, however as the versions improve spring should be providing support for more IDEs. Benoy Jose is a web developer with over six years of experience in J2EE and Microsoft technologies. He is A Sun Certified Programmer and Enjoys Writing Technical and Non-Technical Articles For Various Magazines. --------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------