zoukankan      html  css  js  c++  java
  • Spring4之AOP

    [www.dev1234.com]一头扎进Spring4视频教程一头扎进Spring4源码[www.java1234.com]《一头扎进Spring4》第七讲 源码

    [www.dev1234.com]一头扎进Spring4视频教程一头扎进Spring4源码[www.java1234.com]《一头扎进Spring4》第八讲 源码 

    配置:以下重点划出的是新添加进去的 

    <?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="studentServiceAspect" class="com.java1234.advice.StudentServiceAspect"></bean> //先创建好两个bean
        <bean id="studentService" class="com.java1234.service.impl.StudentServiceImpl"></bean>
        
        <aop:config>//config  aop标志
            <aop:aspect id="studentServiceAspect" ref="studentServiceAspect"> //aspect定义一个切面,studentServiceAspect是切面类
            //定义切点,是方法级别的,要写表达式{第一个*表示任何是任意,后面的两点..表示参数是任意的},指定方法
                <aop:pointcut expression="execution(* com.java1234.service.*.*(..))" id="businessService"/>
                <aop:before method="doBefore" pointcut-ref="businessService"/>
                <aop:after method="doAfter" pointcut-ref="businessService"/>
                <aop:around method="doAround" pointcut-ref="businessService"/>//环绕
                <aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>//返回
                <aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>//异常
            </aop:aspect> 
        </aop:config>
    package com.java1234.advice;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    
    public class StudentServiceAspect {
    
        public void doBefore(JoinPoint jp){
            System.out.println("类名:"+jp.getTarget().getClass().getName());
            System.out.println("方法名:"+jp.getSignature().getName());
            System.out.println("开始添加学生:"+jp.getArgs()[0]);
        }
        
        public void doAfter(JoinPoint jp){
            System.out.println("类名:"+jp.getTarget().getClass().getName());
            System.out.println("方法名:"+jp.getSignature().getName());
            System.out.println("学生添加完成:"+jp.getArgs()[0]);
        }
        
        public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
            System.out.println("添加学生前");
            Object retVal=pjp.proceed();
            System.out.println(retVal);
            System.out.println("添加学生后");
            return retVal;
        }
        
        public void doAfterReturning(JoinPoint jp){
            System.out.println("返回通知");
        }
        
        public void doAfterThrowing(JoinPoint jp,Throwable ex){
            System.out.println("异常通知");
            System.out.println("异常信息:"+ex.getMessage());
        }
    }
  • 相关阅读:
    按单生产案例
    【转】linux中执行外部命令提示" error while loading shared libraries"时的解决办法
    【转】WARNING! File system needs to be upgraded. You have version null and I want version 7. Run the '${HBASE_HOME}/bin/hbase migrate' script. 的解决办法
    根据Rowkey从HBase中查询数据
    【转】在一个Job中同时写入多个HBase的table
    sqoop 使用
    给VMware下的Linux扩展磁盘空间(以CentOS6.3为例)
    chrome 版本 29.0.1547.76 m 解决打开新标签页后的恶心页面的问题
    tomcat7+jdk的keytool生成证书 配置https
    如何打包和生成你的Android应用程序
  • 原文地址:https://www.cnblogs.com/rogge7/p/7026041.html
Copyright © 2011-2022 走看看