|
|||||||||
|
|||||||||
|
|||||||||
|
|||||||||
Copyright Acknowledgement | |||||||||
The challenge is to evaluate, select, and code the Spring Framework as a replacement for the Java EE architectures:
Lighter Weight Java Frameworks There has been a move from EJB: Enterprise JavaBeans and heavy architectures to lighter frameworks such as Spring. CMP: Container-managed Persistence transaction management systems with complex container independent services have been replaced with simpler Hibernate and AOP: Aspect-oriented Programming. Spring provides a full featured lightweight container for building applications based on the principle of IOC: Inversion of Control. Spring services are used to bring open source projects into a unified framework. JavaFX is an open source tool used to create interactive environments and experiences comparable to commercial software: Abobe Flash and Microsoft Silverlight. JavaFX is platform independent and integrated with the Java Runtime. A device or system which runs Java can provide a JavaFX application. The Spring Framework has been designed to facilitate Java application development by ensuring the integrity of the base code and component connections. A web application is a large channel through which an application is consumed and a logical extension linked to mobile or rich clients. Spring MVC is an established technology for building web applications. It provides a component model, the Spring MVC servlet and Spring Mobile, for building and testing scalable applications. Spring MVC provides REST support for mobile open source clients - Google Android, J2ME, WebOS and proprietary platforms - Apple iOS, Blackberry, and Microsoft Windows 10Phone REST services can be utilized directly from HTML5 and AJAX.
A lightweight framework is not dependent on framework interfaces or abstract classes to hook or instantiate components. Leading lightweight frameworks include: Apache Cocoon, Google Web Toolkit, Induction, Makumba, Spring, Stripes, Struts, Tapestry, and WebOnSwing. Heavyweight frameworks require the extension of framework classes or the implementation of framework interfaces to utilize middleware. This results in a large number of classes being instantiated and integrated into an application. A heavyweight framework typically will not be dependent on another container to support the framework features. Spring and Hibernate offer a specialized featureset. Client-specific requirements and specifications will be used in presenting the subject matter.
Spring’s Bean Container and Dependency Injection The recommended practice is to create the Spring beans with dependencies at start up with lazy initialization set as true. Whenever a class requires a bean, the setter injection or constructor injection can be used to provide an instance. This will lead the Spring to act as a real container of Beans and the Singleton gives or, in the case of prototype, creates bean instances at the time of requirement. Singleton is the default.
BeanFactory is a factory implementation for creating and destroying Spring beans. ApplicationContext is an advanced Beanfactory which supports i18N - internationalization text message resolution, resource loading, and the capability for posting events to listener beans. When there is a limitation on resources and size of the application, BeanFactory is the preferred implementation. Singleton beans are loaded differently in BeanFactory and ApplicationContext. A bean factory loads all beans, while deferring bean creation until the getBean() method is called. When ApplicationContext is being used, an application context loads all Singleton beans upon context startup. An application will not have to wait for bean creation.
Given that a large number of constructor arguments can get unwieldy, especially when some properties are optional, the recommended practice is to use the setter injection. The setter injection is used to: 1- Prevent long argument lists for constructors. 2- Prevent argument lists for constructors with arguments of the same type. 3- For enabling reflection. In a compiled class file, constructor argument names are not retained. However, setter and getter names are visible for reflection. By supplying all of an object’s dependencies, constructor-injection is provided. The object is not returned to client code in less than a totally initialized state. Constructor injection also is used to set required properties that do not have default values.
A bean cannot be instantiated without having all of its dependencies. It is ready to use if all argument constructors are used and it is good for a bean with one or two properties. It is suitable for beans with several properties; this will serve to make constructor injection very lengthy. Fewer code lines as setters should be avoided and flexibility is provided for creating a valid object. It also facilitates the immutable property. Setters are self-explaining; a constructor argument with same type might not be clear.
By default, all beans defined in Spring are Singleton. Spring can be used to create a unique instance of a particular bean by setting “Singleton” property of the bean to false. A new instance of this prototype bean will be created each time getBean() is invoked with the bean’s name. Prototype beans will have a negative impact on performance during creation. When resource use includes database or network connections, its use should be avoided or designed carefully. It will be useful for factory to create new bean instances.
Inner beans are used when there is a requirement not to have a particular bean available without its parent bean. It can be used to preclude Beanfactory from the bean. When a bean is dependent on another bean, such as a static variable that it will be using, then specification to the bean needs to be made inside the bean definition with the keyword “depends”. This enables Spring to initialize that bean prior to the initialization of the actual bean. Java is a registered trademark of Sun Microsystems, Inc. and Oracle America, Inc. |