spring-在web应用中使用spring-test与junit整合

一、创建项目
    项目名称:spring100806
二、在项目中添加spring 支持
    1.在lib目录下添加jar包
        commons-logging.jar
        junit-4.10.jar
        log4j.jar
        spring-beans-3.2.0.RELEASE.jar
        spring-context-3.2.0.RELEASE.jar
        spring-core-3.2.0.RELEASE.jar
        spring-expression-3.2.0.RELEASE.jar
        spring-web-3.2.0.RELEASE.jar
        spring-test-3.2.0.RELEASE.jar
三、在项目中添加配置文件
    1.在项目中创建conf目录
        /conf
    2.在conf目录添加配置文件
        配置文件名称: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:context="http://www.springframework.org/schema/context"
               xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        </beans>
四、在web.xml文件中添加配置信息
    <!-- 指定配置文件路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
    <!-- 配置监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
四、相关bean的创建
    1.在src目录下创建bean
        包名:cn.jbit.spring100806.domain
        类名:Teacher.java
        类中内容:
        @Component("teacher")
        public class Teacher {
            public void sayHello(){
                System.out.println("hello");
            }
        }
    2.在配置文件中配置注解扫描和注解解析
        <!-- 通知spring解析常用的注解 -->
        <context:annotation-config/>
        <!-- 通知spring应用注解bean所在位置 -->
        <context:component-scan base-package="cn.jbit.spring100806.domain"></context:component-scan>
五、测试
    1.在项目中创建test目录
        /test
    2.在test目录下创建测试包
        包名:cn.jbit.spring100806.domain    
    3.在包下创建测试类
        类名:TeacherTest.java
        内容:
        @RunWith(SpringJUnit4ClassRunner.class)
        @ContextConfiguration("classpath:applicationContext.xml")
        public class TeacherTest {
            private Teacher teacher;
            @Test
            public void testSayHello(){
                teacher.sayHello();
            }
            @Resource(name="teacher")
            public void setTeacher(Teacher teacher) {
                this.teacher = teacher;
            }
            public Teacher getTeacher() {
                return teacher;
            }
        }

时间: 2024-08-03 00:37:07

spring-在web应用中使用spring-test与junit整合的相关文章

06_在web项目中集成Spring

在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService helloService = (HelloService) applicationContext.getBean("helloS

web项目中 集合Spring&amp;使用junit4测试Spring

web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService helloService = (HelloService) applicationContext.getBean("helloService"); helloService.sayHello(

web 工程中利用Spring的 ApplicationContextAware接口自动注入bean

最常用的办法就是用 ClassPathXmlApplicationContext, FileSystemClassPathXmlApplicationContext, FileSystemXmlApplicationContext 等对象去加载Spring配置文件,这样做也是可以, 但是在加载Spring配置文件的时候,就会生成一个新的ApplicaitonContext对象而不是Spring容器帮我们生成的哪一个, 这样就产生了冗余, 所以不采用应用程序手动加载文件的方式,而是使用Applic

如何在Web项目中配置Spring MVC

要使用Spring MVC需要在Web项目配置文件中web.xml中配置Spring MVC的前端控制器DispatchServlet 1 <servlet> 2 <servlet-name>SpringMVC</servlet-name> 3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 4 <!-- 默认所对应的配置文件是

在完全由Spring管理的环境中使用Spring的Context获取Bean实例

在大型的应用中,常常会有很灵活的需求,而在使用了框架之后,虽然可以大大提高开发的效率,但同时,也把我们框到一个架子中了. 下面先说一下我遇到的问题,事情大概是这样的: @Component @Scope("prototype") public class Action1 implements Action{ ..... } @Component @Scope("prototype") public class Action2 implements Action{ .

Spring在Web应用中使用的原理

那Spring如何在web应用中使用 ①加入Spring web应用的特定jar包spring-web-4.0.0.RELEASE.jar.spring-webmvc-4.0.0.RELEASE.jar ②添加Spring的配置文件----跟Java工程没有什么不一样 ③如何实例化IOC容器 I. 如果在非 WEB 应用在 main 方法中直接创建 II 如果自web应用的话应该在 WEB 应用被服务器加载时就创建 IOC 容器 那问题又来了,我怎么知道WEB应用什么时候被服务器加载呢? 实际上

Web开发中获取Spring的ApplicationContext的三种方式

在 WEB 开发中,可能会很少需要显示的获得 ApplicationContext 来得到由 Spring 进行管理的某些 Bean, 今天我就遇到了,在这里和大家分享一下, WEB 开发中,怎么获取 ApplicationContext 一       要想怎么获取 ApplicationContext, 首先必须明白 Spring 内部 ApplicationContext 是怎样存储的.下面我们来跟踪一下源码 首先:从大家最熟悉的地方开始 Java代码   <listener> <

spring在web.xml中的配置

在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制,自动加载的容器中去,在web项目中,配置文件加载到web容器中进行解析,目前,spring提供了两种加载器,以供web容器的加载:一种是ContextLoaderListener,另一种是ContextLoaderServlet.这两种在功能上完全相同,只是一种是基于Servlet2.3版本中新引入的Listener接口实现,而另一种是基于Servlet接口实现,以下是这两种加载器在w

综合-----Web工程中启动Spring的三种方法

一.利用Spring 自带的Listener(推荐) 在web.xml中配置如下 <context-param> <!-- 名字固定 --> <param-name>contextConfigLocation</param-name> <!-- 值为Spring配置文件的路径 --> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> <

Spring 在web 容器中的启动过程

1.对于一个web 应用,其部署在web 容器中,web 容器提供其一个全局的上下文环境,这个上下文就是 ServletContext ,其后面的spring IoC 容器提供宿主环境 2.在web.xml 中会提供有 contextLoaderListener.在web 容器启动时,会触发容器初始化事件,此时 contextLoaderListener 会监听到这个事件,其 contextInitialized 方法会被调用,在这个方法中,spring 会初始化一个启动上下文,这个上下文就被称