ASP Spring.net

89 篇文章 0 订阅

Spring.net的作用(通过配置文件和接口实现解耦):

1、IoC:Inversion of Control
控制翻转:就是创建对象的权利由开发人员自己控制New,转到了由容器来控制。
2、DI:Dependency Injection
It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments and properties that are set on the object instance after it is constructed. 
依赖注入:就是在通过容器来创建对象的时候,在对象的初始化是可以给一些属性、构造方法的参数等注入默认值(可以是复杂的类型.
3、Aop--权限校验--日志处理。Filter.

操作步骤:

一、将D:\Spring.NET-2.0.0-M1\Spring.NET\bin\net\4.0\release\下的Spring.Core.dll、Spring.Core.pdb、Spring.Core.xml、Common.Logging.dll四个文件放在项目目录下。并在项目中添加引用。

二、配置文件App.config(Web.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="config://spring/objects"/>  <!--资源来源-->
      <resource uri="file://services.xml"/>   <!--可以将<object>节点放在一个services.xml文件中。xml文件放在项目根目录下,xml设置属性"始终复制(复制到bin/debug目录下)"-->
    </context>
    <objects xmlns="http://www.springframework.net">
      <description>An  example that demonstrates simple IoC features.</description>
        <object name="UserInfoService"  type="Myweb.UserInfoService,Myweb">   <!--要创建的类(对象)-->
          <property name="UserName" value="zhangsan"/>  <!--属性注入,依赖注入(DI)-->
          <property name="Person" ref="类名XX"/>      <!--Person类类型的属性,要用ref(关联)。-->
        </object> 
        <object name="类名XX"  type="命名空间.类名XX, 程序集名">   <!--Person类类型属性关联(ref)的类(对象)。命名空间和程序集一般是相同的,也可能不同-->
          <property name="属性名" value="属性值"/>
        </object>
    </objects>
  </spring>
</configuration>

三、要创建的类UserInfoService.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Myweb
{
   public class UserInfoService:IUserInfoService  //实现接口是为了解耦。(表现层和业务层解耦)
    {
       public string UserName { get; set; }
       public Person Person { get; set; }  //Person是类类型的属性
        public string ShowMsg()
        {
            return "Hello World:"+UserName+":年龄是:"+Person.Age;
        }
    }
}
四、在项目程序中通过Spring容器创建对象:
using Spring.Context;
using Spring.Context.Support;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyWeb
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IApplicationContext ctx = ContextRegistry.GetContext();  //获取Spring容器
            IUserInfoService lister = (IUserInfoService)ctx.GetObject("UserInfoService");   //通过容器创建对象。接口IUserInfoService是为了解耦(表现层和业务层解耦)
            MessageBox.Show(lister.ShowMsg());
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值