接着上篇文章,我们将采用新的方式进行动态代理。
ObjectNameAutoProxyCreator创建代理
根据配置文件中的配置,Spring容器会根据此配置,为符合条件的对象创建代理。
配置文件
<?xml version="1.0"?>
<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 xmlns="http://www.springframework.net">
<object id="beforeAdvice" type="AOPExample.LogBeforeAdvice,AOPExample"/>
<!--在容器中配置,必须的,以便在容器中可以根据后缀名Service查找到这个对象-->
<object id="userService" type="AOPExample.UserService,AOPExample"/>
<!--通过ObjectNameAutoProxyCreator自动创建AOP代理-->
<object id="IServiceProxy" type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator, Spring.Aop">
<property name="ObjectNames">
<list>
<!--只要是对象的后缀名为Service的,就会自动为它们创建代理-->
<value>*Service</value>
</list>
</property>
<property name="InterceptorNames">
<list>
<value>beforeAdvice</value>
</list>
</property>
</object>
</objects>
</spring>
</configuration>
客户端
class Program
{
static void Main(string[] args)
{
User enUser = new User() {
Name ="Danny",
Age=15
};
IApplicationContext context = ContextRegistry.GetContext();
IUserService userService = (IUserService)context.GetObject("userService");
userService.GetUserInfo(enUser);
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u010924834/article/details/47283731