1.錯誤一:ObjectDefinitionStoreException
“Spring.Objects.Factory.ObjectDefinitionStoreException”類型的未經處理的異常在 Spring.Core.dll 中發生
通過看詳細信息,原來是找不到文件
未能找到文件“F:\20160221\Demo\CPrj\bin\Debug\objects.xml”。
處理方式:將objects.xml的【復制到輸出目錄】設置為始終復制
2.錯誤二:annotLoadObjectTypeException
“Spring.Core.CannotLoadObjectTypeException”類型的未經處理的異常在 Spring.Core.dll 中發生
通過看詳細信息,原來是找不到對象的定義
Cannot resolve type [Person] for object with name 'Person' defined in file [F:\20160221\Demo\CPrj\bin\Debug\objects.xml] line 3
處理方式
<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net"> <object id="Person" type="CPrj.Person,Cprj"></object> </objects>
3.錯誤三:ConfigurationErrorsException
“System.Configuration.ConfigurationErrorsException”類型的未經處理的異常在 Spring.Core.dll 中發生
查看詳細信息,原來是找不到配置文件(obejcts拼錯了)
Error creating context 'spring.root': 未能找到文件“F:\20160221\Demo\CPrj\bin\Debug\obejcts.xml”。
處理方式
<spring> <context> <resource uri="file://objects.xml"></resource> </context> </spring>
4.錯誤四:TypeInitializationException
“System.TypeInitializationException”類型的未經處理的異常在 CPrj.exe 中發生
查看詳細信息
“Spring.Context.Support.ContextRegistry”的類型初始值設定項引發異常。
處理方式:把<startup>放到最后一個節點
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"></section> </sectionGroup> </configSections> <spring> <context> <resource uri="file://objects.xml"></resource> </context> </spring> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
5.使用屬性注入時,報了如下錯
Error creating context 'spring.root': 'MovieFinder' node cannot be resolved for the specified context [Spring.Examples.MovieFinder.MovieLister
意思是說,在MovieLister類中,找不到屬性名為MovieFinder項
看了下配置文件,如下
<objects xmlns="http://www.springframework.net"> <object name="MyMovieLister" type="Spring.Examples.MovieFinder.MovieLister,Spring.Examples.MovieFinder"> <property name="MovieFinder" ref="MyMovieFinder"></property> </object> <object name="MyMovieFinder" type="Spring.Examples.MovieFinder.MovieFinder,Spring.Examples.MovieFinder"></object> <!--<description>An example that demonstrates simple Ioc features</description>--> </objects>
再看下MovieLister類
public class MovieLister { private MovieFinder finder; public List<Movie> MoviesDirectedBy() { List<Movie> allMovies = finder.FindAll(); return allMovies; } }
發現問題了,不是MovieFinder,是finder;配置文件改成如下,就OK了
<objects xmlns="http://www.springframework.net"> <object name="MyMovieLister" type="Spring.Examples.MovieFinder.MovieLister,Spring.Examples.MovieFinder"> <property name="finder" ref="MyMovieFinder"></property> </object> <object name="MyMovieFinder" type="Spring.Examples.MovieFinder.MovieFinder,Spring.Examples.MovieFinder"></object> <!--<description>An example that demonstrates simple Ioc features</description>--> </objects>
6.在構造函數注入是報如下錯
Error creating context 'spring.root': Cannot instantiate a class that does not have a no-argument constructor [Spring.Examples.MovieFinder.ColonMovieFinder].
原來Spring容器默認在加載的時候,都會嘗試預先創建對象。
處理方式一:給ColonMovieFinder一個無參數的構造函數
處理方式二:在配置文件中提供相應的構造函數參數
<objects xmlns="http://www.springframework.net"> <object name="MyMovieLister" type="Spring.Examples.MovieFinder.MovieLister,Spring.Examples.MovieFinder"> <property name="finder" ref="MyMovieFinder"></property> </object> <object name="MyMovieFinder" type="Spring.Examples.MovieFinder.MovieFinder,Spring.Examples.MovieFinder"></object> <object name="AnotherMovieFinder" type="Spring.Examples.MovieFinder.ColonMovieFinder,Spring.Examples.MovieFinder"> <constructor-arg index="0" value="movies.txt"></constructor-arg> <constructor-arg index="1" value="arg2"></constructor-arg> </object> <!--<description>An example that demonstrates simple Ioc features</description>--> </objects>
7.xml錯誤
錯誤代碼如下
Error creating context 'spring.root': Unable to locate Spring NamespaceHandler for XML schema namespace []
看了下xml
<objects> <object name="person" type="SpringNetSetDi.Person,SpringNetSetDi"> <property name="RealName" value=""></property> </object> <object name="animal" type="SpringNetSetDi.Animal,SpringNetSetDi"> <property name="TypeList"> <list element-type="string"> <value>哺乳類</value> <value>鳥類</value> <value>爬行類</value> <value>昆蟲類</value> <value>兩棲類</value> </list> </property> </object> </objects>
處理方式,objects處加上屬性
<objects xmlns="http://www.springframework.net"> <object name="person" type="SpringNetSetDi.Person,SpringNetSetDi"> <property name="RealName" value=""></property> </object> <object name="animal" type="SpringNetSetDi.Animal,SpringNetSetDi"> <property name="TypeList"> <list element-type="string"> <value>哺乳類</value> <value>鳥類</value> <value>爬行類</value> <value>昆蟲類</value> <value>兩棲類</value> </list> </property> </object> </objects>
8.方法注入時,掉進的坑
事情來源,以下怎么改代碼,方法都不能被替換
<object name="replaceValue" type="SpringNetMethodDi.SuperMarket,SpringNetMethodDi"></object> <object name="realOp" type="SpringNetMethodDi.RealOp,SpringNetMethodDi"> <replaced-method name="Buy" replacer="replaceValue"> <arg-type match="string"/> </replaced-method> </object>
以下代碼是可以運行的,發現不同了沒
<object name="replaceValue" type="SpringNetMethodDi.SuperMarket,SpringNetMethodDi"></object> <object name="realOp" type="SpringNetMethodDi.RealOp,SpringNetMethodDi"> <replaced-method name="Buy" replacer="replaceValue"> <arg-type match="String"/> </replaced-method> </object>
不同之處,竟然是string和String
