zoukankan      html  css  js  c++  java
  • 通知advice

    基于注解的Spring AOP开发,来自https://www.cnblogs.com/junzi2099/p/8274813.html

    1.定义目标类接口和实现类

    2.编写Spring AOP的aspect 类

    @Aspect
    public class MyAspect {
    
        /**
         * 前置通知
         */
        @Before("execution(* com.zejian.spring.springAop.dao.UserDao.addUser(..))")
        public void before(){
            System.out.println("前置通知....");
        }
    
        /**
         * 后置通知
         * returnVal,切点方法执行后的返回值
         */
        @AfterReturning(value="execution(* com.zejian.spring.springAop.dao.UserDao.addUser(..))",returning = "returnVal")
        public void AfterReturning(Object returnVal){
            System.out.println("后置通知...."+returnVal);
        }
    
    
        /**
         * 环绕通知
         * @param joinPoint 可用于执行切点的类
         * @return
         * @throws Throwable
         */
        @Around("execution(* com.zejian.spring.springAop.dao.UserDao.addUser(..))")
        public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
            System.out.println("环绕通知前....");
            Object obj= (Object) joinPoint.proceed();
            System.out.println("环绕通知后....");
            return obj;
        }
    
        /**
         * 抛出通知
         * @param e
         */
        @AfterThrowing(value="execution(* com.zejian.spring.springAop.dao.UserDao.addUser(..))",throwing = "e")
        public void afterThrowable(Throwable e){
            System.out.println("出现异常:msg="+e.getMessage());
        }
    
        /**
         * 无论什么情况下都会执行的方法
         */
        @After(value="execution(* com.zejian.spring.springAop.dao.UserDao.addUser(..))")
        public void after(){
            System.out.println("最终通知....");
        }
    }
    View Code

    3.编写配置文件交由Spring IOC容器管理

    <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"
           xmlns:context="http://www.springframework.org/schema/context"
           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">
    
        <!-- 启动@aspectj的自动代理支持-->
        <aop:aspectj-autoproxy />
    
        <!-- 定义目标对象 -->
        <bean id="userDaos" class="com.zejian.spring.springAop.dao.daoimp.UserDaoImp" />
        <!-- 定义aspect类 -->
        <bean name="myAspectJ" class="com.zejian.spring.springAop.AspectJ.MyAspect"/>
    </beans>
    View Code

    4.编写测试类

    /**
     * Created by zejian on 2017/2/19.*/
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations= "classpath:spring/spring-aspectj.xml")
    public class UserDaoAspectJ {
        @Autowired
        UserDao userDao;
    
        @Test
        public void aspectJTest(){
            userDao.addUser();
        }
    }
    View Code

    5.结果

      


  • 相关阅读:
    myeclipse启动后,卡在loading workbench界面
    oracle数据库导入dmp文件
    ORA-28009: 应当以 SYSDBA 身份或 SYSOPER 身份建立 SYS 连接
    debug启动项目很慢
    CSS利用border绘制图形
    HTML创建链接框
    CSS实现单行文本溢出显示省略号
    HTML5中的Web Worker
    HTML拖放元素
    Canvas和SVG的比较
  • 原文地址:https://www.cnblogs.com/yunhemeihe/p/12080178.html
Copyright © 2011-2022 走看看