(一)實體路徑(可以複制)
IResource input = new FileSystemResource(@"self.xml");
IObjectFactory fac = new XmlObjectFactory(input);
(二)程式集方法
url文法:
file://檔案名(複制)
assembly://程式集名/命名空名/檔案名(嵌入)
IApplicationContext context = new XmlApplicationContext("file://self.xml");
IObjectFactory fac = (IObjectFactory)context;
還可以:
IApplicationContext context = new XmlApplicationContext("assembly://SpringSample/SpringSample/self.xml");
(三)配置檔案
app.config 或web.config檔案内容或無,xml檔案嵌入
app.config檔案
<?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://SpringSample/SpringSample/self.xml"/>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net">
</objects>
</spring>
</configuration>
Self.xml檔案
<objects xmlns="http://www.springframework.net">
<object id="Customer" name="Customer" type="SpringSample.Customer"></object>
</objects>
IApplicationContext context = ContextRegistry.GetContext();
(context.GetObject("Customer") as Customer).ShowMessage();
部落格園大道至簡
<a href="http://www.cnblogs.com/jams742003/" target="_blank">http://www.cnblogs.com/jams742003/</a>
轉載請注明:部落格園