利用Spring.NET实现WCF的AOP编程
2015-09-30 09:41
573 查看
在WCF中,有时我们希望实现AOP编程,用于权限验证、日志记录等,而Spring.NET就是一种很好的实现工具。
契约代码:
服务实现:
添加WcfServer.svc文件:
注意,这里添加了Spring.NET的相关Factory,同时Service变成了我们自定义的名字,它在稍后的配置文件中定义。
在WCF应用程序的配置文件Web.config中添加下列配置内容:
由此配置完成,即可实现针对WCF的AOP编程。
1、引用Spring.NET相关DLL
下载Spring.NET包,并在WCF项目中引用相关DLL:2、添加WCF服务
在WCF项目中添加相关WCF服务:契约代码:
using System.ServiceModel; namespace MyWcfTest { [ServiceContract] public interface IWcfContract { [OperationContract] string GetData(string value); } }
服务实现:
namespace MyWcfTest { public class ImplementService : IWcfContract { public string GetData(string value) { return string.Format("你输入的是:{0}", value); } } }
添加WcfServer.svc文件:
<%@ ServiceHost Language="C#" Debug="true" Service="WcfServer" Factory="Spring.ServiceModel.Activation.ServiceHostFactory" %>
注意,这里添加了Spring.NET的相关Factory,同时Service变成了我们自定义的名字,它在稍后的配置文件中定义。
3、配置Spring.NET
添加Spring.NET配置文件MyObjects.xml,内容如下:<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd"> <object type="Spring.Aop.Framework.AutoProxy.TypeNameAutoProxyCreator, Spring.Aop"> <property name="TypeNames"> <list> <value>*Service*</value> </list> </property> <property name="InterceptorNames"> <list> <value>aroundAdvice</value> </list> </property> </object> <object id="aroundAdvisor" type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor, Spring.Aop"> <property name="Advice" ref="aroundAdvice"/> <property name="MappedNames"> <list> <value>Find*</value> </list> </property> </object> <object id="aroundAdvice" type="MyWcfTest.MyAroundAdvice, MyWcfTest"/> <object id="WcfServer" type="MyWcfTest.ImplementService, MyWcfTest" /> <object id="htService1" type="MyWcfTest.Service1, MyWcfTest" /> </objects>
在WCF应用程序的配置文件Web.config中添加下列配置内容:
<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="~/MyObjects.xml" /> <resource uri="config://spring/objects" /> </context> <objects xmlns="http://www.springframework.net" /> </spring>
4、注册Spring.NET上下文
新增应用程序全局设置文件Global.asax,在其中的Application_Start方法中添加下面的代码,用以注册Spring.NET上下文。protected void Application_Start(object sender, EventArgs e) { ContextRegistry.GetContext(); }
由此配置完成,即可实现针对WCF的AOP编程。
相关文章推荐
- 打造Eclipse的Sublime Text风格全黑酷炫模式,不伤眼!!!
- eclipse在Ubuntu 12.04下的安装
- Java反射机制
- test9.4
- Java 多线程断点下载文件
- Struts2-Results机制
- java提高篇(四)-----理解java的三大特性之多态
- (转)java提高篇(四)-----理解java的三大特性之多态
- JAVA获取相对时间,几分前、几天前、几月前
- JAVA获取相对时间,几分前、几天前、几月前
- 启动Eclipse 弹出Failed to load the JNI shared library jvm.dll解决方案
- 配置JAVA的环境变量
- jdk 1.5升级提供只唤醒对方对象方法
- javac编译成功但是没有class文件生成
- super与this用法
- java war 打包、解压命令
- myeclipse导出javadoc时特殊字符 尖括号
- java中Comparator的用法 -- 实现集合和数组排序
- Eclipse中Maven+Spring3.2.8+SpringMVC HelloWorld项目
- java for循环的执行顺序和几种常用写法