2.添加spring-framework-3.1.1.RELEASE環(huán)境:
1)將下載的spring環(huán)境解壓后,將dist下面的jar包拷到項目lib文件夾下面。
2)添加applicationContext.xml文件至項目config下,內(nèi)容如下:
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環(huán)境。
4)在cn.clear.web.test下創(chuàng)建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的請求都會創(chuàng)建一個新的bean實例。
6)在SpringTest.java中,選中方法名testSpring右擊Runs ---》 JUnit Test,如果結(jié)果顯示綠條,控制臺輸出信息,則Spring環(huán)境添加成功:

控制臺輸出:cn.clear.web.test.ActionTest@27b4c1d7
新聞熱點
疑難解答