码迷,mamicode.com
首页 > 编程语言 > 详细

SSH整合之二:添加Spring环境

时间:2015-03-20 16:07:07      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

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

SSH整合之二:添加Spring环境

标签:

原文地址:http://www.cnblogs.com/clear5/p/4353745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!