并安装
下载日志记录器: http://mvnrepository.com/artifact/commons-logging/commons-logging
下载spring的包:http://repo.springsource.org/libs-release-local/org/springframework/spring/4.0.0.RELEASE/spring-framework-4.0.0.RELEASE-dist.zip
新建HelloWorld class
package edu.spring.beans;
public class HelloWorld {
private String name;
public void setNamex(String name){
this.name=name;
}
public void hello(){
System.out.println("hello "+name);
}
}
新建配置文件appContext.xml
<?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="helloworld" class="edu.spring.beans.HelloWorld">
<property name="namex" value="Spring"></property></bean>
</beans>
新建Main class
public class Main {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("appContext.xml");
HelloWorld helloworld=(HelloWorld) ctx.getBean("helloworld");
helloworld.hello();
}
}
结果:
hello Spring