Google

Friday, March 30, 2007

Why Spring framework???

Spring is a lightweight open source framework written by Rod Johnson in 2000 while he was working as an independent consultant for customers in the financial industry in London. It has been widely accepted in industry.

There are many java frameworks available in market; widely accepted ones are struts as MVC framework, Hibernate as ORM framework, EJB and frameworks like velocity and tiles at a view layer. Each of these frameworks serves specific purpose and also requires a container. And integrating them in one application is a pain for programmer. Spring unlike those frameworks is a container as well as a framework and supports integration with all other frameworks by means of some configuration. It is framework because it has its own implementation of MVC and container because it can manage your implementation of ORM and MVC as well as it also integrates well with EJBs and in that case you don't require EJB container. Spring becomes container of EJBs there.

On top of this spring gives some unique features like IoC and AOP.

Inversion of Control:

Spring is an IoC container," Jeff explained. "That stands for Inversion of Control, which enables you to inject dependencies by declaring them in an XML file."

Just to give you an example, I have an application which accepts payment from end users, I have to calculate tax while calculating payment. Here Payment and TaxCalculator are two beans. Every time i instantiate Payment, i need TaxCalculator which is dependency. One option is to whenever i create Payment, i can create TaxCalculator and can give a reference of it to Payment. Instead if you use spring, you just need to define in configuration that Payment has a dependency on TaxCalculator. So whenever Payment is instantiated, it will get TaxCalculator instance from session. I won't have to code anything for that.
Note: This can only be used for dependency of service classes.

Another example of IoC is, You have a TaxCalculator which calculates tax, but its implementation will change country by country. So while deploying application, you specify which country you are deploying it for, And it will automatically pick up TaxCalculator of that country by simple means of configuration, which reduces programmers overhead of writing factory implementation or any other implementation to have this capability.

Exception Handling

Spring wraps all checked exceptions into run time exceptions. So you don't have to unnecessarily catch them when you are sure they won't occur. In fact new versions of EJB, struts and Hibernate also does this.

Aspect Oriented Programming

Easy integration of AOP is unique feature that spring cones with. AOP enables you to do lots of interesting things.

  1. You can add member variables or member methods to any class without actually modifying definition of that class.
  2. You can introduce Aspects into your application. Aspect is a behavior that applies to many of your classes and implementation of that behavior is same for all those classes. You just have to create Aspect and introduce it to set of classes to which that behavior applies. Ex: Logging behavior
  3. You can handle your exceptions in a better way. You can create Aspects for all exceptions and handle them at one place. Then by configuration apply those Aspects to all classes. Doing this you don't have to handle exceptions separately in all classes which saves lots of programming effort.

Access Control

Spring has a nice way of managing access control. Once you have Authentication and Authorization modules in place, AccessDecisionManager provided by spring lets you define access control per bean through configuration. So if you want to change access control for any information in future, you just have to change configuration and you are done.

Scheduled job or crone job

Spring has a mechanism by which you can create crone job or scheduled job. It uses Java Time APIs. It also supports quartz to create scheduled job.

Managing Configuration:

When you use hibernate and struts, and do not use any features of spring as such, Spring becomes a container for you which integrates both of them via configuration. Spring makes everything as just configuration in which case your configuration file can become too large to manage. To address this, Spring allows you to have array of configuration files, list them all in one file and supply that file as a configuration file. Spring will do rest for you.

Spring also lets you expose any service you have written as a web service or Remote Method by means of configuration.

Spring divides whole web application into different tier. And handles each tier separately. So that you can integrate any component at any layer and can also replace a component at any layer without affecting other layers or other components. That is the only reason of ease of integration of struts or hibernate or any other framework with spring.

So if you know struts, hibernate, EJB, RMI technologies and want to use one of them or all of them or some of them in your application then integration of those technologies is just a matter of configuration using spring. otherwise you will run into writing whole lot of code which is pain and difficult too. Also there is huge community built around spring that helps a lot in figuring out or solving problems that you may face while development.

For detailed information on spring framework visit springframework.org.

Labels: , ,

Wednesday, March 14, 2007

Article on AJAX

Introduction to AJAX

AJAX (Asynchronous Java-script Ans XML) is a widely accepted technology for web based applications. If you have many forms in your web page and you want user to send data to server — once they have filled out one of the forms, They normally have to submit the form and then wait as the entire page is refreshed including all other forms. Similarly, if you want to refresh a portion of your web page, you have no choice but to reload the whole page.

This is inefficient, time-consuming, and particularly frustrating if there’s only a small amount of data needs to be sent back and forth. In this Article, you’ll be introduced to AJAX, a technology that allows you to send these requests through small !JavaScript calls, and a !JavaScript then refreshes only an area of a page rather than whole page.

AJAX is not only about refreshing an area of web page, But it is also about user experience. It offers almost same user experience, in web applications, as desktop based applications offer.

Looking at the history of AJAX, The term AJAX was first coined by Jesse James Garrett in a blog entry on February 18, 2005. Microsoft is the first to introduce a way of making calls to server using JavaScript in IE 5.5. Microsoft's Exchange 2000's web based Outlook is the first well known application to use AJAX. But strength of technology was overlooked that time. Extensive use of AJAX by Google in GMail, Orkut and GoogleMap brought AJAX into attention. It then became so famous that all others also started using AJAX into their web applications. Read A brief history of AJAX for further information.

Working of AJAX

Ajax is not a technology, but it is a combinations of technology working together. Those technologies include,

HTML and CSS for presenting.
JavaScript to make a call to server, Getting a response and modifying HTML DOM.
The XMLHttpRequest class to send or receive data back and forth to server.

Traditional web application model works like this: Most user actions in the web page trigger an HTTP request back to the server. The server does some processing — and then returns an HTML page to the client. This approach seems nice, but it does not give a great user experience. While the server is doing its thing, Why should user be waiting?

AJAX eliminates the start-stop-start-stop nature of interaction of web application. It introduces one layer between client and server called AJAX engine which makes calls to server asynchronous.

Comparison between classic web application approach and Ajax web application approach:



Image taken from blog post of Jesse James Garrett

AJAX engine is responsible for both rendering the interface the user sees and communicating with the server on the user’s behalf. The Ajax engine allows the user’s interaction with the application to happen asynchronously — independent of communication with the server. So that user does not have to wait for the response of one action and can keep working on page continuously.

Difference between Synchronous and Asynchronous approach:


Image taken from blog post of Jesse James Garrett

pros and cons of AJAX

Pros of using AJAX
  • User Experience
  • Bandwidth usage
  • Separation of data, format, style and function
Cons of using AJAX
  • Browser support
    • Browser integration
    • Back button problem
  • Bookmark problem
  • Search engine optimization
  • JavaScript reliability
I have taken this from wikipedia. I will be writing on these pros and cons in detail with examples. For further information on pros and cons of AJAX read Why use AJAX

What is AJAX framework?

AJAX framework hides, the complexity involved with AJAX from programmers and makes their life easy. Any AJAX framework consists of two parts. One is client side part which involves sending request to server, getting the response, and processing response. And the other is server side part which involves processing the requests, searching for the data, and sending response back to client.

Different AJAX frameworks and comparison between them

There are many AJAX frameworks available. This gives you good overall comparison between those available frameworks.

Other References

  1. Mastering AJAX
  2. Seven Things to know for AJAX
  3. AJAX tutorial
  4. AJAX based TWiki
  5. Learn step by step AJAX
This is it for AJAX as of now. Be tuned to get more on AJAX. Feel free to write your views and feedback. Let me know if i can even help you solve any problems in AJAX.

Signing Off
Kiran Thakkar

Labels: , , , ,