Spring.net 后处理器 可用来切换实例

1.xml配置
 <!--我们在Object.xml文件上将HexuObjectPostProcessor注册到上下文对象中去-->
  <object id="hexu" type="CnblogLesson_4_7.Model.Person,HRABLL" init-method="Init" destroy-method="Destroy"/>
  <object type="CnblogLesson_4_7.HexuObjectPostProcessor,HRABLL"/>
2.代码
namespace CnblogLesson_4_7
  {
      public class HexuObjectPostProcessor : IObjectPostProcessor
      {
          public object PostProcessAfterInitialization(object instance, string objectName)
          {
          
            if (objectName.EndsWith("SysDictTypeDao"))
            {
                return Activator.CreateInstance(typeof(SysDictTypeDao));
            }
          
             Console.WriteLine("我调用了对象后处理器的 PostProcessAfterInitialization 方法,当前的对象是{0}", objectName);
             return instance;
 
         }
 
         public object PostProcessBeforeInitialization(object instance, string name)
         {
             Console.WriteLine("我调用了对象后处理器的 PostProcessBeforeInitialization 方法,当前的对象是{0}", name);
             return instance;
         }
     }
 
 }

 

posted on 2016-09-29 16:51  听哥哥的话  阅读(265)  评论(0编辑  收藏  举报

导航