使用多种方式实现AOP

声明式增强

IdoSomeService接口

public interface IdoSomeService {
    public void doSome();
}

IdoSomeServiceImpl实现类

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

MyBeforeAdvice切面类

/*切面*/
public class MyBeforeAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] args, Object target) 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>

测试类

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

环绕增强 

IdoSomeService接口

public interface IdoSomeService {
    public void doSome();
}
IdoSomeServiceImpl实现类
/*原始对象*/
public class IdoSomeServiceImpl implements IdoSomeService {
    public void doSome(){
        System.out.println("=========真实业务===========");
    }
}
MyAroundAdvice增强类
/*环绕增强*/
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;
    }
}

测试类

public class ProxyFactoryTest {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取代理工厂Bean
        IdoSomeService idoSomeServiceProxy = (IdoSomeService)ctx.getBean("proxyFactory");
        idoSomeServiceProxy.doSome();
    }
}
异常增强
接口IdoSomeService
public interface IdoSomeService {
    public void doSome() throws Exception;
}
IdoSomeServiceImpl实现类
/*原始对象*/
public class IdoSomeServiceImpl implements IdoSomeService {
    public void doSome() throws Exception{
        int result=5/0;
        System.out.println("=========真实业务===========");
    }
}
MyAdvice类
public class MyAdvice {
    public void afterThrowing(Exception ex){
        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>

测试类
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");
    }
}

最终增强

接口IdoSomeService
public interface IdoSomeService {
    public void doSome() throws Exception;
}
IdoSomeServiceImpl实现类
/*原始对象*/
public class IdoSomeServiceImpl implements IdoSomeService {
    public void doSome() throws Exception{
        int result=5/0;
        System.out.println("=========真实业务===========");
    }
}
MyAdvice类
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>

测试类
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");
    }
}


原文地址:https://www.cnblogs.com/Chencheno/p/11759486.html