Spring学习笔记(一)环境搭建和测试开发环境
来源:互联网 发布:如果明朝没有灭亡知乎 编辑:程序博客网 时间:2024/05/21 17:32
用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:
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学习笔记(一)环境搭建和测试开发环境
- Spring学习笔记(一)环境搭建和测试开发环境
- Spring MVC 3学习笔记+教程(一)开发环境搭建和HelloWorld程序
- Spring MVC 3学习笔记+教程(一)开发环境搭建和HelloWorld程序
- Spring学习笔记02---搭建开发环境
- libgdx 学习笔记一 开发环境搭建
- Ehcache 学习笔记(一) 搭建开发环境
- libgdx 学习笔记一 开发环境搭建
- ble学习笔记一---------开发环境搭建
- spring自学笔记-搭建与测试Spring的开发环境
- Spring学习笔记(一)Spring环境搭建
- spring2.5.6学习笔记三:搭建与测试Spring的开发环境
- Spring 学习笔记2 Spring开发环境搭建
- Spring学习笔记 搭建环境
- Windows Phone 开发学习笔记(一) 开发环境搭建
- Spring+SpringMVC+MyBaties学习笔记(一)环境搭建
- Spring进阶一:Spring开发环境搭建
- Java框架学习笔记——Spring开发环境搭建
- 通过H5的新标签canvas做出一个时钟的全过程,希望对初学者有帮助
- HDU 3072 Intelligence System(Tarjan+最小树形图)
- Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
- GVIM win7添加到右键菜单方法
- Python 科学计算之实验环境安装及使用
- Spring学习笔记(一)环境搭建和测试开发环境
- 使用ssh 访问越狱iPhone的两种方式
- 对IoC(Inversion of Control)的理解
- Set的获取方式
- python数据库连接操作---cursor()
- 当从svn导入项目之后,编译运行报错时的解决办法
- monkey测试相关命令
- 经典机器学习&数据挖掘算法
- 矩形覆盖java实现