在eclipse中,使用spring tool suite配置spring環境


本人第一次接觸spring,在經過一天的努力之后,終於成功配置了spring環境。

使用spring tool suite配置

1.打開eclipse,選擇help->Eclipse marketplace,在Search中搜索spring tool suite,點擊install,在下圖中,由於本人已經安裝了,所以右下角顯示為installed。

2.新建一個java project,可命名為springdemo,右擊new->Folder,取名為lib,在lib文件中導入以下五個jar包,直接復制粘貼就行。選中五個jar包,右鍵選擇Build Path->Add to Build Path

完成后項目如下所示

3.建立一個Package,定義兩個java文件,main.java和Person.java文件。 右鍵src,選擇new->other.在Wizards中輸入spring,選擇第二項Spring Bean Configuration file,並命名為xjc.xml.

4.編寫main.java,Person.java,xjc.xml三個文件,代碼如下所示,至此就完成了一個簡單的demo。

package spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class main { public static void main(String[] args) { ApplicationContext aContext=new ClassPathXmlApplicationContext("xjc.xml"); Person person1=(Person) aContext.getBean("person1"); System.out.println(person1); } }
package spring; public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } } 
<?xml version="1.0" encoding="UTF-8"?> <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"> <bean id="person1" class="spring.Person"> <property name="name" value="張三"></property> <property name="age" value="12"></property> </bean> </beans>

 

進行測試,輸出結果為

五月 17, 2018 9:36:32 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4783da3f: startup date [Thu May 17 21:36:32 CST 2018]; root of context hierarchy 五月 17, 2018 9:36:32 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [xjc.xml] Person [name=張三, age=12]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM