Spring——5种增强方式

一、前置增强

二、后置增强

三、环绕增强

   环绕增强相当于前置增强和后置增强的结合体,可以使用<aop:around>进行处理,这里我采用代理工厂的方式

  1.接口及其实现类

public interface ProService {
    public void doSome();
}
public class ProServiceImpl implements ProService {
    @Override
    public void doSome() {
        System.out.println("123");
    }
}

  2.增强类

public class around implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("before");
        Object proceed = methodInvocation.proceed();
        System.out.println("after");
        return proceed;
    }


}

  3.配置文件

    <!--注入bean-->
    <bean id="proService" class="cn.spring.around.ProServiceImpl"></bean>
    <!--切面-->
    <bean id="aroundAdvice" class="cn.spring.around.around"></bean>
    <!--代理工厂实现增强-->
    <bean id="ProFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--将增强和业务织入到一起-->
        <property name="target" ref="proService"></property>
        <!--拦截增强类-->
        <property name="interceptorNames" value="aroundAdvice"></property>
        <!--更换代理方式   默认值为false  jdk动态代理,当没有接口时,自动改成cglib-->
        <property name="proxyTargetClass" value="true"></property>
    </bean>

    或者使用aop:config

<!--将目标对象声明到Spring容器中-->
<bean id="doSomeService" class="com.cmy.service.impl.DoSomeServiceImpl"></bean>
<!-- 声明增强方法所在的Bean -->
<bean id="advice" class="com.cmy.around.AroundLogger"></bean>
<!-- 配置切面 -->
<aop:config>
   <aop:aspect ref="advice">
      <aop:around method="aroundLogger" pointcut="execution(* com.cmy.*.*.*(..))"/>
   </aop:aspect>
</aop:config>

四、异常增强

  异常增强处理,在目标方法抛出异常后织入;使用<aop:after-throwing>处理,这里我依旧采用代理工厂的方法

  1.接口及其实现类

public interface IdoSomeService {
    public void doSome() throws Exception;
}
/**
 * 原始对象
 */
public class IdoSomeServiceImpl implements IdoSomeService {
    public void doSome() throws Exception{
        int result=5/0;
        System.out.println("=========真实业务===========");
    }
}

  2.增强类

package cn.spring.throwadvice;

import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.ThrowsAdvice;

import java.lang.reflect.Method;

public class MyAdvice {
    public void afterThrowing(Exception ex){
        System.out.println("=====发生了异常,执行增强操作===============");
    }


}

  3.配置文件

<bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
          <!-- 将增强和业务织入到一起-->
            <property name="target" ref="idoSomeService"></property>
            <!--拦截增强类;-->
            <property name="interceptorNames" value="myThrowAdvice"></property>
           <!-- 更换代理方式       proxyTargetClass默认值为false     默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib -->
            <property name="proxyTargetClass" value="true"></property>
        </bean>

    或者采用aop:after-throwing

<aop:config>
        <aop:pointcut id="pointcut" expression="execution(* *..throwadvice.*.*(..))"/>
        <aop:aspect ref="myAdvice">
            <aop:after-throwing method="afterThrowing" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing>
           
    </aop:config>

五、最终增强

   无论方法是否抛出异常,都会在目标方法后做织入的增强处理,即该增强一定会执行,有点类似try-catch-finally块中的finally,一般用于释放资源。 使用<aop:after>处理最终增强。这里依旧运用代理工厂实现

  1.增强类

 public void afterAdvice(){
        System.out.println("======执行最终异常===============");
    }

  2.配置文件

<aop:config>
        <aop:pointcut id="pointcut" expression="execution(* *..throwadvice.*.*(..))"/>
        <aop:aspect ref="myAdvice">
            <aop:after method="afterAdvice" pointcut-ref="pointcut"></aop:after>
        </aop:aspect>
    </aop:config>

  

  

原文地址:https://www.cnblogs.com/xiao-ran/p/11760140.html