zoukankan      html  css  js  c++  java
  • spring aop的xml设置

    1.aop可以在不更改代码的情况下,在代码执行前后增加方法。可以方便的提高效率。 例如在查看方法性能时、追加日志、增加权限等

    测试类

    package springdemo.demo4;
    
    import org.springframework.stereotype.Component;
    
    
    public class studentDao {
        public void save(){
            System.out.println("保存");
        }
        void update(){
            System.out.println("更新");
        }
        public String delete(){
            System.out.println("删除");
            return "删除了哈。。";
        }
        void search(){
            System.out.println("查询");
            int i =1/0;
        }
    }
    

      aop 代理

    package springdemo.demo4;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    
    /**
     * 切面类
     */
    public class MyAspectXml {
        /**
         * 前置通知 可以获取切入点信息
         * @param joinPoint
         */
        public void chekPri(JoinPoint joinPoint){
            System.out.println("权限校验" + joinPoint);
        }
    
        /**
         * 后置通知 可以获取返回值内容
         * @param result
         */
        public void wlog(Object result){
            System.out.println("日志记录。。。" + result);
        }
    
        /**
         * 环绕通知  -性能监控
         * @param joinPoint
         * @return
         * @throws Throwable
         */
        public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
            System.out.println("环绕前通知");
            Object object = joinPoint.proceed(); //执行目标程序
            System.out.println("环绕后通知");
            System.out.println("总共执行了3s");
            return object;
        }
    
        /**
         * 异常抛出
         */
        public void afterThrow(Throwable ex){
            System.out.println("异常抛出.." + ex.getMessage());
        }
    
        /**
         * 最终通知,相当于 finally
         */
        public void after(){
            System.out.println("最终通知...");
        }
    }

    xml配置

    <?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
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--配置扫描(哪些包下需要使用注解)-->
        <!--
        <context:component-scan base-package="springdemo.demo4"/>
        -->
    
        <bean id="studentDao" class="springdemo.demo4.studentDao"></bean>
    
        <!--将切面类交给spring管理-->
        <bean id="myAspect" class="springdemo.demo4.MyAspectXml"/>
        <!--通过AOP配置完成对目标产生的代理-->
        <aop:config>
            <!--表达式配置哪些类哪些方式需要增强-->
    
            <!--配置切点,需要运用切面的方法-->
           <aop:pointcut id="pointcut1" expression="execution(* springdemo.demo4.studentDao.save(..))"></aop:pointcut>
            <aop:pointcut id="pointcut2" expression="execution(* springdemo.demo4.studentDao.delete(..))"></aop:pointcut>
            <aop:pointcut id="pointcut3" expression="execution(* springdemo.demo4.studentDao.update(..))"></aop:pointcut>
            <aop:pointcut id="pointcut4" expression="execution(* springdemo.demo4.studentDao.search(..))"></aop:pointcut>
            <!--配置切面-->
            <aop:aspect ref="myAspect">
                <!--前置通知-->
                <aop:before method="chekPri" pointcut-ref="pointcut1"/>
                <!--后置通知-->
                <aop:after-returning method="wlog" pointcut-ref="pointcut2" returning="result"/>
                <!--环绕通知-->
                <aop:around method="around" pointcut-ref="pointcut3"/>
                <!--异常抛出通知-->
                <aop:after-throwing method="afterThrow" pointcut-ref="pointcut4" throwing="ex"/>
                <!--最终通知-->
                <aop:after method="after" pointcut-ref="pointcut4"/>
            </aop:aspect>
        </aop:config>
    </beans>
  • 相关阅读:
    Android -- SEGV_MAPERR,SEGV_ACCERR
    使用预编译库PREBUILT LIBRARY官方说明
    Application.mk文件官方使用说明
    Android.mk文件官方使用说明
    ndk-build官方使用说明
    cocos中lua使用shader实例
    Wifi 攻击科普
    狠心把小米笔记本的操作系统换成了kali
    端口转发正反向链接 NC 和 SSH下的用法
    linux下无回显可将回显发送到服务器
  • 原文地址:https://www.cnblogs.com/xiaoyii/p/9781318.html
Copyright © 2011-2022 走看看