黑馬程式設計師_黎活明_Spring2.5視訊教程-02_搭建與測試Spring的開發環境
黑馬程式設計師_黎活明_Spring2.5視訊教程
01_全面闡釋Spring及其各項功能
02_搭建與測試Spring的開發環境
1.新建java專案
操作步驟:右鍵------New -----Java Project------Project name-----finish |
2.匯入jar包:
操作步驟:右鍵------Build Path-------Configure Build path----Libraries-------AddExternal JARS----- dist\spring.jar lib\jakarta-commons\commons-logging.jar |
3.在專案src下新建配置檔案beans.xml
操作步驟:右鍵-----New ----file------beans.xml |
4.配置beans.xml
操作步驟:Spring2.5.6解壓,從解壓spring-framework-2.5.6的jar包裡,開啟參考手冊.參考手冊的路徑spring-framework-2.5.6\docs\reference\html_single\index.html,CTRL+F查詢<bean---- Cope複製配置程式碼到beans.xml, |
下圖為手冊裡的配置程式碼.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions go here --> </beans>
刪除沒有用的配置如圖:
5.單元測試:
右鍵專案----New-----JUnit Test Case---選擇New JUnit 4 Test------填入包名和name----勾選setUpBeforeClass-------here-------匯入JUnit 4的jar包------OK----finish |
Springtest.java的test方法加入程式碼:
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
執行單元測試
我們看到單元測試的結果正常.
6.建立ServiceBean
寫入程式碼:
package cn.itcast.service.impl;
import cn.itcast.service.PersonService;
public class PersonServiceBean implements PersonService {
public void save(){
System.out.println("我是save()方法");
}
}
7.抽取介面:
操作步驟:右鍵PersonServiceBean.java檔案-----Refresh-------Extract interface-----PersonService------勾選save方法-------OK |
介面和實現類要在不同的包下移動介面:
右鍵PersonService.java檔案-----Refresh-------Move-----Service-----OK |
8.在beans.xml配置資訊:
編寫配置資訊時候就無法出現提示資訊,解決方法有兩種:
1.讓機器上網,eclipse會自動從網路上下載schema檔案並快取在硬碟上。
2.手動新增schema檔案,方法如下:
操作步驟:windwos->preferences->xml->xmlcatalog----add-------- 出現的視窗中的Key Type中選擇URI,在location中選"File system"-------------- 然後在spring解壓目錄的spring-framework-2.5.6\dist\resources目錄中選擇spring-beans-2.5.xsd,回到設定視窗的時候不要急著關閉視窗,應把視窗中的Key Type改為Schema location,Key改為http://www.springframework.org/schema/beans/spring-beans-2.5.xsd |
現在beans.xml內容為:
<beanid="personService"class="service.impl.PersonServiceBean"> </bean> //注意:id的personService命名第一個字母的首字母小寫 //如下beans中的程式碼: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="personService" class="service.impl.PersonServiceBean"></bean> </beans> |
Springtest.java程式碼如下:
publicclass Springtest { @BeforeClass publicstaticvoid setUpBeforeClass() throws Exception { } @Test publicvoid test() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService=(PersonService)ctx.getBean("personService"); personService.save(); } } |
選中test()方法進行單元測試成功輸出Save方法