首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

施用注解创建通知

2012-09-23 
使用注解创建通知使用注解创建通知前面讲了创建通知,现在用注解来创建通知具体案例分析public interface S

使用注解创建通知

使用注解创建通知

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

具体案例分析

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"-->

<!-- aspectj的bean -->

<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>

?

热点排行