一,配置文件進行Spring初始化
1,配置文件編寫
<?xml version="1.0" encoding="utf-8" ?> <configuration>
/************* 這里會報異常“Spring.Context.Support.ContextRegistry”的類型初始值設定項引發異常。把配置注釋掉就行了 <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> </startup>
***************/ <configSections>
//初始化Spring <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" /> <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> </sectionGroup> </configSections>
//Spring配置節點 <spring> <context> <resource uri="config://spring/objects" /> </context> <objects xmlns="http://www.springframework.net"> <description>人類</description> <object id="Model" type="Model.Person, Model" /> </objects> </spring> </configuration>
2,初始化代碼
異常:No object named 'Person' is defined : Cannot find definition for object [Person]
static void Main(string[] args) { IApplicationContext ctx = ContextRegistry.GetContext(); IPerson dao = ctx.GetObject("Person") as IPerson; }
這里報異常是因為在配置文件中沒有找到 <object id="Person" type="Model.Person, Model" /> 的節點
需在項目中增加Spring.Core.dll的引用
二,使用Xml文件Spring初始化
(1)具體文件的初始化
編寫Objects.xml
<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd"> <object id="Person" type="Model.Person, Model"> </object> </objects>
后台初始化
private static void ReadFromXml() { IResource input = new FileSystemResource("Objects.xml"); //路徑----這里使用的是相對路徑,也可以使用絕對路徑,如果路徑錯了會報異常 IObjectFactory factory = new XmlObjectFactory(input); IPerson person = factory.GetObject("Person") as IPerson;
person.Speak("hello"); }
(2)程序集下尋找配置文件
private static void ReadFromMuiltDoc() { //需滿足URI語法。 //file://文件名 //assembly://程序集名/命名空名/文件名 string[] xmlFiles = new string[] { "file://Objects.xml" }; IApplicationContext context = new XmlApplicationContext(xmlFiles); IObjectFactory factory = (IObjectFactory)context; IPerson person = factory.GetObject("Person") as IPerson; person.Speak("多文件初始化"); }
這種方式相對靈活
(3)在配置文件App.config或Web.config添加自定義配置節點
在配置文件中要引入<objects xmlns="http://www.springframework.net"/>命名空間,否則程序將會無法實例化Spring.NET容器。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" /> <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> </sectionGroup> </configSections> <spring> <context> <resource uri="assembly://FirstSpringNetApp/FirstSpringNetApp/Objects.xml"/> <resource uri="config://spring/objects" /> </context> <objects xmlns="http://www.springframework.net"/> <!--必要--> </spring>
這里<context>中的type是可選的
通過構造器創建對象
<object id="exampleObject" type="Examples.ExampleObject, ExamplesLibrary"/>
type屬性的格式:類型的全名,然后是一個逗號,最后是類型所在的程序集名稱。
如果需要為嵌套類型創建對象,可以使用+號。例如,如果在類型Examples.ExampleObject嵌套定義了類型Person
<object id="exampleObject" type="Examples.ExampleObject+Person, ExamplesLibrary"/>
通過靜態工廠方法創建對象
下面的對象就是通過靜態工廠方法創建的。注意:對象定義中的type並非是要創建的對象的類型,而是包含了工廠方法的類型;同時,CreateInstance必須是靜態方法。
<object id="exampleObject"
type="Examples.ExampleObjectFactory, ExamplesLibrary"
factory-method="CreateInstance"/>
通過實例工廠方法創建對象
如果要通過實例工廠方法創建對象,對象定義就不能包含type屬性,而要用factory-object屬性引用工廠方法所在的對象;注意,該屬性值必須是包含工廠方法的對象的名稱,且該對象必須定義在當前容器或父容器中。工廠方法的方法名則通過factory-method屬性指定。
<!-- the factory object, which contains an instance method called 'CreateInstance' --> <object id="exampleFactory" type="..."/> <!-- the object that is to be created by the factory object --> <object id="exampleObject" factory-method="CreateInstance" factory-object="exampleFactory"/>