使用 windsor 实现IOC 和 AOP
代码很简单,不多说。
对于拦截,windsor 使用动态代理的方式,即生成继承类的方式来实现的,因此无法拦截private 方法,因为无法在继承类中看见private方法。
using System;
using System.Reflection;
using Castle.Core;
using Castle.DynamicProxy;
using Castle.MicroKernel.Registration;
using Castle.Windsor; namespace WindsorSample
{
public class ApplicationInterceptor : IInterceptor
{
#region IInterceptor Members public void Intercept(IInvocation invocation)
{
Console.WriteLine("call started " + invocation.Method.Name);
invocation.Proceed();
Console.WriteLine("call ended " + (invocation.ReturnValue ?? "NULL"));
} #endregion
} public interface IUserApplication
{
void Show();
} public class UserApplication : IUserApplication
{
private static IUserResoistory _userResoistory; public UserApplication(IUserResoistory userResoistory)
{
_userResoistory = userResoistory;
}
public void Show()
{
Console.WriteLine(_userResoistory.Name);
}
} public interface IUserResoistory
{
string Name { get; }
} public class UserResoistory : IUserResoistory
{
public string Name { get; private set; } public UserResoistory()
{
Name = "UserResoistory";
}
} class Program
{
static void Main(string[] args)
{
IWindsorContainer container = new WindsorContainer();
container.Register(Classes.FromAssembly(Assembly.GetExecutingAssembly()).BasedOn<IUserResoistory>().WithServiceAllInterfaces());
container.Register(Component.For<IInterceptor>().ImplementedBy<ApplicationInterceptor>().Named("myinterceptor"));
container.Register(Component.For<IUserApplication>().ImplementedBy<UserApplication>().Interceptors(InterceptorReference.ForKey("myinterceptor")).Anywhere); var userApplication = container.Resolve<IUserApplication>(); userApplication.Show(); Console.ReadKey();
}
}
}
使用 windsor 实现IOC 和 AOP的更多相关文章
- Spring的IOC和AOP之深剖
今天,既然讲到了Spring 的IOC和AOP,我们就必须要知道 Spring主要是两件事: 1.开发Bean:2.配置Bean.对于Spring框架来说,它要做的,就是根据配置文件来创建bean实例 ...
- spring的IOC和AOP协同工作
看网络上的spring资料,基本都是在讲解IOC和AOP,但是二者是如何协同工作的,说的很少. 粗略调试了下BeanFactory的创建过程,发现是如图所示的大概过程.其中BeanPostProces ...
- spring - ioc和aop
1.程序中为什么会用到spring的ioc和aop 2.什么是IOC,AOP,以及使用它们的好处,即详细回答了第一个问题 3.原理 关于1: a:我们平常使用对象的时候,一般都是直接使用关键字类new ...
- spring的IOC和AOP
spring的IOC和AOP 1.解释spring的ioc? 几种注入依赖的方式?spring的优点? IOC你就认为他是一个生产和管理bean的容器就行了,原来需要在调用类中new的东西,现在都是 ...
- Castle框架中的IOC和AOP机制
反转控制(IOC)和面向切面编程(AOP)技术作为当前比较流行的技术,其优势已受到广泛关注,但是这两项新技术在实际项目上的应用研究却很落后,而且在.NET平台下实现这两项技术没有形成可以广泛套用的框架 ...
- 【转】spring - ioc和aop
[转]spring - ioc和aop 1.程序中为什么会用到spring的ioc和aop 2.什么是IOC,AOP,以及使用它们的好处,即详细回答了第一个问题 3.原理 关于1: a:我们平常使用对 ...
- 深入浅出学习Spring框架(四):IoC和AOP的应用——事务配置
在前文 深入浅出学习Spring框架(一):通过Demo阐述IoC和DI的优势所在. 深入浅出学习Spring框架(三):AOP 详解 分别介绍了Spring的核心功能——IoC和AOP,光讲知识远远 ...
- Spring入门导读——IoC和AOP
和MyBatis系列不同的是,在正式开始Spring入门时,我们先来了解两个关于Spring核心的概念,IoC(Inverse of Control)控制反转和AOP()面向切面编程. 1.IoC(I ...
- 六:Ioc和AOP使用拓展
Ioc和AOP使用拓展 一:1.构造注入 一个<constructor-arg>元素表示构造方法的一个参数,且使用时不区分顺序,index指定元素,位置从0开始,Type用来指定参数,避免 ...
随机推荐
- Android中通过进程注入技术修改系统返回的Mac地址
致谢 感谢看雪论坛中的这位大神,分享了这个技术:http://bbs.pediy.com/showthread.php?t=186054,从这篇文章中学习到了很多内容,如果没有这篇好文章,我在研究的过 ...
- haproxy笔记
haproxy安装.启动.日志配置 方法1:#安装 yum install haproxy -y #日志配置 sed -i 's/^#$ModLoad imudp/$ModLoad imudp/g' ...
- 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】06、Mybatis+SQLServer集成
1.增加POM依赖 注意pagehelper插件,我重写过,可以到我的这篇文章了解https://www.cnblogs.com/LiveYourLife/p/9176934.html <dep ...
- 4、jQuery面向对象之简单的插件开发
1.alert例子 (function($){ $.alert = function(msg){ window.alert(msg); } $.fn.alert = function(msg){ wi ...
- maven项目报错:Class path contains multiple SLF4J bindings
maven项目编译不报错,运行时报错如下: SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [b ...
- (转)Git 提交的正确姿势:Commit message 编写指南
Git 每次提交代码,都要写 Commit message(提交说明),否则就不允许提交. $ git commit -m "hello world" 上面代码的-m参数,就是用来 ...
- Linux用ifconfig设置IP、掩码、网关
ifconfig eth0 ip netmask 255.255.255.0 route add default gw 网关
- PHP 遍历目录下面的所有文件及子文件夹
//遍历文件夹下面所有文件及子文件夹 function read_all_dir ( $dir ){ $result = array(); $handle = opendir($dir);//读资源 ...
- 一个普通函数的冷僻属性(length、caller、arguments、name、[[Scopes]]和[[FunctionLocation]])
https://blog.csdn.net/qq_17175013/article/details/81915059
- centos7 编译打包bcache-tools
centos7 build bcache-tools 获取源码 centos 本身不提供bcache-tools的rpm,所以需要自己build. 从fedora下载源码,也可以从github社区下载 ...