spring2.5.6环境配置

 

 

 

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>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值