Spring学习笔记(一)环境搭建和测试开发环境
2011-07-03 00:16
801 查看
用Myeclipse 8.5集成开发,可用其自带的集成环境。
选中目录,在菜单栏上单击MyEclipse-->Add Spring Capabilities
在新窗口中选择要集成的Spring版本(这里是3.0)如果还要集成Struts和hibernate的话,可在选择框中把Spring 3.0 AOP Libraries 、Spring 3.0 Core Libraries、Srping 3.0 Persistence Core Libraries、 Spring 3.0 Persistence JDBC Libraries 和Spring 3.0 Web Libraries 选上。
在JAR Library Installation 一栏中选择Copy checked Library contents to project folder后,单击Next> ; 在下一页中选择Specify new or existing Spring bean configuration file?单击Finish。然后MyEclipse 会在src目录下生成一个applicationContext.xml文件。即Spring的配置文件。
开发环境搭建好以后就可以写测试类了。
先New一个接口UsersDao:
再New一个UsersDao的实现接口:
再对Spring的配置文件applicationContext.xml进行配置:
把要交给Sping管理的bean类加入到applicationContext.xml文件中。
注:在bean属性下id和name和区别:他们都是为当前bean取的一个名称,那为什么有id还要提供一个name属性呢,因为id本身就是xml的一个属性,他会受到xml解析器的校验,在此过程中他不能包含各种特殊字符的如:"/",但在有些情况下我们为bean取得的名称要用到一些特殊字符,这时候,我们就要用到name这个属性了。
基本工作已经完成,现在可以对Spring 进行测试了。
先New一个测试JUnit Test Case工具类:
我这用的是JUnit 4
右击项目-->New-->Other-->JUnit/JUnit Test Case在新面板中选择New JUnit 4 Test -->Finish
先实例化Spring容器,通常实例化Sping容器有两种方式:
1、在类路径下寻找配置文件来实例化容器
2、在文件系统目录下寻找配置文件来实例化容器
Spring配置文件可以指定多个,通过数组传入。
在测试工具类中写入:
在Outline视图下右击test方法-->Run As-->JUnit Test。
这时如果在控制台上输出“开发环境测试成功!”即可。
选中目录,在菜单栏上单击MyEclipse-->Add Spring Capabilities
在新窗口中选择要集成的Spring版本(这里是3.0)如果还要集成Struts和hibernate的话,可在选择框中把Spring 3.0 AOP Libraries 、Spring 3.0 Core Libraries、Srping 3.0 Persistence Core Libraries、 Spring 3.0 Persistence JDBC Libraries 和Spring 3.0 Web Libraries 选上。
在JAR Library Installation 一栏中选择Copy checked Library contents to project folder后,单击Next> ; 在下一页中选择Specify new or existing Spring bean configuration file?单击Finish。然后MyEclipse 会在src目录下生成一个applicationContext.xml文件。即Spring的配置文件。
开发环境搭建好以后就可以写测试类了。
先New一个接口UsersDao:
package com.lz.service; public interface UsersDao { public void save(); }
再New一个UsersDao的实现接口:
package com.lz.service.imp; import com.lz.service.UsersDao; public class UserImpDao implements UsersDao { public void save() { System.out.println("开发环境测试成功!"); } }
再对Spring的配置文件applicationContext.xml进行配置:
把要交给Sping管理的bean类加入到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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="userImpDao" class="com.lz.service.imp.UserImpDao"></bean> </beans>
注:在bean属性下id和name和区别:他们都是为当前bean取的一个名称,那为什么有id还要提供一个name属性呢,因为id本身就是xml的一个属性,他会受到xml解析器的校验,在此过程中他不能包含各种特殊字符的如:"/",但在有些情况下我们为bean取得的名称要用到一些特殊字符,这时候,我们就要用到name这个属性了。
基本工作已经完成,现在可以对Spring 进行测试了。
先New一个测试JUnit Test Case工具类:
我这用的是JUnit 4
右击项目-->New-->Other-->JUnit/JUnit Test Case在新面板中选择New JUnit 4 Test -->Finish
先实例化Spring容器,通常实例化Sping容器有两种方式:
1、在类路径下寻找配置文件来实例化容器
ApplicationContext act = new ClassPathXmlApplicationContext(String[]{"applicationContext.xml"});
2、在文件系统目录下寻找配置文件来实例化容器
ApplicationContext act = new ClassPathXmlApplicationContext(String[]{"d:\\applicationContext.xml"});
Spring配置文件可以指定多个,通过数组传入。
在测试工具类中写入:
package com.lz.test; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lz.service.UsersDao; public class UserTest { private static UsersDao usersDao; @BeforeClass public static void setUpBeforeClass() throws Exception { ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml"); usersDao =(UsersDao)act.getBean("userImpDao"); } @Test public void test(){ usersDao.save(); } }
在Outline视图下右击test方法-->Run As-->JUnit Test。
这时如果在控制台上输出“开发环境测试成功!”即可。
相关文章推荐
- Spring学习笔记(一)环境搭建和测试开发环境
- ITCAST视频-Spring学习笔记(搭建与测试Spring的开发环境)
- spring2.5.6学习笔记三:搭建与测试Spring的开发环境
- spring学习笔记整理--02(搭建与测试Spring的开发环境)
- Java学习笔记38:通过Spring Bean 注入static变量,来设计一套适合测试,开发,生产环境的配置项
- Java框架学习笔记——Spring开发环境搭建
- Spring学习笔记02---搭建开发环境
- Spring 学习笔记2 Spring开发环境搭建
- (2)搭建与测试 Spring 的开发环境
- [maven学习笔记]第一节,认识maven,搭建maven开发环境,写第一个HelloWorld
- ES6 学习笔记 (2)-- Liunx环境安装Node.js 与 搭建 Node.js 开发环境
- Android 学习笔记2:搭建开发环境
- Hadoop学习笔记(4) ——搭建开发环境及编写Hello World
- Spring学习心得(1)-- 简单开发环境搭建
- 【深度学习笔记】Anaconda及开发环境搭建
- 搭建与测试Spring的开发环境_容器常用的两种方式
- XMPP协议学习笔记六(搭建Tigase开发环境)
- Android开发学习笔记(三)——基于Eclipse开发环境的搭建及HelloAndroid示例程序的创建
- 第97讲:使用SBT开发Akka第一个案例环境搭建详解学习笔记
- 我的嵌入式学习笔记(1)-- 搭建开发环境