zoukankan      html  css  js  c++  java
  • Spring.Net 初探之牛刀小试

    又是一个周末,感受着外面30°的高温,果断宅在家里,闲来无事,就研究了一下spring .net 框架, 在这里不得不说 vs2013确实是一个强大的开发工具(起码对于.net开发来说是这样的),哈哈 废话不多说了开始展示一下我的成果吧.

    1、  项目采用多层架构 IRepository/Repository/Service/Web层,而spring.net主要安装在service层,这样做的目的就是使注入和web层分开,使代码逻辑更加清晰,而在web层只需根据service层的类进行静态调用即可。项目架构如图所示:

    在此之前需要在webconfig中配置spring.xml目录,代码如下所示:

    1   <!--Spring.Net节点详细配置-->
    2   <spring>
    3     <context>
    4       <!--读取嵌入在程序集中的配置文件-->
    5       <!--<resource uri="file://~/Config/Spring.xml"/>-->
    6       <!--读取嵌入在程序集中的配置文件 将Spring.xml的属性设置为【嵌入的资源】-->
    7       <resource uri="assembly://Spring.Net.Service/Spring.Net.Service.Config/Spring.xml"/>
    8     </context>
    9   </spring>

    下面就以BookInfo类为例贴出代码

    1、IBookInfo接口类定义代码,定义函数

    1 namespace Spring.Net.IRepository
    2 {
    3     public interface IBookInfo
    4     {
    5         string GetBook();
    6     }
    7 }
    View Code

    2、BookInfo实现类代码

     1 namespace Spring.Net.Repository
     2 {
     3     public class BookInfo : IBookInfo
     4     {
     5         public string GetBook()
     6         {
     7             return "Hello World!";
     8         }
     9     }
    10 }
    View Code

    3、BookService逻辑处理类代码,在该类中用到了依赖注入技术,将BookInfo注入到Service中,需在Spring.xml中进行配置

     1 namespace Spring.Net.Service
     2 {
     3     public class BookInfoService
     4     {
     5         static IBookInfo Book { get; set; }
     6 
     7         public static string GetBook()
     8         {
     9             return Book.GetBook();
    10         }
    11     }
    12 }
    View Code

    4、spring.xml配置代码

     1 <objects xmlns="http://www.springframework.net">
     2   <!--放容器里面的所有的节点 type属性的规范:程序集名称在内的类型全名,程序集名-->
     3   <!--<object type="Spring.Net.Service.UserRegInfoService,Spring.Net.Service" singleton="true" >
     4     <property name="UserRegInfoRepository" ref="UserRegInfoRepository" />
     5   </object>-->
     6   <object type="Spring.Net.Service.BookInfoService,Spring.Net.Service" singleton="true" >
     7     <property name="Book" ref="Book" />
     8   </object>
     9   <!--<object type="Spring.Net.Service.LoginService,Spring.Net.Service" singleton="true" >
    10     <property name="Login" ref="Login" />
    11   </object>-->
    12   <!--配置Service-->
    13   <!--<object name="UserRegInfoRepository" type="Spring.Net.Repository.UserRegInfoRepository,Spring.Net.Repository" singleton="true" >
    14   </object>-->
    15   <object name="Book" type="Spring.Net.Repository.BookInfo,Spring.Net.Repository" singleton="true" >
    16   </object>
    17   <!--<object name="Login" type="Spring.Net.Repository.LoginRepository,Spring.Net.Repository" singleton="true" >
    18   </object>-->
    19 </objects>
    View Code

    5、web层调用

    1         public ActionResult SpringTest()
    2         {
    3             //ViewBag.Msg = UserRegInfoService.GetAllData();
    4             //ViewBag.Msg = LoginService.LoginInfo();
    5             ViewBag.Msg = BookInfoService.GetBook();
    6             return View();
    7         }

    6、效果图如下所示:

    在spring.xml中 有几点需要注意:

    1、当object属性中的singleton为true时,标识已单例模式访问service类,故函数要写为静态的,如BookService代码所示,否则运行不正常。

    2、object和property的name属性值 必须和service类代码里约定的一致,例如:在BookService里约定 对象为static IBookInfo Book { get; set; },那么在<property name="Book" ref="Book" />就必须保持一致,否则运行不正常。

    3、spring.xml文件【生成操作属性】要设置为嵌入的资源,否则,配置文件报错。

    以上就是本人对spring.net的浅析,如有需更正的地方,请各位多多指点,期待与热爱技术的你共同进步。

  • 相关阅读:
    骚猪队的模板
    cs231n 作业2 心路历程
    cs231n 作业1 心路历程
    视觉语言导航综述Visual Language Navigation
    论文阅读DSAE,不知道VAE能不能玩的下去
    icpc 2019 word final A题 思路
    VAE 变分自动编码器入门
    luogu4827 梦美的线段树
    EOJ Monthly 2019.2 存代码
    国王游戏,高精度完全模板
  • 原文地址:https://www.cnblogs.com/AlphaThink-AT003/p/4770586.html
Copyright © 2011-2022 走看看