Basic Spring Step by Step Hello World Example

来源:互联网 发布:fugue免流源码 编辑:程序博客网 时间:2024/05/17 00:08

http://www.springaddon.com/?p=36


First lets install Spring development environment that helps in developing Spring Applications. Spring applications can be developed using normal eclipse IDE but there is an additional plugin/IDE which is given by SpringSource to ease Spring development. So we will be using this plugin/IDE for developing our Spring applications/codes. You can download it in two ways.

  1. Installing Spring IDE Plugin in existing eclipse being used.
  2. Downloading and installing new SpringSource Tool Suite.

Installing Spring IDE Plugin in existing eclipse being used

Open eclipse and go to Help -> Software Updates.

Click the “Add Site” button and enter “http://springide.org/updatesite” in the Add Site popup.

Select all the Spring IDE features and click Install.

Once the installation is complete you are done.

Downloading and installing new SpringSource Tool Suite

One can download SpringSource Tools Suit from the following link :-

http://www.springsource.org/springsource-tool-suite-download

I downloaded STS and will be using this in the example.

Now we are ready with our development environment , so lets get started with our hands on Spring :)

Open STS and go to File -> New -> Project and Select Spring Project and click Next.

 

Give name to the project as HelloWorld.

Click finish and new Spring Project with name HelloWorld is created in the workspace.

The “S” in the upper right corner indicates that it is a Spring Project.

Right click the src package and create a new package “com.springaddon.helloworld”.

Create the following HelloWorld class in the above created package :

view plaincopy to clipboardprint?
  1. package com.springaddon.helloworld;  
  2.   
  3. public class HelloWorld {  
  4.     private String message;  
  5.   
  6.     public void setMessage(String helloMessage) {  
  7.         this.message = helloMessage;  
  8.     }  
  9.   
  10.     public void sayHello() {  
  11.         System.out.println(message);  
  12.     }  
  13. }  


The HelloWorld class has a message property and its value is set using the setMessage() method. In Spring world this is called Setter Injection. So the message property is being set by a setter. The value of this message will be injected by external configuration file. We will create that xml file in next step. This design pattern is called Ddependency Injection Design pattern. Hello World class also has a sayHello() method which simple prints the message.

Now lets create a bean confriguation file which will be injecting the value for the message property in the HelloWorld class. To create a new bean configuration file right click the src folder and select New -> Spring Bean Configuration File.

Enter the bean name and click Finish

Now the spring bean config file is created, add the following to this file.

view plaincopy to clipboardprint?
  1. <!--?xml version="1.0" encoding="UTF-8"?-->  
  2. <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.xsd">  
  3.   
  4.     <bean id="helloWorld" class="com.springaddon.helloworld.HelloWorld">  
  5. <property name="message" value="Hello! Welcome to Spring World!"></property>  
  6.     </bean>  
  7. </beans>  

The <bean> tag is used to define a new bean in Spring Framework. “id” attribute is used to give logical name to the bean. So now helloWorld is a bean in the application and this name is unique which means no two beans can have same id value. “class” represents fully qualified name of the class which corresponds to this bean – helloWorld. “helloWorld” bean has a <property> in which the variable message is set to a value “Hello! Welcome to Spring World!”. Notice that this message is same as the instance variable message in HelloWorld.java class.

Now lets create a test class which will be displaying the hello message.

view plaincopy to clipboardprint?
  1. package com.springaddon.helloworld;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class HelloWorldTest {  
  7.     public static void main(String[] args) {  
  8.         ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");  
  9.         HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");  
  10.         helloWorld.sayHello();  
  11.         }  
  12.   
  13. }  


Add the following jar’s in the classpath :

antlr-runtime-3.0commons-logging-1.0.4org.springframework.asm-3.0.0.M3org.springframework.beans-3.0.0.M3org.springframework.context-3.0.0.M3org.springframework.context.support-3.0.0.M3org.springframework.core-3.0.0.M3org.springframework.expression-3.0.0.M3

Now Run the HelloWorldTest class as a java application. You will see that “Hello! Welcome to Spring World!” is printed on the console :)

Please post any questions if you have or if you are stuck somewhere. I will be there to help you out.

Thanks

原创粉丝点击