• spring.net入门


    1.在项目中引入Spring.Aop.dll Spring.core.dll和antlr.runtime.dll

    2.app.config(web.config)中加入以下代码,请注意注释

      <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://~/spring.config"/--><!--这里使用独立配置文件-->
        
    </context>
        
    <objects xmlns="http://www.springframework.net">


          
    <object id="aroundAdvice" type="SpringLib.Lib.AroundAdvice,SpringLib" />

          
    <object id="myCommand" type="Spring.Aop.Framework.ProxyFactoryObject">
            
    <property name="Target">
              
    <object type="SpringLib.Lib.ServiceCommand,SpringLib" />
            
    </property>
            
    <property name="InterceptorNames">
              
    <list>
                
    <value>aroundAdvice</value>
                
    <!--<value>throwsAdvice</value>-->
              
    </list>
            
    </property>
          
    </object>
          
        
    </objects>
      
    </spring>

    使用独立配置文件时的示例代码(spring.config)

    <objects xmlns="http://www.springframework.net">


      
    <object id="aroundAdvice" type="SpringLib.Lib.AroundAdvice,SpringLib" />

      
    <object id="myCommand" type="Spring.Aop.Framework.ProxyFactoryObject">
        
    <property name="Target">
          
    <object type="SpringLib.Lib.ServiceCommand,SpringLib" />
        
    </property>
        
    <property name="InterceptorNames">
          
    <list>
            
    <value>aroundAdvice</value>
            
    <!--<value>throwsAdvice</value>-->
          
    </list>
        
    </property>
      
    </object>

    </objects>

    3.建立一个独立的Class Library 工程,建立以下示例文件

    代码
    //AroundAdvice.cs

    using System;
    using AopAlliance.Intercept;

    namespace SpringLib.Lib
    {

        
    public class AroundAdvice : IMethodInterceptor
        {
            
    public object Invoke(IMethodInvocation invocation)
            {
                System.Windows.Forms.MessageBox.Show(
    "bengin.");
                
    object returnValue = invocation.Proceed();
                System.Windows.Forms.MessageBox.Show(
    "end.");
                
    return returnValue;
            }
        }
    }

    //--------------------------------
    //ICommand.cs
    namespace SpringLib.Lib
    {    
        
    public interface ICommand 
            {
                    
    bool IsUndoCapable { get; }

            
    void Execute();

                    
    void UnExecute();
        }
    }
    //----------------------------------
    //ServiceCommand.cs
    using System;
    namespace SpringLib.Lib
    {   
        
    public class ServiceCommand : ICommand
        {
            
    #region ICommand Members

            
    public bool IsUndoCapable
            {
                
    get { return true; }
            }

            
    public void Execute()
            {
                System.Windows.Forms.MessageBox.Show(
    "Execute...");
            }

            
    public void UnExecute()
            {
                System.Windows.Forms.MessageBox.Show(
    "UnExecute()...");
            }

            
    #endregion
        }
    }

     4.在主项目中添加刚才新建的工程引用

     5. 在主项目中建立SpringHelper.cs

    //SpringHelper
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Spring.Context;
    using Spring.Context.Support;

    namespace Test
    {
        
    public class SpringHelper
        {
            
    public static object GetObj(string name)
            {
                IApplicationContext ctx 
    = ContextRegistry.GetContext();
                Object o 
    = ctx[name];
                
    return o;
            }
        }
    }

    //TestForm.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    using Spring.Aop.Framework;


    using Spring.Context;
    using Spring.Context.Support;

    using SpringLib.Lib;

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

            
    private void button1_Click(object sender, EventArgs e)
            {
                Object o 
    = SpringHelper.GetObj("myCommand");
                
    if (o == null)
                    MessageBox.Show(
    "null");
                
    else
                    MessageBox.Show(o.GetType().ToString());
                ICommand command 
    = o as ICommand;
                command.Execute();
            }
        }
    }
  • 相关阅读:
    第六周总结
    《构建之法》读后感二
    移动端疫情展示
    第五周
    用python爬取疫情数据
    第四周
    疫情图表展示和时间查询
    wpf datagrid row height 行高自动计算使每行行高自适应文本
    c# 实现mysql事务
    c# 简单实现 插件模型 反射方式
  • 原文地址:https://www.cnblogs.com/zyip/p/1857469.html
Copyright © 2020-2023  润新知