zoukankan      html  css  js  c++  java
  • AOP

    /**
     * 需要被增强功能的类
     */
    public class UserServiceImpl implements UserService {
        @Override
        public void add() {
            System.out.println("添加一个用户");
        }
    
        @Override
        public void del() {
            System.out.println("删除一个用户");
    
        }
    
        @Override
        public void update() {
            System.out.println("修改一个用户");
    
        }
    
        @Override
        public void query() {
            System.out.println("查询一个用户");
    
        }
    }
    public class AfterLog implements AfterReturningAdvice {
        /**
         * 如何定位:那个类,那个方法
         * @param o       返回值
         * @param method  目标方法
         * @param objects 参数类型
         * @param o1      执行对象
         * @throws Throwable
         */
        public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
            System.out.println("AfterLog 执行方法为:" + method + ",返回值为:" + o);
        }
    }
    public class BeforeLog implements MethodBeforeAdvice {
        /**
         *
         * @param method 执行目标的方法
         * @param objects 方法参数
         * @param o 目标
         * @throws Throwable
         */
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println("BeforeLog : 那个类:"+o.getClass().getName()+"的,那个方法:"+method.getName());
        }
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd">
        <bean id="beforeLog" class="com.nbg.log.BeforeLog"/>
        <bean id="afterLog" class="com.nbg.log.AfterLog"/>
        <bean id="usi" class="com.nbg.service.impl.UserServiceImpl"/>
        <!--使用原生spring api接口-->
        <aop:config>
            <!--execution(修饰词 返回值 类名 方法名 参数)-->
            <aop:pointcut id="pit" expression="execution(* com.nbg.service.impl.UserServiceImpl.*(..))"/>
            <!--执行环绕增强 将advice-ref 切入到pointcut-ref中-->
            <aop:advisor advice-ref="afterLog" pointcut-ref="pit"/>
            <aop:advisor advice-ref="beforeLog" pointcut-ref="pit"/>
        </aop:config>
    </beans>

    一:如何在不改动原有代码的基础上,增强其功能?

    二:该怎么做?

    三:使用代理模式增强被代理对象的功能

    自定义:

    public class DiyPoint {
        public void before() {
            System.out.println("before");
        }
    
        public void after() {
            System.out.println("after");
        }
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd">
        <bean id="usi" class="com.nbg.service.impl.UserServiceImpl"/>
        <bean id="diy" class="com.nbg.log.DiyPoint"/>
        <aop:config>
            <!--自定义切面 ref:要引用的类-->
            <aop:aspect ref="diy">
                <!--需要切人点-->
                <aop:pointcut id="point" expression="execution(* com.nbg.service.impl.UserServiceImpl.*(..))"/>
                <!--通知-->
                <aop:after method="after" pointcut-ref="point"/>
                <aop:before method="before" pointcut-ref="point"/>
            </aop:aspect>
        </aop:config>
    </beans>
    方式三:注解
    @Aspect //标注这个类为一个切面
    public class Anno {
        @Before("execution(* com.nbg.service.impl.UserServiceImpl.*(..))")
        public void before() {
            System.out.println("方法执行    前");
        }
    
        @After("execution(* com.nbg.service.impl.UserServiceImpl.*(..))")
        public void after() {
            System.out.println("方法执行    后");
        }
    
        @Around("execution(* com.nbg.service.impl.UserServiceImpl.*(..))")
        public void around(ProceedingJoinPoint jp) throws Throwable {
            System.out.println("环绕  前");
            Object proceed = jp.proceed();//执行方法,如果不开启,则就只有这个方法执行,其余如before,after,add等方法不会执行;
            System.out.println("环绕  后");
        }
    }
        <bean id="usi" class="com.nbg.service.impl.UserServiceImpl"/>
        <bean id="anno" class="com.nbg.log.Anno"/>
        <!--开启自动注解-->
        <aop:aspectj-autoproxy/>
     
  • 相关阅读:
    C. Dima and Salad 背包好题
    centos7下查看cup核数
    code码说明
    数据库慢查询
    centos7重启Mysql命令
    many connection errors,更改max_connection_errors的值
    CentOS7 linux下yum安装redis以及使用
    django Warning: (3135, "'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes
    linux命令
    linux命令解压压缩rar文件的详细步骤
  • 原文地址:https://www.cnblogs.com/NBG-SDL/p/14122681.html
Copyright © 2011-2022 走看看