zoukankan      html  css  js  c++  java
  • 使用多种方式实现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");
        }
    }


  • 相关阅读:
    Dapr Actor 的微服务架构
    社区 正式发布了跨平台的 CoreWCF 0.1.0 GA
    使用 Tye 辅助开发 dotnet 应用程序
    Dapr 交通流量控制示例
    Dapr是如何简化微服务的开发和部署
    牛年 dotnet云原生技术趋势
    Dapr 已在塔架就位 将发射新一代微服务
    Raden Blazor 组件以MIT 开源
    ASP Net Core – CORS 预检请求
    如何在 Blazor WebAssembly中 使用 功能开关
  • 原文地址:https://www.cnblogs.com/Chencheno/p/11759486.html
Copyright © 2011-2022 走看看