您的位置:首页 > 编程语言 > Java开发

新新新新新新到不行的新手手向:Spring在Eclipse中的配置方法

2016-03-21 03:42 363 查看
一、软件准备
Spring Framework:
下载地址http://repo.spring.io/release/org/springframework/spring/
为了能够使测试类能在控制台输出结果我们还需要下载commons-logging,
下载地址如下:
http://commons.apache.org/proper/commons-logging/download_logging.cgi
二、软件安装
将下载好的Spring-framework 解压到固定文件夹如:H:\java\Spring
如图为解压完成的文件夹内容:



commons-logging同上 我把他们都放在了H:\java文件夹中

三、配置Eclipse:

打开Eclipse-window-Preference

找到java-Build Path-User Libraries



填入自己的库名:





添加上spring离线包里libs内的jar包以及下载的commons-logging jar包,完成后我们就可以测试我们的项目了。

四、测试项目

我们需要几个类来完成测试

在测试之前我们需要把测试代码的类和配置文件建好:

首先新建java project



然后在src文件夹中新建几个类:



(这里的Person上面的小S是我添加完bean Xml之后出现的,)

然后在src文件夹下新建bean.xml



完成后是这个样子的:



然后需要配置项目类库:

右键项目properties-java Build Path选择Add Library



选择userlibrary:



然后选定我们刚才添加的类库就好了

Person类代码

<span style="font-size:18px;">package com.test.bean;
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;
}
public void info(){
System.out.println("一起来吃麻辣烫!");
System.out.println("name:"+getName()+" age:"+getAge());
}
}</span>


test类 代码

<span style="font-size:18px;">package testSpring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.bean.Person;
public class test {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
//读取bean.xml中的内容
Person p = ctx.getBean("person",Person.class);
//创建bean的引用对象
p.info();
}
}
</span>


然后是bean.xml 中的代码

<span style="font-size:18px;"><?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="person" class="com.test.bean.Person">
<property name="name" value="xingoo"/>
<property name="age" value="12"/>
</bean>
</beans>
</span>

然后跑起来的输出结果是这样的:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: