Spring Overview


When considering Spring for Java you probably need to know what exactly Java is? You may think that Java is just another programming language, made only for fun or money but there is a few facts you simply need to know. The main advantage of Java is portability that enables your application to run on different systems and hardware. The second important feature is object oriented character of the language. Thanks to that, we can create a program than reflects the real world. The next think, you probably find excellent, is simply syntax, similar to popular languages like C++, PHP or C#. You need also know what a web application stands for. It is just a program which you can use via any web browser (called a ‘client’). You do not need install any additional software and benefit from such applications like online auctions, encyclopedia or webmail.

I think we have everything to move to the subject of this site. Spring is a framework working both with Java platform and .NET Framework. The idea of Spring project appeared first time in 2002 and is being developed till now (as a 3.0 version). What is the purpose of this “big” thing? Program written with J2EE Container is usually too heavy-weight. We do not need to use all of its services so why don’t think about something to replace this technology? In this way, we have the Spring that will easily manage Business Objects. What is more, we don’t have to use a specific class or interface. Anybody, who ever used J2ee Components will quickly appreciate this innovation because we simply cut down on coupling between the Clients and the Spring Framework. Let’s take a look inside the Spring Framework. It consist of modules with different services.

What is the most important principle of this framework? Of course Inversion of Control! The rule says that all the components should have abstract relationships.What exactly does it mean? Simply one of our class should not consist of the other. To solve this problem, we need to create a class or an interface that can be easily assign to a new object of any (of our) classes. It can be done in the run-time.
Sample code:

• Just a few interfaces:
public interface myService{}
public interface FirstS extends myService{}
public interface SecondS extends myService{}

• Work.java file:
(just a few methods to deal with different services) class Work {

private myService ser;
public Work(myService ser){
this.ser = ser;
}

public void setService(myService ser){
this.ser = ser;
}

public myService getService(){
return ser;
}

}

• And the most important part:
(assign a service)
myService ser = new FirstS();
// other possibility is: ser = new SecondS();

Work work = new Work(ser);

It is important to make the framework (not a client) to call the method setService(). That’s why we name it Inversion of Control – it is the framework, who sets the relationships between components.

The last thing, we want to talk about, is integrating Spring with Hibernate. So what exactly is Hibernate? The definitions says it is object-relational mapping library, working on any platform, to handle with databases. Its task is simply establishing connection and working with data (like editing, displaying, and making data independent from data type). Everything we need to do to access data is editing an Xml file, like in the example below:

<?xml version="1.0"?>;
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id = "myObj"
class = "a_path_to_.java_file_that_will_use_this_bean">
<property name = "Adress">
<value>Heaven</value>
</property>
</bean>
</beans>

That sounds great but it is necessary to say that Hibernate’s objects have to be placed in many parts of application’s code and we have to work with them ‘manually’. But there is a great solution for this problem. Let’s use here Hibernate’s object but on Spring rules – just to put them away from the application code. And that’s it. All we have to do now, is to create code for the client application with following lines:

Resource classRes = new ClassPathResource("path_to_class_file", myClass.class);
String xmlF = "./res/name_of_the_file.xml");
Resource xmlRes = new FileSystemResource(xmlF);

And load all necessery beans from .xml file.