容器中对象的部署分为两种方式:singleton和非singleton(java里叫prototype)。这里的singleton指的是“单例模式”,就是说当一个对象被定义为singleton时,容器中就只会有一个共享的实例,任何时候通过id或别名请求该对象都会返回这个共享实例的引用(也就是说这个对象只会被创建一次)。当使用非singleton,或者说原型模式布署时,每次请求对象都会创建新的实例。在某些场合,如果需要为每个用户返回单独的用户对象或其它对象,非singlton布署模式就比较理想。Spring.NET默认为singleton模式。每次调用GetObject方法时得到的都是同样的实例;当singleton="false"时,每次调用GetObject方法时得到的则是不同的实例。

  <!--单例模式-->
  
<object id="personDao" type="SpringNetScop.PersonDao, SpringNetScop" />

 

Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        [Test]
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
public void CreateWithSingleton()
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
{
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            
string[] xmlFiles = new string[] 
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            
{
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200                
"assembly://SpringNetScop/SpringNetScop/Objects.xml"
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            }
;
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            IApplicationContext context 
= new XmlApplicationContext(xmlFiles);
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            IObjectFactory factory 
= (IObjectFactory)context;
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            
object obj1 = factory.GetObject("personDao");
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            
object obj2 = factory.GetObject("personDao");
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            Assert.AreEqual(obj1, obj2);
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        }

我们用NUnit可以看到下图:Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200

 

 

 

 

 

 

 

 

使用singleton="false"的代码

  <!--非单例模式-->
  
<object id="person" type="SpringNetScop.Person, SpringNetScop" singleton="false" />

 

Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        [Test]
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
public void CreateWithOutSingleton()
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
{
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            
string[] xmlFiles = new string[] 
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            
{
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200                
"assembly://SpringNetScop/SpringNetScop/Objects.xml"
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            }
;
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            IApplicationContext context 
= new XmlApplicationContext(xmlFiles);
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            IObjectFactory factory 
= (IObjectFactory)context;
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            
object obj1 = factory.GetObject("person");
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            
object obj2 = factory.GetObject("person");
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            Assert.AreNotEqual(obj1, obj2);
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        }

 

Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200    public class Person
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200    
{
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
public Person()
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
{
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            Console.WriteLine(
"Person被实例");
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        }

Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
public override string ToString()
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
{
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            
return "我是Person";
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        }

Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
~Person()
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
{
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            Console.WriteLine(
"Person被销毁");
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200      

 

图为:Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200

 

 

 

 

 

 

 

 

这说明singleton=false后,每次调用GetObject方法获取的对象是不同实例的,当脱离调用方法(CreateWithOutSingleton)的作用域后,该实例会被Spring.NET容器销毁。

   

   lazy-init属性是指:当Spring.NET容器初始化的时候标注该属性的对象将被实例化,反之则是调用GetObject方法的时候才被实例化。

  <!--调用时加载-->
  
<object id="personServer" type="SpringNetScop.PersonServer, SpringNetScop" lazy-init="true" />

 

Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        [Test]
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
public void CreateWithLazy()
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
{
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            
string[] xmlFiles = new string[] 
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            
{
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200                
"assembly://SpringNetScop/SpringNetScop/Objects.xml"
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            }
;
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            IApplicationContext context 
= new XmlApplicationContext(xmlFiles);
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            IObjectFactory factory 
= (IObjectFactory)context;
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            
object dao = factory.GetObject("personDao");
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            Console.WriteLine(dao.ToString());
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            
object server = factory.GetObject("personServer");
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            Console.WriteLine(server.ToString());
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        }

 

Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200    public class PersonServer
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200    
{
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
public PersonServer()
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
{
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            Console.WriteLine(
"PersonServer被实例");
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        }

Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
public override string ToString()
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        
{
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200            
return "我是PersonServer";
Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200        }

Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200    }

 

如图:Spring.NET学习笔记5——容器中对象的作用域(基础篇) Level 200

PersonDao类未设置lazy-init属性,则当Spring.NET初始化时被实例;PersonServer类设置lazy-init="true",则当调用GetObject方法时才被实例。

一般情况下可以有选择的设置lazy-init属性,正如双刃剑一样,设置为lazy-init=true的时候应用程序启动时会快一点,但是在启动的时候就不能够帮我们检测错误,但当调用的时候一旦发生错误,后果是不堪设想的。

  更多资料请查看Spring.NET中文手册。

 

  代码下载 

 

 

  返回目录

作者:刘冬.NET 博客地址:http://www.cnblogs.com/GoodHelper/ 欢迎转载,但须保留版权

相关文章:

  • 2021-07-04
  • 2022-01-29
  • 2022-01-15
  • 2021-10-17
猜你喜欢
  • 2022-12-23
  • 2021-10-04
  • 2021-06-21
  • 2022-12-23
  • 2021-12-25
相关资源
相似解决方案