Spring is one of the most popular framework out side of the standard. The basic idea that Spring promotes is inversion of control.Spring was first introduced by rod Johnson in 2004. Spring framework has put the following principals as their mission statement. These statements do not look path breaking at the moment as most of the newer generation frameworks has bought these ideas. But it was quite path breaking when they were introduced first time when the world was struggling with EJB2.1 infinite number of interfaces. Now the mission statements:
- J2EE should be easier to use - Take this statement with the amount of code you have to write in EJB2.1 or doing a statement execution against a database. Many young developers may not appreciate this but talk to some of your senior colleagues about the way the EJB2.1 programs were done.
- It’s best to program to interface, rather than classes. Spring reduces the complexity cost of using interfaces to zero. - This is a basic programming principal and is actually at a higher level than Spring. It's about loose coupling and building services which are bound with contract and not glued tightly with implementations.
- JavaBeans offer a great way of configuring applications - Your class is a plain POJO and do not depends on the framework. This improves the testability of the program.
- OO design is more important than any implementation technology. - Again this comes basically from EJB2.1. The framework forces you to write so many things as part of implementation that you start loosing grip on your Business logic, which is covered more with OO paradigm.
- Checked exceptions are overused in Java. A framework shouldn’t force you to catch exceptions you’re unlikely to be able to recover from.- Checked exceptions are nuisance most of the time. Think if any time in your life you have done anything useful with your SQLException when dealing with JDBC code.
- Testability is essential, and a framework such as Spring should help make your code easier to test. - This again comes from loose coupling in the pieces of the program so that they can be tested independently.
Spring Hello World Program
Let's write a simple Hello world program using the spring way:
Write a HelloWorld Bean which has a method print message
@Component("helloWorld")
public class HelloWorld {
public String getMessage(){
return "Hello World";
}
}
public class HelloWorld {
public String getMessage(){
return "Hello World";
}
}
Note the @Component annotation. That tells Spring that HelloWorld is a managed bean for Spring
Now let's write the Spring configuration file. We will name it as application-context.xml. The name can be anything. Put the file somewhere in the classpath.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <context:component-scan base-package="com.lalit" /> </beans>
Write the main program. We will write it as a JUnit test
public class SpringHelloWorldTest { @Test public void test() { // Let's create the Spring container ApplicationContext applicationContext = new ClassPathXmlApplicationContext( new String[] { "application-context.xml" }); // Fetch the helloWorld bean HelloWorld helloWorld = applicationContext.getBean("helloWorld",HelloWorld.class); //Let's check if the bean is not null and returns the string assertNotNull(helloWorld); assertSame("Hello World", helloWorld.getMessage()); } }
If you look into the test class than we are not creating the HelloWorld object ourself but we ask spring factory to return us an instance of HelloWorld object. This is the most important aspect of Spring, which is Ability to Create Objects. Spring essentially is a factory which creates objects for us.
To decouple the functionality of providing message to HelloWorld we pass that responsibility to a different interface called Message, which will be injected with a concrete implementation HelloWorldMessage
Message Inteface:
public interface Message {
public String getMessage();
}
Message Inteface:
public interface Message {
public String getMessage();
}
HelloWorldMessage:
@Component("helloWorldMessage")
public class HelloWorldMessage implements Message{
@Override
public String getMessage() {
return "Hello World";
}
}
public class HelloWorldMessage implements Message{
@Override
public String getMessage() {
return "Hello World";
}
}
Now HelloWorld has a dependency relationship and it needs an object of HelloWorldMessage. For that we need to modify the HelloWorld class. See that we have injected a Message interface but the object of concrete class will get injected
@Component("helloWorld") public class HelloWorld { @Autowired Message helloWorldMessage; public String getMessage(){ return helloWorldMessage.getMessage(); } }
@Autowired annotation tells Spring to find the helloWorldMessage object and set it to the helloWorldMessage attribute.
The test class still remains the same. Here what we did is to use spring to build the relationship. This is another important aspect of Spring. To Wire the relationship..
Again to reiterate, the two important things that Spring bring to table is:
- Ability to create objects.
- Ability to build the relationship between objects.
The corollary is that if you are making instance of objects your self or building relationships yourself than you are not using Spring effectively.
Check Source code at GitHub
Check Source code at GitHub
Please follow the video to how to make the application:
These are basic premises of Inversion of Control(IOC) or DI(Dependency Injection). We will not get into theoretical debate here. But the important thing to understand is that the control of building objects and wiring relationships is delegated to the environment. You as an application developer go to the Spring container and ask for your object to do your work. The object is given to you created and all relationships set.
For pure XML way of making Spring application
More Articles on Spring
For pure XML way of making Spring application
More Articles on Spring
No comments:
Post a Comment