一、前言:

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中某个对象的类型,然后框架自动实例化,好处在于提高软件的灵活性,特别是对于一个接口有多个实现的场景。

Spring.Net依赖注入(属性注入)的更多相关文章

  1. spring注入 属性注入 构造器注入 set方法注入

    spring注入 属性注入 构造器注入 set方法注入(外部bean注入)

  2. spring练习,使用Eclipse搭建的Spring开发环境,属性注入通过构造方法方式实现,模拟用户的正常登录。

    相关 知识 >>> 相关 练习 >>> 实现要求: 使用Eclipse搭建的Spring开发环境,属性注入通过构造方法方式实现,模拟用户的正常登录.要求如下: 通过 ...

  3. Spring.Net依赖注入(属性注入)学习笔记

    一.前言: Spring.Net是Java开源框架迁移过来的,主要分为 1)依赖注入 2)面向方面编程 3)数据访问抽象 4)Asp.Net扩展 四个模块功能,这里只是简单介绍依赖注入模块功能. 对于 ...

  4. Spring中集合类型属性注入

    我们都知道如何去注入普通属性的值,非常简单,那么我们如何去注入开发中常见的集合类型的属性了,别急,往下看. 这里将介绍如何给Map list set Array Properties 这些属性注入值. ...

  5. Spring boot将配置属性注入到bean类中

    一.@ConfigurationProperties注解的使用 看配置文件,我的是yaml格式的配置: // file application.yml my: servers: - dev.bar.c ...

  6. Spring Bean 注入 1 - 构造方法注入,属性注入,自动装配

    1.代码结构图 xxx 2.bean代码 package com.xxx.bean; /** * Created with IntelliJ IDEA. * User: zhenwei.liu * D ...

  7. Spring boot将配置属性注入到bean 专题

    https://blog.csdn.net/wangmx1993328/article/details/81002901 Error starting ApplicationContext. To d ...

  8. Spring中@value以及属性注入的学习

    1.简单的Java配置 配置文件(jdbc.properties) jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://1 ...

  9. spring的bean的属性注入

    一.设置注入 设置注入要求: 要求属性在实体类中必须有getter 和setter方法,然后在spring的工厂中就可以使用property标签进行设值注入. 二.构造注入 通过类的构造方法的方式注入 ...

随机推荐

  1. java部署ubuntu后中文显示问号问题

    1.首先先回忆自身项目的编码格式,即在本地进行编码时使用的编码格式.UTF-82.检测tomcat的设置问题,在web.xml和server中的设置:server.xml中: <Connecto ...

  2. MySQL集群系列2:通过keepalived实现双主集群读写分离

    在上一节基础上,通过添加keepalived实现读写分离. 首先关闭防火墙 安装keepalived keepalived 2台机器都要安装 rpm .el6.x86_64/ 注意上面要替换成你的内核 ...

  3. Python_uuid 学习总结

    1. 背景知识: UUID: 通用唯一标识符 ( Universally Unique Identifier ), 对于所有的UUID它可以保证在空间和时间上的唯一性. 它是通过MAC地址, 时间戳, ...

  4. [转]软件测试- 3 - Mock 和Stub的区别

    由于一直没有完全搞明白Mock和Stub的区别,所以查了很多文章,而这一篇是做好的: http://yuan.iteye.com/blog/470418 尤其是8楼,Frostred的发言,描述地相当 ...

  5. PHP手机号码归属地查询API接口

    淘宝网 API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443 参数: tel:手机号码 返回:JSON ...

  6. VC++ Debug显示指针所指的array内容

    If you expand a pointer and you only get a single item, just add ",n" to the entry in the ...

  7. EF提供的3中查询方式

    1. Linq to Entities using (TestEntities te = new TestEntities()) { var user = from a in te.User wher ...

  8. python学习之路----输出所有大小写字母

    print([chr(i) for i in range(48, 58)]) # 所有数字print([chr(i) for i in range(65, 91)]) # 所有大写字母print([c ...

  9. 【LeetCode】Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  10. http协议详解(1)

    HTTP协议报文格式 接下来我们看看HTTP协议(Hypertext Transfer Protocol――超文本传输协议)浏览器端(客户端)向WEB服务器端访问页面的过程和HTTP协议报文的格式. ...