Spring AOP section of the configuration

Previous article on SpringAOP a simple practice, this chapter explains how to configure the section in the actual development.

A, Spring configuration shown ProxyFactory

1, Direct Advice to use all the methods applied to the target class

<!--配置Advice-->
<bean id="customAdvice" class=""/>
<!--目标对象-->
<bean id="target" class=""/>
<!--配置代理工厂-->
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"
    p:proxyInterfaces="com.xx.MyInterface" <!--指定代理的接口-->
    p:interceptorNames="customAdvice" <!--指定增强-->
    p:target-ref="target"/>  <!--指定要代理的bean-->

proxyInterfaces (interfaces): ask agent interface implemented, a plurality of available comma separated;
the interceptorNames: target object list, separated by commas plurality of available;
Singleton: return agent is a single embodiment, by default;
Optimize property set to true, enforce CGLib dynamic proxy.
You may also be used to indicate whether the attribute is proxyTargetClass proxy class interfaces instead of, when set to true, and the use of dynamic proxies no longer CGlib provided proxyInterfaces property, even if the set will be ignored;

In addition to the above mentioned plurality of reinforcing separated by commas, but also can be used as follows

<property name="interceptorNames">
    <list>
        <idref local="advice1" />
        <idref local="advice2" />
    </list>
</property>

2, cut by a custom application to enhance

<bean id="customAdvice" class=""/>
<bean id="customAdvisor" class="com.custom.xx"
    p:advice-ref="customAdvice"/>  <!--定义切面-->

<bean id="base" abstract="true"  <!--定义公共配置类-->
    class="org.springframework.aop.framework.ProxyFactoryBean"
    p:interceptorNames="customAdvisor" />  

<bean id="proxy1" parent="base" p:target-ref="" />
<bean id="proxy2" parent="base" p:target-ref="" />

3, through regular cut to enhance the application

<bean id="regexAdvisor" 
    class="org.springframework.aop.support.RegexpMethonPointcutAdvisor"
    p:advice-ref="customAdvice"
    <property name="patterns">
        <list>
            <value>.*insert*</value>
        </list>
    </property>
</bean>

<bean id="proxy"
    class="org.springframework.aop.framework.ProxyFactoryBean"
    p:interceptorNames="regexAdvisor" 
    p:target-ref=""
    p:proxyTargetClass="true"/>  

Two, Spring automatic proxy mechanism

Configure proxy object above configuration will be displayed, for small applications or go to the trouble, then it is acceptable, if a large face in large-scale system, through the above configuration to achieve error-prone, it is very the trouble, so Spring provides automatic proxy mechanism, allowing developers freed from disturbing configuration.

Automatic Proxy mechanism is based BeanPostProcessor implementations, examples being matched generating agent Bean creator automatic generation of rules in accordance with some examples of the container automatically Bean. The proxy creator which three major classes
1, based on Bean autoproxy configuration name rule creator: Bean allow specific configuration for a group name agent instance is created automatically, the implementation class of the BeanNameAutoProxyCreator
2, the agent automatically created based on matching mechanism Advisor is: it's all in a container Advisor scans automatically apply these facets to match the Bean (Bean to create a proxy instance of the target), for the implementation class the DefaultAdvisorAutoProxyCreator.
3, based on the Bean AspectJ annotation auto proxy creator: to Bean contains AspectJ annotations automatically create proxy instance, the implementation class is AnnotationAwareAspectJAutoProxyCreator.
Spring AOP section of the configuration

BeanNameAutoProxyCreator

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProyCreator" 
    p:beanNames="*er"  
    p:interceptorNames="" 
    p:optimize="true" />

DefaultAdvisorAutoProxyCreator

<bean id="regexAdrisor" class="" />
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"  />

AnnotationAwareAspectJAutoProxyCreator

1) XML configuration to enable AnnotationAwareAspectJAutoProxyCreator

<!--使用@AspectJ注解定义的切面类-->
<bean id="aspectJAnnotationBean" class="" />
<!--自动代理创建器,自动将@AspectJ注解切面类织入目标bean中-->
<bean class="org.springframework.aop.framework.autoproxy.AnnotationAwareAspectJAutoProxyCreator"  />

2) Use of aop Schema namespace configuration

<!--基于@AspectJ切面的驱动器-->
<aop:aspectj-autoproxy/>

<!--使用@AspectJ注解定义的切面类-->
<bean class="com.xx.customAspectJAdvisor" />

3) may be used to assemble manner annotations

<!-- 使用注解来装配(等同schema方式)-->
@EnableAspectJAutoProxy(exposeProxy=true)

<!--使用@AspectJ注解定义的切面类-->
<bean id="aspectJAnnotationBean" class="" />

Class Examples section:

@Aspect
@Component
public class CustomAspect {
    /*
        执行getName方法后执行
    */
    @AfterReturning(returning = "result", pointcut = "execution(* com.xx.getName())")
    public void singeLoginAfter(String result) throws Exception {
        //TODO
    }

    /*
        使用注解CustomAnnoation的方法执行后
    */
    @AfterReturning("@annotation(com.xx.annotation.CustomAnnoation)")
    public void refreshLinkGroupCache(JoinPoint joinPoint) {

    }
}

A plurality of enhancement (the Advice) woven into the sequence

1, if the reinforcing section in the same class stated, in accordance with the weaving order on the enhanced connection point is woven into
2, if the reinforcing section is located in a different class, and these cut classes implement org.springframework.core. Ordered interface, the notch sequence decision method (the first sequence number of small weaving)
3, if the reinforcing section is in a different class, and these implements do not cut class org.springframework.core.Ordered interface, the weaving uncertainty of the order

Guess you like

Origin blog.51cto.com/dengshuangfu/2412495