spring2.5.6的环境配置
a 普通加载
1 下载spring2.5.6
2 用Eclipse创建一个dynamic web project
3 导入spring2.5.6必须的包 spring.jar + commons-logging.jar (以上是最基本的包,后面会逐步添加新的包)
4 新建类 com.spring.domain.A.java
package com.spring.domain;
public class A {
public String toString()
{
return "com.spring.domain.A";
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
5 在src包下新建beans.xml,并配置A.java
<?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-2.5.xsd">
<bean id="a" class="com.spring.domain.A"></bean>
</beans>
6 测试是否可以正确加载spring框架
a 新建测试类 com.spring.test.
package com.spring.test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.domain.A;
public class TestA {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//加载spring配置文件
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
A a = (A)ctx.getBean("a");
System.out.println(a.toString());
}
}
b refresh项目 执行 TestA的Main方法 控制台输出: com.spring.domain.A spring加载成功
b web加载
在web.xml里添加spring的配置路径和spring的监听器,这样web服务器加载时自动加载spring
<!-- spring 加载配置路径 加载WEB-INF/spring下所有的xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/*.xml</param-value>
</context-param>
<!-- 加载spring -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>