首页 > 学院 > 开发设计 > 正文

SSH整合之二:添加Spring环境

2019-11-14 23:23:39
字体:
来源:转载
供稿:网友
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.xsd10                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd11                 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     @Test10     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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表