深入浅出AOP(二)--IOC容器
2015-06-28 22:23
387 查看
上一篇,用的静态代理实现了AOP,实际上,AOP就是一种思想,实现的方式有很多种,而要实现AOP,将提供的非业务类的方法(服务类)放在容器中,更加高级一点。
IOC就是提供了一种容器。
AOP+IOC实现:
整体的解决方案:
在这个里面,我们首先写Model:
在写Service:
写Conpontext类:
在测试中首先建立APP.config文件,该文件划定了Spring容器要盛放的对象:
在测试Porgram中:
有了容器,我们的AOP就可以从将服务了都放在容器中了。
AOP就变成了这样:
我们将serivce都放在容器中,用的时候,直接在容器中进行配置就可以了,这样,就减少了AOP和Service之间的耦合。
IOC容器的两个接口:
未完待续。。。。。。
参阅博客:http://www.csharpwin.com/csharpspace/3183.shtml
IOC就是提供了一种容器。
AOP+IOC实现:
整体的解决方案:
在这个里面,我们首先写Model:
<span style="font-size:18px;">using System; using System.Collections.Generic; using System.Text; namespace Spring.Demo.Model { /// <summary> /// 用户类 /// </summary> public class Users { /// <summary> /// 编号 /// </summary> private int _oid; public int Oid { get { return _oid; } set { _oid = value; } } /// <summary> /// 姓名 /// </summary> private string _name; public string Name { get { return _name; } set { _name = value; } } /// <summary> /// 性别 /// </summary> private string _sex; public string Sex { get { return _sex; } set { _sex = value; } } /// <summary> /// 年龄 /// </summary> private int _age; public int Age { get { return _age; } set { _age = value; } } } }</span>
在写Service:
<span style="font-size:18px;">using System; using System.Collections.Generic; using System.Text; namespace Spring.Demo.Service { public interface IUsers { /// <summary> /// 返回用户的详细信息的方法 /// </summary> /// <returns></returns> string GetUserInfo(); } } </span>
写Conpontext类:
<span style="font-size:18px;">using System; using System.Collections.Generic; using System.Text; using Spring.Demo.Service; using Spring.Demo.Model; namespace Spring.Demo.Compontext { public class UsersCompontents : IUsers { public UsersCompontents() { } #region 获取用户信息 public string GetUserInfo() { Users user = new Users(); user.Oid = 1; user.Name = "Beniao"; user.Sex = "Boy"; user.Age = 25; return string.Format("编号:{0}--->姓名:{1}--->性别:{2}--->年龄:{3}", user.Oid, user.Name, user.Sex, user.Age); } #endregion } }</span>
在测试中首先建立APP.config文件,该文件划定了Spring容器要盛放的对象:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8" ?> <configuration> <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"/> </context> <objects> <!--这的配置根据实际的程序来的,UsersCompontents是程序集Spring.Demo.Compontext下的一个类--> <object name="Users" type="Spring.Demo.Compontext.UsersCompontents,Spring.Demo.Compontext"> </object> </objects> </spring> </configuration></span>
在测试Porgram中:
<span style="font-size:18px;">using Spring.Context; using Spring.Demo.Service; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; namespace Sping.Demo.SimpleTest { class Program { static void Main(string[] args) { //从config文件中取得程序集信息 IApplicationContext context = ConfigurationManager.GetSection("spring/context") as IApplicationContext; //调用方法 //Users为config文件里的配置节 //<object name="Users" // type="Spring.Demo.Compontext.UsersCompontents,Spring.Demo.Compontent"> //</object> IUsers user = context.GetObject("Users") as IUsers; Console.WriteLine(user.GetUserInfo()); Console.Read(); } } } </span>
有了容器,我们的AOP就可以从将服务了都放在容器中了。
AOP就变成了这样:
我们将serivce都放在容器中,用的时候,直接在容器中进行配置就可以了,这样,就减少了AOP和Service之间的耦合。
IOC容器的两个接口:
未完待续。。。。。。
参阅博客:http://www.csharpwin.com/csharpspace/3183.shtml
相关文章推荐
- yui3的AOP(面向切面编程)和OOP(面向对象编程)
- JavaScript AOP编程实例
- 使用AOP改善javascript代码
- 初识SmartJS - AOP三剑客
- java使用动态代理来实现AOP(日志记录)的实例代码
- Javascript aop(面向切面编程)之around(环绕)分析
- php笔记之:AOP的应用
- 实例讲解Java的Spring框架中的AOP实现
- autoload-cache-1.7 发布
- Spring3 AOP配置异常。执行时间记录
- spring 切面memcached的环绕通知
- 关于HouseMD
- Spring AOP介绍及源码分析
- 基于 Java 语言的极速 WEB + ORM 框架 JFinal 1.6 发布
- SpringAOP实现自动生成日志
- Spring Aspect Oriented Programming
- 30、最简单的mvc框架tiny,增加Aop
- 31、最简单的mvc框架tiny,增加Ioc,jdbc工具类(1个类),连接池(1个类)
- 利用Scala特征(trait)的堆叠操作特性进行切面编程
- 面向切面编程(AOP)初探