SpringAOP进阶

利用代理工厂实现增强

com.Spring.proxyfactory中的IdoSomeService

package cn.spring.proxyfactory;

public interface IdoSomeService {
    public void doSome();
}

IdoSomeServiceImpl类

package cn.spring.proxyfactory;

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

MyBeforeAdavice类

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class MyBeforeAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("==============前置增强=================");
    }
}

applicationContext.xml

<!--注入业务Bean-->
       <bean id="idoSomeService" class="com.Spring.proxyfactory.IdoSomeServiceImpl"></bean>
       <!--增强:切面-->
        <bean id="myBeforeAdvice" class="com.Spring.proxyfactory.MyBeforeAdvice"></bean>
        <!--使用代理工厂实现增强-->
        <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
            <!--将增强和业务织入到一起-->
            <property name="target" ref="idoSomeService"></property>
            <!--拦截增强类-->
            <property name="interceptorNames" value="myBeforeAdvice"></property>
            <!--更换代理方式   proxyTargetClass默认值为false   默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib-->
            <property name="proxyTargetClass" value="true"></property>
        </bean>

ProxyFactoryTest测试类

package com.Spring;

import com.Spring.proxyfactory.IdoSomeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Around {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取代理工厂Bean
        IdoSomeService idoSomeService = (IdoSomeService)ctx.getBean("proxyFactory");
        idoSomeService.doSome();
    }
}

环绕增强

com.Spring.around中的IdoSomeService接口

package cn.spring.around;

public interface IdoSomeService {
    public void doSome();
}

IdoSomeServiceImpl类

package cn.spring.around;
/**
 * 原始对象
 */
public class IdoSomeServiceImpl implements IdoSomeService {
    public void doSome(){
        System.out.println("=========真实业务===========");
    }
}

MyAroundAdvice类

package cn.spring.around;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * 环绕增强
 */
public class MyAroundAdvice implements MethodInterceptor{

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("============环绕前=============");

        //调用核心业务方法      也可以获取方法内的参数   也可以获取目标对象
        Object proceed = invocation.proceed();
        Object aThis = invocation.getThis();
        System.out.println(aThis);

        System.out.println("============环绕后=============");
        return proceed;
    }
}

AroundTest测试类

package cn.spring;

import cn.spring.throwadvice.IdoSomeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AroundTest {
    public static void main(String[] args){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取代理工厂Bean
        IdoSomeService idoSomeServiceProxy = (IdoSomeService) ctx.getBean("idoSomeService");

        try {
            idoSomeServiceProxy.doSome();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("1231231231131312123");
    }
}

applicationContext.xml文件

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



     <aop:config>
         <aop:pointcut id="pointcut" expression="execution(* *..around.*.*(..))"/>
         <aop:aspect ref="myAroundAdvice">
             <aop:around method="around" pointcut-ref="pointcut"></aop:around>
         </aop:aspect>
     </aop:config>

最终异常

com.Spring.throwadvice中的IdoSomeService接口

package cn.spring.throwadvice;

public interface IdoSomeService {
    public void doSome() throws Exception;
}

IdoSomeServiceImpl类

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

MyAdvice类

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("=====发生了异常,执行增强操作===============");
    }

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

applicationContext.xml文件

<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:after method="afterAdvice" pointcut-ref="pointcut"></aop:after>
        </aop:aspect>
    </aop:config>

原文地址:https://www.cnblogs.com/dabrk/p/11758766.html