




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
Spring簡單用例所需jarSpring配置問題注意一:注入beanXML文件源碼(文件的標頭)<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd"> <beanid="hello"class=".HelloWord"></bean> <beanid="student"class=".Student"></bean> <beanid="studentService"class=".StudentService"> <propertyname="jk"ref="student"></property> </bean></beans>配置中的ref指定的是Bean注意二:屬性注入屬性注冊,順序可改變注意三:重寫toString方法使用直接輸出時需要重寫toString方法。注意四:構(gòu)造方法注入之類型注入?yún)?shù)的順序不可以改變;此處有個幾個參數(shù)就找?guī)讉€參數(shù)的構(gòu)造方法Exceptioninthread"main"org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'peoper3'definedinclasspathresource[beans.xml]:Couldnotresolvematchingconstructor(hint:specifyindex/type/nameargumentsforsimpleparameterstoavoidtypeambiguities)如報此錯誤是:類中沒有有參構(gòu)造方法!注意五:構(gòu)造方法注入控制臺錯誤及原因Exceptioninthread"main"org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'peoper'definedinclasspathresource[beans.xml]:Instantiationofbeanfailed;nestedexceptionisorg.springframework.beans.BeanInstantiationException:Couldnotinstantiatebeanclass[com.zx.entity.Peoper]:Nodefaultconstructorfound;nestedexceptionisjava.lang.NoSuchMethodException:com.zx.entity.Peoper.<init>()原因是缺少無參構(gòu)造方法注意六:構(gòu)造方法注入之順序注入注意:順序從0開始,順序注入可添加類型格式。注意七:工廠方法注入之動態(tài)注入創(chuàng)建工廠類及方法。 結(jié)果:注意八:工廠方法注入之靜態(tài)注入注意九:注入?yún)?shù)之bean間的嵌套(注入bean)注意十:注入?yún)?shù)之內(nèi)部bean注意十一:集合注入ListSetMap系統(tǒng)配置propertiesSpring自動裝配通過配置default-autowire屬性,SpringIOC容器可以自動為程序注入bean;默認default,no,不啟動自動裝配按照名稱裝配注意一:按照名稱裝配必須跟類中的名稱匹配起來按照類型裝配注意一:按照類型裝配問題:原因是存在兩個類型一樣的!注意二:按類型裝配(id問題)此處的id可不與類中的名一致!按照構(gòu)造方法裝配注意一:按構(gòu)造方法裝配(id問題)此處的id可不與類中的名一致!方法注入Springbean作用默認是單例,通過配置prototype實現(xiàn)多利方法注入lookup-methodBean之間的關(guān)系繼承Parent屬性依賴未添加依賴:添加依賴:依賴屬性depends-on引用如spring配置注意九Bean的作用范圍參數(shù):scope默認singletonSpringioc容器中僅有一個bean實例,bean以單例的方式存在Prototype每次從容器中調(diào)用Bean時,都產(chǎn)生一個新的實例。SpringAOP實例實例一:前置通知ServiceAspect接口及實現(xiàn)類ServiceAspectIplPeopleService接口及實現(xiàn)類PeopleServiceIpl<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:aop="/schema/aop"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd/schema/aop/schema/aop/spring-aop-2.0.xsd"> <beanid="peopleService"class="com.zx.service.ipl.PeopleServiceIpl"></bean> <beanid="serviceAspect"class="com.zx.service.ipl.ServiceAspectIpl"></bean> <!-- 設(shè)置千面切入點--> <aop:config> <!-- 將serviceAspect切入進peopleService--> <aop:aspectid="serviceAspect"ref="serviceAspect"> <aop:pointcutexpression="execution(*com.zx.service.*.*(..))"id="aspect"/> <aop:beforemethod="add"pointcut-ref="aspect"/> </aop:aspect> </aop:config></beans>啟動報錯及原因原因:缺少jar包AOP所需的jar包獲取切點的類及方法名后置通知環(huán)繞通知異常通知Spring對JDBC的支持Xml標頭引入命名空間:<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="/schema/beans" xmlns:context="/schema/context"xmlns:xsi="/2001/XMLSchema-instance"xmlns:aop="/schema/aop"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd/schema/aop/schema/aop/spring-aop-2.0.xsd/schema/context/schema/context/spring-context-2.5.xsd"></beans>數(shù)據(jù)源代碼XML文件代碼:<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="/schema/beans" xmlns:context="/schema/context"xmlns:xsi="/2001/XMLSchema-instance"xmlns:aop="/schema/aop"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd/schema/aop/schema/aop/spring-aop-2.0.xsd/schema/context/schema/context/spring-context-2.5.xsd"><!--加載指定路徑properties--><context:property-placeholderlocation="classpath:perties"/> <!--數(shù)據(jù)源--><beanid="dataSource"class="mons.dbcp.BasicDataSource"destroy-method="close"><propertyname="driverClassName"value="${driverClassName}"/><propertyname="url"value="${url}"/><propertyname="username"value="${username}"/><propertyname="password"value="${password}"/></bean></beans>此處用到了dbcp的連接池!數(shù)據(jù)源代碼所需要的jar包數(shù)據(jù)源鏈接driverClassName=oracle.jdbc.driver.OracleDriverurl=jdbc:oracle:thin:@localhost:1521:ORCLusername=scottpassword=tiger數(shù)據(jù)源鏈接所需的jar包添加oracle驅(qū)動jdbc所用到的包使用Jdbctemplate對象來完成jdbc操作<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="/schema/beans" xmlns:context="/schema/context"xmlns:xsi="/2001/XMLSchema-instance"xmlns:aop="/schema/aop"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd/schema/aop/schema/aop/spring-aop-2.0.xsd/schema/context/schema/context/spring-context-2.5.xsd"><!--加載指定路徑properties--><context:property-placeholderlocation="classpath:perties"/> <!--數(shù)據(jù)源--><beanid="dataSource"class="mons.dbcp.BasicDataSource"destroy-method="close"><propertyname="driverClassName"value="${driverClassName}"/><propertyname="url"value="${url}"/><propertyname="username"value="${username}"/><propertyname="password"value="${password}"/></bean><!--使用Jdbctemplate對象來完成jdbc操作--><beanid="jdbcTemplate"class
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權(quán)】 IEC 62841-4-8:2025 EN-FR Electric motor-operated hand-held tools,transportable tools and lawn and garden machinery - Safety - Part 4-8: Particular requirements for shredder
- 【正版授權(quán)】 IEC 60335-2-40:2024 EXV EN Household and similar electrical appliances - Safety - Part 2-40: Particular requirements for electrical heat pumps,air-conditioners and dehumidi
- 汽車行業(yè)新車質(zhì)量保修免責合同
- 城市交通設(shè)施建設(shè)合同
- 個人對個人協(xié)議書
- 醫(yī)療信息化系統(tǒng)建設(shè)協(xié)議
- 前臺文員個人年終工作總結(jié)
- 勞務(wù)分包合同履約擔保
- LED照明產(chǎn)品研發(fā)合作協(xié)議
- 股東內(nèi)部承包經(jīng)營協(xié)議書
- 中國地方政府融資平臺行業(yè)市場深度分析及投資前景展望報告
- 2025年廣東中考物理學(xué)科模擬試卷(廣東專屬)
- 光伏安全施工方案范本
- 2025上半年江蘇省南通如東事業(yè)單位招聘7人易考易錯模擬試題(共500題)試卷后附參考答案
- 山東省濟南市2024-2024學(xué)年高三上學(xué)期1月期末考試 地理 含答案
- 2025年湘教版二年級美術(shù)下冊計劃與教案
- 2024-2030年中國自動光學(xué)檢測儀(AOI)市場競爭格局與前景發(fā)展策略分析報告
- 2024-2025學(xué)年人教版數(shù)學(xué)八年級上冊期末模擬試卷
- 《智能制造單元集成應(yīng)用》課件-數(shù)控機床參數(shù)備份與恢復(fù)
- 中學(xué)學(xué)校2024-2025學(xué)年工作計劃
- 四川省成都市2023年中考道德與法治真題試卷(含答案)
評論
0/150
提交評論