Spring.net配置相关

代码
<? xml version="1.0" encoding="utf-8"  ?>
< configuration >

    
< configSections >
        
< sectionGroup  name ="common" >
            
< section  name ="logging"  type ="Common.Logging.ConfigurationSectionHandler, Common.Logging"   />
        
</ sectionGroup >
        
        
        
< 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 ="assembly://Model/Model.config/object_all.xml" />             
            
            
<!-- <resource uri="D:\wucg\test\HelloWorldSpringDotnet\HelloWorldSpringDotnet\config\object_all.xml"/> -->
            
<!-- <resource uri="config://spring/objects"/> -->
            
<!-- <resource uri="assembly://程序集名/命名空间.文件夹名/资源名" /> -->   <!-- xml的属性“生成操作”设为“嵌入资源” -->
            
<!-- <resource uri="assembly://HelloWorldSpringDotnet/HelloWorldSpringDotnet.config/object_all.xml"/> -->
        
        
</ context >
        
<!-- <objects xmlns="http://www.springframework.net">
            <object id="MyObject" type="HelloWorldSpringDotnet.MyCls" >
            </object>
        </objects>
-->
    
</ spring >
</ configuration >

 

 


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

namespace  HelloWorldSpringDotnet
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            IApplicationContext context 
=  ContextRegistry.GetContext();
            MyCls o 
=  context.GetObject( " MyObject " as  MyCls;
            MyCls o2 
=  context.GetObject( " MyObject " as  MyCls;
            o.MyInt 
=   10 ;
            o2.MyInt 
=   20 ;

            Console.WriteLine(
object .ReferenceEquals(o, o2));
            Console.WriteLine(
" o.MyInt={0} " ,o.MyInt);
            Console.WriteLine(
" o2.MyInt={0} " , o2.MyInt);


            
/*  法三:独立配置文件,xml编译时作为嵌入资源,最好
             * e.g.:
             * <!--<resource uri="assembly://程序集名/命名空间.文件夹名/资源名" />--> 
             * <!--xml的属性“生成操作”设为“嵌入资源”-->            
             * <!--<resource uri="assembly://Model/Model.config/object_all.xml"/>-->
             * 
                IApplicationContext context = ContextRegistry.GetContext();
                object o = context.GetObject("MyObject");
            
*/

            
// 法2:
            
// IApplicationContext context = new XmlApplicationContext(@"D:\wucg\test\HelloWorldSpringDotnet\HelloWorldSpringDotnet\config\object_all.xml");            
            
// object o = context.GetObject("MyObject");
            
            
/* 法1:
            StaticApplicationContext context = new StaticApplicationContext();
            context.RegisterPrototype("MyCls1", typeof(MyCls), null);
            object o = context.GetObject("MyCls1");
           
            
*/  


            Console.WriteLine(o);
            Console.ReadKey();
        }
    }
}

 

你可能感兴趣的:(spring)