在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]
posted @ 2018-12-12 11:00  野狼谷  阅读(2901)  评论(0编辑  收藏  举报