Java -- use spring on Eclipse

Source: Internet
Author: User

Vs. NET + spring.net + nhib.pdf used in. net. On the Java platform, it naturally corresponds to eclipse + spring + hibernate. The previous article introduced how to use hibernate in eclipse. This article briefly introduces how to use spring in eclipse.

(1) first, download spring from SourceForge, http://sourceforge.net/projects/springframework. Currently the latest can download spring-framework-1.2.8-with-dependencies.zip.

(2) You can then introduce spring into your project.
First, extract spring-framework-1.2.8-with-dependencies.zip and extract the spring. jar (in the Dist directory), commons-logging.jar (Lib \ Jakarta-commons directory), log4j-1.2.13.jar (Lib \ log4j directory) these three files copied to the D: \ Java \ spring \ Lib "directory, create a" Spring "library in eclipse, and add the three files to the" Spring "library.

(3) test:
Create two classes, student and book.

Public class Book
{
Private int id = 0;
Private string bookname;
Public String getbookname (){
Return bookname;
}
Public void setbookname (string bookname ){
This. bookname = bookname;
}
Public int GETID (){
Return ID;
}
Public void setid (int id ){
This. ID = ID;
}
}

Public class student
{
Private int age = 0;
Private string name;
Private book;

Public int getage (){
Return age;
}

Public void setage (INT age ){
This. Age = age;
}

Public String getname (){
Return name;
}

Public void setname (string name ){
This. Name = Name;
}

Public book getbook (){
Return book;
}

Public void setbook (Book ){
This. Book = book;
}

Public String getbookname ()
{
Return this. Book. getbookname ();
}
}



Then add the spring configuration file bean. XML (bean. xml must be in the directory that can be accessed by classpath ):

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring/DTD bean/EN"
Http://www.springframework.org/dtd/spring-beans.dtd>

<Beans>
<Bean id = "student" class = "com. springtest. Student">
<Property name = "Age">
<Value> 22 </value>
</Property>
<Property name = "name">
<Value> sky </value>
</Property>
<Property name = "book" ref = "book">
</Property>
</Bean>

<Bean id = "book" class = "com. springtest. Book">
<Property name = "ID">
<Value> 1000 </value>
</Property>
<Property name = "bookname">
<Value> War And Peace </value>
</Property>
</Bean>
</Beans>



The final main program:

Public static void main (string [] ARGs)
{
Resource res = new classpathresource ("bean. xml ");
Beanfactory factory = new xmlbeanfactory (RES );

Student Stu = (student) Factory. getbean ("student ");
System. Out. println (STU. getbookname ());
}


After running, you can see the console output "war and peace ".

The usage of spring.net is basically the same as that of spring.net (including the configuration file and beanfactory acquisition), so it is very smooth to transition from spring.net to spring.
Finally, the attributes in Java are not concise in C.

Related Article

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.