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 工程,建立以下示例文件

ExpandedBlockStart.gif 代码
// 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();
        }
    }
}

 

 

 

转载于:https://www.cnblogs.com/zyip/archive/2010/10/21/1857469.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值