|
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>
然后跑起来的输出结果是这样的: