• SSH整合之二:添加Spring环境


    2.添加spring-framework-3.1.1.RELEASE环境:

      1)将下载的spring环境解压后,将dist下面的jar包拷到项目lib文件夹下面。

      2)添加applicationContext.xml文件至项目config下,内容如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!--
     3   - Middle tier application context definition for the image database.
     4   -->
     5 <beans xmlns="http://www.springframework.org/schema/beans"
     6         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     7         xmlns:context="http://www.springframework.org/schema/context"
     8         xmlns:tx="http://www.springframework.org/schema/tx"
     9         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    10                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    11                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    12 
    13     <!-- 自动扫描与装配bean -->
    14     <context:component-scan base-package="cn.clear.web"></context:component-scan>
    15     
    16 </beans>

      3)点击项目名右键---》Build Path ---》Add Libraries ---》选择JUnit ---》点击Next ---》选择JUnit4 ---》Finish。添加了JUnit环境。

      4)在cn.clear.web.test下创建SpringTest.java,具体如下:

     1 package cn.clear.web.test;
     2 
     3 import org.junit.Test;
     4 import org.springframework.context.ApplicationContext;
     5 import org.springframework.context.support.ClassPathXmlApplicationContext;
     6 
     7 public class SpringTest {
     8     
     9     @Test
    10     public void testSpring(){
    11         
    12         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    13         
    14         ActionTest actionTest = (ActionTest) ac.getBean("actionTest");
    15         System.out.println(actionTest);
    16     }
    17 }

      5)在ActionTest.java类上添加注解:

      @Controller //控制层注解,在类上添加此注解便于spring管理进行依赖注入。

      @Scope("prototype") //每次对bean的请求都会创建一个新的bean实例。

      6)在SpringTest.java中,选中方法名testSpring右击Runs ---》 JUnit Test,如果结果显示绿条,控制台输出信息,则Spring环境添加成功:

      

      控制台输出:cn.clear.web.test.ActionTest@27b4c1d7

  • 相关阅读:
    js禁用回退键
    css和js引用图片路径
    js 文字横向滚动
    数组转换
    Vbox共享串口
    office2003 打开docx文件
    注销退出客户点击回退怎么办
    vs2010 安装mvc3
    修改头像
    iis发布网站局域网无法访问
  • 原文地址:https://www.cnblogs.com/clear5/p/4353745.html
Copyright © 2020-2023  润新知