zoukankan      html  css  js  c++  java
  • Spring.Net依赖注入(属性注入)

    一、前言:

    Spring.Net是Java开源框架迁移过来的,主要分为

    1)依赖注入

    2)面向方面编程

    3)数据访问抽象

    4)Asp.Net扩展

    四个模块功能,这里只是简单介绍依赖注入模块功能。

    对于Mybatis,在这里也是简单介绍相关配置和实现插入和查找功能。

    二、项目结构:

    三、具体开发

    1、Spring.Net模块介绍

    这里以BLL层的Web层为例作介绍,其他层也是类似,读者可以举一反三。

    关于Spring.Net框架的WebConfig配置如下:

    1)注册Spring.Net库的dll:

    <!--Spring.Net配置 引用Spring需要使用的类型-->
    <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>

    2)添加对aspx页面注入支持:

    在webconfig根节点的config节点下面的configuration的System.Web(如果没有此节点请自行添加)节点下配置如下。这个配置也是必要的,作用是添加对aspx页面注入的支持。

    <httpHandlers>
          <remove verb="*" path="*.asmx" />
          <add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web" />
        </httpHandlers>
        <httpModules>
          <!--这里不能漏,不然页面注入的时候会出错-->
          <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
    
        </httpModules>

    3)引入类型定义文件:

    Webconfig xml根节点下配置引入定义对象的资源文件。引入资源文件分为两种方式,一种是作为资源嵌入到程序集中,如下面注释代码所示,uri="assembly://MyBlog.BLL/MyBlog.BLL/Objects.xml"/>。用这种方式要在Visual Studio资源管理器中将Objects.xml文件的生成操作属性改成嵌入的资源,不然是没把文件带进去的。这里推荐第二中方式,即本项目使用的方式,

    直接使用相对路径描述资源。如:<resource uri="~/Res/Objects/BLLObjects.xml"/>。符号"~"代表根目录。

    <spring>
        <context>
          <!--assembly://程序集名/命名空名/文件名(程序集内嵌资源)-->
          <!--<resource uri="assembly://MyBlog.BLL/MyBlog.BLL/Objects.xml"/>-->
          <resource uri="config://spring/objects" />
          <resource uri="~/Res/Objects/TestObjects.xml"/>
           <!--数据库连接对象-->
          <resource uri="~/Res/Objects/DBConfig.xml"/>
          <resource uri="~/Res/Objects/BLLObjects.xml"/>
          <resource uri="~/Res/Objects/DALObjects.xml"/>
        </context>
        <!--必要-->
        <objects xmlns="http://www.springframework.net">
        </objects>
      </spring>

    4)类型定义配置:

    BLLObjects.xml文件里面的内容如下所示。诸如此类的文件作用是定义类型,这个是Spring.Net注入的关键。这里解释一下下面配置文件的意思。

    每一个object节点,描述了C#代码中的一个类,object节点的id是个身份标记,这个必须是唯一的;type属性描述了具体的类,这个属性由类的全名+逗号+类的命名空间构成。

    例如type="MyBlog.BLL.ArtiCaManager,MyBlog.BLL"。这里描述的是BLL层中的ArtiCaManager类。而object节点下面的property 属性则是描述了这个类下面的属性,这是本项目依赖注入的关键(本项目采用的注入方式只要是属性注入和构造注入)。例如节点 :

    <property name="ArtiCaDao" ref="ArtiCaDao"></property> 。

    这个是ArtiCaManager类下面的属性:

    public IArtiCaDao ArtiCaDao { get; set; } //文章类别数据操作类。

    这个属性节点的作用是描述了IArtiCaDao接口实例化的类型是ArtiCaDao(ref="ArtiCaDao">)。而ref属性里面写的是object节点的ID,作用也是描述了一个类型。

    <?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="ArtiCaMana" type="MyBlog.BLL.ArtiCaManager, MyBlog.BLL" />-->
      
      <object id="ArtiCaMana" type="MyBlog.BLL.ArtiCaManager,MyBlog.BLL">
        <property name="ArtiCaDao" ref="ArtiCaDao"></property>
      </object>
      
      <object id="LogMana" type="MyBlog.BLL.LogManager,MyBlog.BLL">
        <property name="iLogDao" ref="LogDao"></property>
      </object>
    
      <object id="DeptMana" type="MyBlog.BLL.DepartmentManager,MyBlog.BLL">
        <property name="iDepartmentDao" ref="DepartmentDao"></property>
      </object>
        
    </objects>

    5)代码实现:

    BLL层ArtiCaManager类代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using MyBlog.Model;
    using MyBlog.IDAL;
    using Spring.Context;
    using Spring.Context.Support;
    using MyBlog.IBLL;
    
    namespace MyBlog.BLL
    {
        /// <summary>
        /// 文章类别管理业务逻辑类
        /// </summary>
        public class ArtiCaManager : IArtiCaManager
        {
    
            public IArtiCaDao ArtiCaDao { get; set; } //文章类别数据操作类
    
            /// <summary>
            /// 插入文章类别
            /// </summary>
            /// <param name="arCa">文章类别实体类</param>
            /// <returns>是否插入成功</returns>
            public bool InsertCa(ArticleCategory arCa)
            {
                arCa.CategoryName =  arCa.CategoryName+ "[这里是调用了文章类别管理类BLL接口插入的]";   //仅供测试留个记号
                return ArtiCaDao.InsertCa(arCa);
            }
    
            /// <summary>
            /// 取出所有文章类别
            /// </summary>
            /// <returns>文章类别列表</returns>
            public IList<ArticleCategory> SelectAllCa()
            {
                return ArtiCaDao.SelectAllCa();
            }
        }
    }

    四、结论

    Spiring.Net的依赖注入,主要是利用了.Net反射的特性,在xml中定义了.Net中某个对象的类型,然后框架自动实例化,好处在于提高软件的灵活性,特别是对于一个接口有多个实现的场景。

  • 相关阅读:
    day 66 crm(3) 自创组件stark界面展示数据
    day 65 crm(2) admin源码解析,以及简单的仿造admin组件
    用 Python+nginx+django 打造在线家庭影院
    django -admin 源码解析
    day 64 crm项目(1) admin组件的初识别以及应用
    云链接 接口不允许 情况 解决方法 mysql Host is not allowed to connect to this MySQL server解决方法
    day 56 linux的安装python3 ,虚拟环境,mysql ,redis
    day55 linux 基础以及系统优化
    Codeforces 989 P循环节01构造 ABCD连通块构造 思维对云遮月参考系坐标轴转换
    Codeforces 990 调和级数路灯贪心暴力 DFS生成树两子树差调水 GCD树连通块暴力
  • 原文地址:https://www.cnblogs.com/hornet/p/4058993.html
Copyright © 2011-2022 走看看