四. 切入点操作
4.1 静态切入点
4.1.1 ProxyFactoryObject 显式创建AOP代理
- AOP 配置文件
- <object id="UserValidateTarget" type="Stephen.SpringNet.AOPSample.Servcies.Impl.UserValidate, Servcies"></object>
- <object id="RoundInterceptor" type="Stephen.SpringNet.AOPSample.Servcies.Interceptor.RoundInterceptor, Servcies"></object>
- <object id="UserValidateProxy" type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop">
- <property name="proxyInterfaces" value="Stephen.SpringNet.AOPSample.Servcies.IValidateService"/>
- <property name="target" ref="UserValidateTarget"/>
- <property name="interceptorNames">
- <list>
- <value>RoundInterceptor</value>
- </list>
- </property>
- </object>
不过由于显式的创建AOP在要创建多个代理的时候需要重复的配置因此Spring提供了自动代理
4.1.2 ObjectNameAutoProxyCreator 对象名称自动切入点
可以用特定的文本值或通配符匹配目标对象的名称,并为满足条件的目标对象创建AOP代理。该类支持模式匹配字符串,如:"*name","name*",”*name*“和精确文本如"name"。我们可以通过下面这个简单的例子了解一下自动代理的功能。
- AOP 配置文件
- <object id="IValidateProxy" type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator, Spring.Aop">
- <property name="ObjectNames">
- <list>
- <value>*Validate</value>
- </list>
- </property>
- <property name="InterceptorNames" value="RoundInterceptor"></property>
- </object>
- 测试方法
- [TestMethod]
- public void ObjectNameAutoProxyCreatorMethodTest()
- {
- var context = ContextRegistry.GetContext();
- IDictionary validates = context.GetObjectsOfType(typeof(IValidateService));
- foreach (DictionaryEntry validate in validates)
- {
- ((IValidateService) validate.Value).Validate(null);
- }
- }
执