一、环境搭建
Spring开发包的下载位置http://repo.spring.io/release/org/springframework/spring/
二、创建示例工程
1、建立Java Project工程
File-> new other->Java->Java Project 工程名为 Test
在工程文件下新建lib文件夹,将spring-framework压缩包中的libs文件夹中的jar文件拷贝到工程文件中的lib文件中,点击刷新
为了在工程中使用上述Jar文件,需要添加到Build Path中
右键Propertries选择 Java Build Path,点击Libraries 下的Add JARs 选择Test工程下的lib中的所有jar文件
点击OK
完成后的工程目录
2、编写Java文件
a.新建Java Interface
在IHelloMessage中创建方法
package com.cc.test;
public interface IHelloMessage {
public String sayHello();
}
b.创建接口实现类HelloWorld, 接口实现类,向用户输出“Hello everybody”信息
package com.cc.test;
public class HelloWorld implements IHelloMessage {
@Override
public String sayHello() {
// TODO Auto-generated method stub
return "Hello World!";
}
}
c.创建HelloChina实现类
package com.cc.test;
public class HelloChina implements IHelloMessage {
@Override
public String sayHello() {
// TODO Auto-generated method stub
return "大家好";
}
}
d.创建Person类,调用IHelloMessage接口,向用户输出问候信息
在Person类中引用IHelloMessage类,并为helloMessage创建get和set方法,右键helloMessage
选择Source->Generate Getters and Setters
选择Select All 点击OK
public class Person {
private IHelloMessage helloMessage;
public IHelloMessage getHelloMessage() {
return helloMessage;
}
public void setHelloMessage(IHelloMessage helloMessage) {
this.helloMessage = helloMessage;
}
public String sayHello() {
return this.helloMessage.sayHello();
}
}
e.创建Main程序入口,用于加载配置文件,以及IOC容器,调用人物类,向用户输出问候信息
在创建类时需要选择public static void main(String [] args)方法
package com.cc.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Resource resource = new FileSystemResource("helloMessage.xml"); // 用于读取配置文件
BeanFactory factory = new XmlBeanFactory(resource); // 用于加载配置文件
Person person=(Person)factory.getBean("person");
String string=person.sayHello();
System.out.println(string);
}
}
3、编写配置文件
a.新建配置文件helloMessage.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-bin.dtd">
<beans>
<bean id ="helloWorld" class="com.cc.test.HelloWorld"></bean>
<bean id="helloChina" class="com.cc.test.HelloChina"></bean>
<bean id="person" class="com.cc.test.Person">
<property name="helloMessage" ref="helloChina"></property>
</bean>
</beans>
4、运行事例工程
出现错误
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.<init>(DefaultSingletonBeanRegistry.java:83)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.<init>(FactoryBeanRegistrySupport.java:43)
at org.springframework.beans.factory.support.AbstractBeanFactory.<init>(AbstractBeanFactory.java:178)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:160)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:171)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.<init>(DefaultListableBeanFactory.java:193)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:78)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:67)
at com.cc.test.Main.main(Main.java:13)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 9 more
是因为缺少缺少 commons-logging.jar
下载地址 http://download.csdn.net/detail/lrici/9743894
运行结果
修改helloMessage文件
运行结果