小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Spring.NET教程(二)

 黃金屋1 2019-05-26

用VS2015新建一個基于Console的Spring.Net應(yīng)用程序,在菜單欄中選擇 項目--管理NuGet程序包。選擇瀏覽,搜索Spring.net,會出現(xiàn)很多關(guān)于Spring.Net的包。

              選擇Spring.NET框架經(jīng)常用到的以下幾個文件:

              Common.Logging.dll(必要)

              Spring.Core.dll(必要)

              Spring.Data.dll

              Spring.Aop.dll(可選)

              Spring.Data.NHibernate21.dll

              Spring.Web.dll

 

              在基于XML的工廠中,這些對象定義表現(xiàn)為一個或多個<object>子節(jié)點,它們的父節(jié)點必須是<objects> (按:objects節(jié)點的xmlns元素是必需的,必須根據(jù)不同的應(yīng)用添加不同的命名空間,以便有IDE的智能提示(見Spring.NET手冊)。

http://www./doc-latest/reference/html/index.html

Object.XML

  1. <objects xmlns="http://www.">
  2. <object id="" type="">
  3. </object>
  4. <object id="." type="">
  5. </object>
  6. </objects>

新建一個Objects.xml的文件,然后從Spring.NET手冊中復(fù)制來一段配置模板

Objects.xml

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <objects xmlns="http://www.">
  3. <object id="PersonDao" type="Dao.PersonDao, Dao">
  4. </object>
  5. </objects>

          實例化Spring.NET容量(兩種方式程序讀、appconfig設(shè)置):

              1)在程序集下尋找配置xml文件。

  1. string appPath = System.AppDomain.CurrentDomain.BaseDirectory;
  2. string[] xmlFiles = new string[]
  3. {
  4. Path.Combine(appPath, @"Objects.xml")
  5. //"file://Objects.xml"
  6. //"assembly://SpringNet/SpringNet/Objects.xml"
  7. };
  8. IApplicationContext context = new XmlApplicationContext(xmlFiles);
  9. IPersonDao dao = (IPersonDao)context.GetObject("PersonDao");
  10. Console.WriteLine(dao.ToString());
  11. dao.Save();
  12. Console.ReadLine();

       

注意:

Uri的語法:http://www./doc-latest/reference/html/objects.html

File: file:///Objects.xml

Assembly:assembly://<AssemblyName>/<NameSpace>/<ResourceName>

在使用assembly的時候,需要將配置文件的屬性中生成操作設(shè)置為嵌入的資源,復(fù)制到輸出目錄設(shè)置為始終賦值。

               2)還有在配置文件App.config或Web.config添加自定義配置節(jié)點需滿足URI語法

              assembly://程序集名/命名空名/文件名

  在配置文件中要引入<objectsxmlns="http://www."/>命名空間,否則程序?qū)o法實例化Spring.NET容器。

  App.config

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <configSections>
  4. <sectionGroup name="spring">
  5. <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
  6. <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
  7. </sectionGroup>
  8. </configSections>
  9. <spring>
  10. <context>
  11. <resource uri="assembly://SpringNet/SpringNet/Objects.xml"/>
  12. <resource uri="config://spring/objects"/>
  13. </context>
  14. <objects xmlns="http://www."/>
  15. </spring>
  16. <startup>
  17. <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  18. </startup>
  19. </configuration> 

代碼:

  1.     IApplicationContext ctx = ContextRegistry.GetContext();
  2. IPersonDao dao = (IPersonDao)ctx.GetObject("PersonDao");
  3. Console.WriteLine(dao.ToString());
  4. dao.Save();
  5. Console.ReadLine();

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多