Introduction to Spring -Part 1


Here is my first technoticle (technical +article) to give an introduction of Spring.

 

Spring released its first version around 5 yrs back and in a short span of time it has gained huge popularity and now it has become one of the main building components of J2EE application. It has progressed from version 1.2 to the present 2.5, and has been adopted in an even wider range of industries and projects.

I believe Spring has gained huge popularity because:

  1. It is managed by a light weight container known as Inverse Of Control (IOC).
  2. It is modular in design. The various functionalities are modeled separately such that each module can work as an independently with the support of other modules.
  3. It has managed to overcome the EJB drawbacks, especially the cumbersome configuration need to be done just to create a bean.
  4. Applications built using Spring are very easy to test. For certain unit testing scenarios, the Spring Framework provides mock objects and testing support classes. Spring also provides unique “integration testing” functionality in the form of the Spring TestContext Framework and legacy JUnit 3.8 support classes that enable you to test your code quickly and easily, even while accessing a staging database.
  5. Spring is essentially a technology dedicated to enabling you to build applications using Plain Old Java Objects.
  6. Spring provides integration and support for many other technologies like Jsp, Struts, EJB, Hibernate etc.
  7. Spring helps in writing less redundant code and also separates the configuration code in a xml file.

 

The basic overview of Spring framework is:

 

 

 

The Core package is the most fundamental part of the framework and provides the IoC and Dependency Injection features. The main functionality of the container is to create and mangae beans. The Context package build on the solid base provided by the Core package: it provides a way to access objects in a framework-style manner in a fashion somewhat reminiscent of a JNDI-registry. The context package inherits its features from the beans package and adds support for internationalization (I18N) (using for example resource bundles), event-propagation, resource-loading, and the transparent creation of contexts by, for example, a servlet container.

The DAO package provides a JDBC-abstraction layer that removes the need to do tedious JDBC coding and parsing of database-vendor specific error codes. Also, the JDBC package provides a way to do programmatic as well as declarative transaction management, not only for classes implementing special interfaces, but for all your POJOs (plain old Java objects).

The ORM package provides integration layers for popular object-relational mapping APIs, including JPA, JDO, Hibernate, and iBatis. Using the ORM package one can use all those O/R-mappers in combination with all the other features Spring offers, such as the simple declarative transaction management feature mentioned previously.

Spring’s AOP package provides an AOP Alliance-compliant aspect-oriented programming implementation allowing you to define, for example, method-interceptors and pointcuts to cleanly decouple code implementing functionality that should logically speaking be separated.

Spring’s Web package provides basic web-oriented integration features, such as multipart file-upload functionality, the initialization of the IoC container using servlet listeners and a web-oriented application context. When using Spring together with WebWork or Struts, this is the package to integrate with.

 

Spring’s MVC package provides a Model-View-Controller (MVC) implementation for web-applications. Spring’s MVC framework is not just any old implementation; it provides a clean separation between domain model code and web forms, and allows you to use all the other features of the Spring Framework.

 

The main features which Spring has come up with:

1)      Inversion Of Control (IOC)

2)      Dependency Injection (DI)

3)      Aspect Oriented Programming (AOP)

4)      JDBC

5)      RMI

6)      MVC layer for J2EE Application

7)      Web flow

 

 

Each feature above me will be explaining one by one.

 In the next article I will explain IOC.

What is difference between ClassNotFoundException and NoClassDefFoundError?


Many people get confused with these 2 errors. So here is a brief explanation.

 A ClassNotFoundException is thrown when the reported class is not found by the ClassLoader in the CLASSPATH. It could also mean that the class in question is trying to be loaded from another class which was loaded in a parent classloader and hence the class from the child classloader is not visible.

Consider if NoClassDefFoundError occurs which is something like

java.lang.NoClassDefFoundError

src/com/TestClass

does not mean that the TestClass class is not in the CLASSPATH. It means that the class TestClass was found by the ClassLoader however when trying to load the class, it ran into an error reading the class definition. This typically happens when the class in question has static blocks or members which use a Class that’s not found by the ClassLoader. So to find the culprit, view the source of the class in question (TestClass in this case) and look for code using static blocks or static members.

When to use ArrayList ,LinkedList,Vector?


Consider a list collection. Now lets consider some scenario:

1) the list needs to be modify such that the elements are added only at the end of list, and if required only the last element of the list to be deleted, use ArrayList

2) the list needs to be modify such that the elements can be added at any position of the list, and if required the element can be deleted from any position, use LinkedList

3) the list needs to be threadsafe, use Vector.

4) the list is read only and needs to be iterated frequently, use ArrayList

java.lang.OutOfMemoryError while doing ant build?


While doing an ant build sometimes it happen that a user may get the OutOfMemoryError. This is mainly because the heap size of Java JVM is less. This heap size can be increased. But caution that the heap size of java JVM which is running through Ant has to be increased and not of the JVM of Jdk 1.x .

To increase the heap size of JVM in ant ,open the file ant.cmd present in Ant/bin folder.

Change following line

“%_JAVACMD%” %ANT_OPTS% -classpath “%ANT_HOME%\lib\ant-launcher.jar” “-Dant.home=%ANT_HOME%” org.apache.tools.ant.launch.Launcher %ANT_ARGS% -cp “%CLASSPATH%” %ANT_CMD_LINE_ARGS%

“%_JAVACMD%” –Xms512m -Xmx1024m %ANT_OPTS% -classpath “%ANT_HOME%\lib\ant-launcher.jar” “-Dant.home=%ANT_HOME%” org.apache.tools.ant.launch.Launcher %ANT_ARGS% -cp “%CLASSPATH%” %ANT_CMD_LINE_ARGS%