x


Enter your email address below to receive updates each time we publish new Video Tutorial.

Privacy guaranteed. We'll never share your info.

Introduction To Spring - Spring Video Tutorial - Spring Training

DEAR VISITOR , YOU ARE WATCHING : Introduction To Spring - Spring Video Tutorial - Spring Training .

[postlink] http://j2ee-tutorials-videos.blogspot.com/2011/03/introduction-to-spring-spring-video.html [/postlink] http://www.youtube.com/watch?v=Q6mz3lrZqs0endofvid [starttext]

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.
  • 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.

Before we move further download the Spring framework from http://www.springsource.org/download(external link). Dowload spring-framework*-with-dependencies.zip. This includes all the dependent libraries also. When you unzip it, it has following important directories.

  • dist - Contains spring framework.jar
  • docs - Contains documents.
  • lib - Contains all dependent libraries.
  • src - Contains source code.

Spring Hello World Program


Let's write a simple Hello world program using the spring way:

Bring the following jars in library path:

  • dist/spring.jar
  • lib/log4j/log4j-1.2.14.jar
  • lib/jakarta-commons/commons-logging.jar

Write a HelloWorld Bean which has a method print message


public class HelloWorld { 
  public void printMessage(){
    System.out.println("Hello World");
  }
}


Write the Spring configuration file. We will name it as context.xml. The name can be anything. Put the file somewhere in the classpath.


  


Write the main program

String[] files = {"context.xml"};

//Start the context of Spring 
ApplicationContext appContext = new ClassPathXmlApplicationContext(files);

//Get the Helloworld bean from spring context
HelloWorld helloWorld = (HelloWorld)appContext.getBean("helloWorld");
helloWorld.printMessage();


If you look into the main program 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.

Let's now say that we want to decouple the functionality of providing message to HelloWorld and pass that responsibility to a different class called HelloWorldMessage.
HelloWorldMessage:

public class HelloWorldMessage {
 public String getMessage(){
  return "HelloWorld";
 }
}

And our HelloWorld class would look like

public class HelloWorld { 
 private HelloWorldMessage message; 
 public void setMessage(HelloWorldMessage message) {
  this.message = message;
 } 
 public void printMessage(){
  System.out.println(message.getMessage());
 }
}

Now HelloWorld has a dependency relationship and it needs an object of HelloWorldMessage. For that we need to modify the context.xml


 
  
  
 
 

The main 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.

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.
[endtext]
This site does not store any files on its server.We only index and link to content provided by other sites.If you have any doubts about legality of content or you have another suspicions, feel free to contact us HERE - Thank you.

0 commentaires :

Post a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

You can see also

J2EE is an acronym for Java 2 Enterprise Edition. The Java 2 Enterprise Edition is Software that can be used along with the J2SE, the Java 2 Standard Edition which includes the compiler and the other fundamentals of Java. The J2EE software includes development and deployment systems for Java Servlet and Java ServerPages. Both of these combine Java and HTML to produce web pages on demand. Author Mohamed Habou . understand this powerful component of Java programming.

Android Cloud To Device Messaging Video Tutorial

JPA Video Tutorial

Spring MVC Video Tutorial

Spring Security Video Tutorial

Primefaces Video Tutorial

Struts Video Tutorial

free counters

Introduction to J2EE Videos Tutorials

Latest GWT Video Tutorials

Latest Spring Videos Tutorials

Latest Hibernate Videos Tutorials

Latest JAVA Video Tutorials

Latest Struts Videos Tutorials

J2EE is an acronym for Java 2 Enterprise Edition. The Java 2 Enterprise Edition is Software that can be used along with the J2SE, the Java 2 Standard Edition which includes the compiler and the other fundamentals of Java. The J2EE software includes development and deployment systems for Java Servlet and Java ServerPages. Both of these combine Java and HTML to produce web pages on demand. VTC Author Mohamed Habou takes you on a step by step guide to understanding this powerful component of Java programming.