zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    关于android 代码生成布局中遇到的一些问题
    关于选择移动开发平台(android,ios,wp7)的一些看法
    如何成为一个C++高级程序员(转帖)
    最新Windows平台下Android源码的下载(转+原)
    峨眉之巅放歌
    孝感人间
    迁芸(帮客户名字作诗)
    载春
    杨美花(帮客户名字作诗)
    人生几度温泉夜
  • 原文地址:https://www.cnblogs.com/clear5/p/4353745.html
Copyright © 2011-2022 走看看