Spring學(xué)習(xí)心得(五)
1. 對advice細(xì)粒度的控制,可以通過pointcut類來控制。
2. advisor是advice和pointcut的集合體。
3. 控制方法如下:
a) 定義Pointcut中的ClassFilter實(shí)現(xiàn)類和MethodMatcher實(shí)現(xiàn)類,完成對class和method方法的advice的控制,其中MethodMatcher中的matches(Method method, Class targetClass)方法是對class和method的雙重并集控制
b) 定義新的Pointcut實(shí)現(xiàn)類,并將ClassFilter實(shí)現(xiàn)類和MethodMatcher實(shí)現(xiàn)類賦值
c) 定義新的advisor并且實(shí)現(xiàn)PointcutAdvisor接口
d) 在定義的advisor中使用PointcutAdvisor接口中的getPointcut()和getAdvice()將Pointcut的實(shí)現(xiàn)類和需要的advice進(jìn)行傳遞
e) 最后在application-context.xml中將
<bean id="XXXX"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="InterceptorNames">
<list>
<idref local="定義新的advisor的id">
</list>
<property>
</bean>
<bean id="定義新的advisor的id" class="定義新的advisor的類 " >
</bean>
4. 為了使用靜態(tài)的Pointcut,可以使用StaticMethodMatcherPointcut,這時(shí)候
public final boolean isRuntime()
{
return false;
}
便不能修改了。也就是靜態(tài)的Pointcut
5. 更簡單的是使用NameMatchMethodPointcutAdvisor,這里一個(gè)advisor只有一個(gè)advice
v