Spring Hello World)

Source: Internet
Author: User

1. Download spring-Related Development Kits
: Http://sourceforge.net/project/showfiles.php? Group_id = 73357
Spring-framework-1.2.6-with-dependencies.zip. one example is spring-framework-1.2.6.zip. It is recommended to download with-dependencies. There are additional packages in it. After downloading and decompressing, THE Dist directory is the jar of spring and the lib directory is.
2. Create a Java project and add spring. Jar (under the DIST directory) and commons-logging.jar (under the lib directory) to the build path of the project.
3. Create a pojo Bean class: hellobean
// Hellobean. Java
/**
*
*/
Package com. lzy;

/**
* @ Author lzy
*
*/
Public class hellobean {
 
Private string hello;
 
Public void sayhello (){
System. Out. println (this. gethello ());
}

/**
* @ Return returns the hello.
*/
Public String gethello (){
Return hello;
}

/**
* @ Param Hello the hello to set.
*/
Public void sethello (string Hello ){
This. Hello = Hello;
}
 

}

4. Create the bean. xml file and assign a value to the hello attribute of a hellobean instance in this XML file.
// Bean. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<Description> spring Quick Start </description>
<Bean id = "hellobean" class = "com. lzy. hellobean">
<Property name = "hello">
<Value> Hello World </value>
</Property>
</Bean>
</Beans>

5. Create a test class: testspring
// Testspring. Java
/**
*
*/
Package com. lzy;

Import java. util. locale;

Import org. springframework. Context. applicationcontext;
Import org. springframework. Context. Support. filesystemxmlapplicationcontext;

/**
* @ Author lzy
*
*/
Public class testspring {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub

Applicationcontext CTX = new filesystemxmlapplicationcontext ("bean. xml ");
Hellobean Hello = (hellobean) CTX. getbean ("hellobean ");
Hello. sayhello ();
}

}

6. Run the test class:
If no error occurs, "Hello World" will be displayed in the output ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.