Spring的第一個程式,Spring第一個程式
1.開發的步驟(總概述)
(1).準備jar包
(2).開發HelloWorld程式
(3).在applicationContext.xml中完成配置(xsd-config.html)
(4).啟動容器,從容器中得到bean,調用bean中的方法
2.步驟
(1).要匯入的jar包
spring-core-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
(2).普通的POJO類
package cn.wwh.www.spring.aaa.helloworld;/** *類的作用: * * *@author 一葉扁舟 *@version 1.0 *@建立時間: 2014-9-21 下午03:22:26 */public class HelloWorld {public void show() {System.out.println("Hello World! This is my first Spring!");}}(3)配置helloworld.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.xsd"><!--id:bean的唯一標識 class:類的全限定名:不能是抽象類別,不能是介面. Object obj=Class.forName("cn.wwh.www.spring.aaa.helloworld.HelloWorld").newInstance();--><bean id="helloworld" class="cn.wwh.www.spring.aaa.helloworld.HelloWorld"></bean></beans>
在applicationContex.xml檔案中引用
helloworld.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.xsd"><!-- 引入其他設定檔:預設從當前路徑去找檔案.可以帶上路徑首碼:classpath:表示從classpath路徑去找檔案(推薦)file:表示從磁碟中去找檔案<import resource="file:D:\Users\Administrator\Workspaces\MyEclipse 8.5\SpringExercise1\src\cn\wwh\www\spring\aaa\helloworld\helloworld.xml"/>注意:只有Resource介面才能識別和解析這種首碼:/SpringExercise1/src/cn/wwh/www/spring/aaa/helloworld/helloworld.xml --> <import resource="classpath:cn/wwh/www/spring/aaa/helloworld/helloworld.xml"/> </beans>
(4).測試案例
package cn.wwh.www.spring.aaa.helloworld;import org.junit.Test;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;/** *類的作用: * * *@author 一葉扁舟 *@version 1.0 *@建立時間: 2014-9-21 下午03:25:12 */public class TestHelloWorld {// 1.採用傳統的方式進行方法調用@Testpublic void testOld() {// 建立一個對象HelloWorld hello = new HelloWorld();hello.show();}//採用Spring架構的控制反轉的方式調用@Testpublic void testIoC(){//1.載入applicationContext.xml檔案:從classpath路徑路徑找Resource resource = new ClassPathResource("applicationContext.xml");//2.建立並擷取Spring容器BeanFactory bean = new XmlBeanFactory(resource);//3.擷取指定的bean,(特別要注意這個,getBean中的參數是,hellowrld.xml檔案中bean標籤中的id)HelloWorld hello = (HelloWorld) bean.getBean("helloworld");//4.操作beanhello.show();}}
效果測試圖:
3.注意:1.本人resources檔案夾放在工程目錄下,是和src同一個的目錄,applicationcontext.xml檔案就放在該檔案夾中
2.引入其他設定檔:
預設從當前路徑去找檔案.
可以帶上路徑首碼:
classpath:表示從classpath路徑去找檔案(推薦)
file:表示從磁碟中去找檔案
<import resource="file:E:\Users\Administrator\Workspaces\MyEclipse 8.5
\SpringExercise1\src\cn\wwh\www\spring\aaa\helloworld\helloworld.xml"/>
特別要注意:只有Resource介面才能識別和解析這種首碼:/SpringExercise1/src/cn/wwh/www/spring/aaa/helloworld/helloworld.xml
3.在helloworld.xml檔案中<bean>標籤下:
bean元素的id和name:
最好用id:特殊情況才使用name.
Spring開發中 Bean的 id屬性 ,遵守xml文法ID約束:
id 的命名要滿足XML對ID屬性命名規範 必須以字母開始,可以使用字母、數字、連字號、底線、句話、冒號
使用name屬性,就可以使用很多特殊字元,早期在struts1和spring整合 (現在的SpringMVC)
<bean name="/login" class="....LoginAction" /> name中含有/ ,使用id會報錯
4.HelloWorld程式從Spring的角度看: 為什麼配置一下,Spring就會去建立/管理Bean對象.
(1).通過Resource對象載入設定檔;
(2).解析設定檔,得到bean;
(3).解析bean,id作為bean的名字,class用於反射得到bean的執行個體(Class.forName(className));
注意,這種配置下,所有的bean保證有一個無參數構造器
(4).調用getBean的時候,從容器中返回對象執行個體
結論:就是把代碼從JAVA檔案中轉移到了XML中.
5.getBean方法的三種方法:
(1).按照類型擷取bean:
world=factory.getBean(HelloWorld.class);
要求在spring中只配置一個這種類型的執行個體;
若該類型的類配置了多個<bean/>,就要報錯:--一個類型可以找到多個對象
(2).按照bean的名字擷取bean:
world=(HelloWorld)factory.getBean("hello2");
按照名字拿bean不太安全;
異常:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloWorld' is defined:
在容器中找不到一個<bean/>元素的id或name叫helloWorld.
(3).按照名字和類型:(推薦)
world=factory.getBean("hello", HelloWorld.class);
異常:
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No unique bean of type [cn.itcast.cd.day1._1hello.HelloWorld] is defined:
不唯一的Bean類型錯誤:
struts,spring整合的第一個程式出現以下異常··忘指點
你的spring少了個包,spring要檢測表單資料是否輸入為空白,然後需要調用個方法。然後沒找到方法。就出現錯誤。blog.sina.com.cn/s/blog_4575678801009psk.html 這裡面詳細介紹了這個方法。
spring怎得到已建立的應用程式上下文
如果在 web工程裡,有幾個方法,我覺得這個方法最方便。
建個類,實現spring的ApplicationContextAware 介面,即
public class SpringContextTool implements ApplicationContextAware {
private static ApplicationContext context;
public void setApplicationContext(ApplicationContext acx) {
context = acx;
}
public static ApplicationContext getApplicationContext() {
return context;
}
}
然後在spring中定義這個bean,id隨便給,用的時候直接SpringContextTool.getApplicationContext() 就能拿到了