使用注解创建通知

使用注解创建通知

前面讲了创建通知,现在用注解来创建通知

具体案例分析

public interface Service {

    /**去公司*/

    public void goCompany();

    /**签到*/

    public void signIn();

    /**请假*/

    public void leave(Exception ex);

    /**回家*/

    public void goHome();

}

@Aspect//@Aspect是声明切面

public class ServiceImpl implements Service{

    /**

     *  第一个* 代表方法的返回值

     *  第二个work()代表的是方法的名称

     * */

//前置通知

    @Before("execution(* work())"

    public void goCompany() {System.out.println("去公司。。。");}

//前置通知

    @Before("execution(* EmpServiceImpl.*(..))")

    public void signIn() {

       System.out.println("签到");

    }

    /**

     * 第一个*代表的是方法的返回值

     * 第二个cn.csdn.service.Emp* 位于cn.csdn.service包中前缀是Emp的所有类

     * 第三个Emp*.后的*代表的是这个类中的所有方法

     * 第四个(..)代表的是方法的参数可以是可变的参数

     * */

//后置通知

    @After("execution(* cn.csdn.service.Emp*.*(..))")

    public void goHome() {

       System.out.println("回家。。。");

    }

//异常通知

@AfterThrowing(pointcut="execution(* *..EmpService*.*(..))",throwing="ex")

    public void leave(Exception ex) {

       System.out.println("请假"+ex.getMessage());

    }

}

public interface EmpService {

    void work();

}

public class EmpServiceImpl implements EmpService{

    @Override

    public void work() {

       System.out.println("员工正在工作。。。。");

    }

}

public interface AroundService {

    public Object eat(ProceedingJoinPoint pj);

}

//环绕通知切面的具体实现

@Aspect

public class AroundServiceImpl implements AroundService{

    @Around("execution(* EmpService.*(..))")

    public Object eat(ProceedingJoinPoint pj) {

       System.out.println("吃完饭就去上班。。。");

       try {

           Object ob=pj.proceed();

    // 注:proceed()方法会调用目标方法

           System.out.println("工作后要签到的。。。");

           return ob;

       } catch (Throwable e) {

           System.out.println("下班回家喽。。。");

           e.printStackTrace();

       }

       return null;

    }

}

public class App {

@Test

public void test(){

/*解析xml文件创建容器对象*/

ApplicationContext ac=new ClassPathXmlApplicationContext("app*.xml");

/*接口代理*/

    EmpService empService=(EmpService)ac.getBean("empServiceImpl");

    empService.work();

    }

}

<?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-2.0.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">

<!—注意一定要配xmlns:aop="http://www.springframework.org/schema/aop"-->

<!-- aspectjbean -->

<bean id="serviceImpl" class="cn.csdn.service.ServiceImpl"/>

<!-- 环绕通知的切面的具体实现的bean -->

<bean id="aroundServiceImpl" class="cn.csdn.service.AroundServiceImpl"/>

<!-- 业务操作的bean -->

<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"/>

    <!—注意:一定要启用Spring@AspectJ的支持 -->

    <aop:aspectj-autoproxy/>

</beans>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值