spring的第一个项目
1,创建一个web项目,java项目也可以。

2,导入jar包
直接把spring的jar包都导进去
另外还需要commons-collections-3.2.jar与commons-logging-1.0.4.jar
jar包列表

3,目录结构

Hello.java
package cn.abc.hello; public class Hello { public void say(){ System.out.println("hello spring!"); } }
4,创建resourse文件夹并创建ApplicationContext.xml文件

ApplicationContext.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="hello" class="cn.abc.hello.Hello"> </bean> </beans>
5,创建测试程序

package cn.abc.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.abc.hello.Hello; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Hello hello = (Hello) ac.getBean("hello"); hello.say(); } }
测试结果

测试成功
浙公网安备 33010602011771号