首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring项目环境筹建

2012-10-07 
spring项目环境搭建1,下载spring2.5.6.url:http://www.springsource.com/products/spring-community-downl

spring项目环境搭建
1,下载spring2.5.6.
   url:http://www.springsource.com/products/spring-community-download
   下载页面:http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-2.5.6-with-docs.zip

2,解压spring-framework-2.5.6-with-dependencies.zip文件到spring-framework-2.5.6  
   目录。

3,提取spring需要的jar包到目录spring-lib下。
   --------------------------spring 必需的jar包---------------------------
   1)spring-framework-2.5.6\dist\spring.jar
   2)spring-framework-2.5.6\lib\jakarta-commons\commons-logging.jar
   --------------------------AOP必需的jar包--------------------------------
   3)spring-framework-2.5.6\lib\aspectj\aspectjrt.jar
   4)spring-framework-2.5.6\lib\aspectj\aspectjweaver.jar
   5)spring-framework-2.5.6\lib\cglib\cglib-nodep-2.1_3.jar
   -------------------------annotation注解必需jar包----------------------
   6)spring-framework-2.5.6\lib\j2ee\common-annotations.jar

4,用myeclipse6.0新建一个java项目:michaelssh,导入相应jar包,项目编码环境设置为
   UTF-8.Window->preference->workspace->text file encoding : utf-8

5,在src下编写spring配置文件:config\beans.xml。文件格式可以在\spring-framework-2.5.6\docs\reference\html_single\index.html这里,查找“<beans>”找到。

6,编写测试类,检测环境的正确性。
package com.michaelzqm.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {

/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext springIOC =
new ClassPathXmlApplicationContext(new String[]{"config/beans.xml"});//new FileSystemXmlApplicationContext("")
}

}

7,编写单元测试类,用junit4.
package spring.test;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import spring.service.VirtualDbService;


public class SpringTest {
private static AbstractApplicationContext springIOC;
@BeforeClass
public static void beforeClass(){
springIOC = new ClassPathXmlApplicationContext(
new String[]{"config/beans.xml"});
}

@AfterClass
public static void afterClass(){
if (springIOC != null)springIOC.close();
}

@Test
public void testSpringIoc(){
VirtualDbService virtualDbService = (VirtualDbService)springIOC.getBean("virtualDbService");
virtualDbService.save("old");
}

}


8,@Resource注解方式注入bean,需修改配置文件,引入context命名空间,文件格式可以在\spring-framework-2.5.6\docs\reference\html_single\index.html这里,查找“xmlns:context”找到。
如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
              
     <context:annotation-config/>
     <!--  <bean id="virtualDbService" class="spring.service.impl.VirtualDbServiceImpl"></bean>-->
</beans>


热点排行