使用pojo+xml开发aop案例

来源:互联网 发布:sql numeric是什么类型 编辑:程序博客网 时间:2024/06/05 05:33
定义一个接口public interface EmployeeService { public void work(); public void work(String name,String pass);}新建一个实体类实现上述接口public class EmployeeServiceBean implements EmployeeService{@Overridepublic void work() {//int i=1/0;System.out.println("++++++++++++++++++工作中+++++++++++++++++");}@Overridepublic void work(String name,String pass) {//int i=1/0;System.out.println("++++++++++++++++++带参数的工作中+++++++++++++++++");}}定义一个切面类public class AdviceUtil {//具体通知-------进行安全检测public void checkLogin(String name,String pass){System.out.println("检查用户登录的用户名和密码。。。。。。。。。。。。");}//通知public void goHome(){System.out.println("...............回家................");}//通知public void getUp(){System.out.println("...............起床................");}//通知public Object punchCard(ProceedingJoinPoint jp) throws Throwable{System.out.println("===============上班打卡================");Object obj=jp.proceed();System.out.println("===============下班打卡================");return obj;}//通知public void back(){System.out.println("===============back方法================");}//通知public void unWell(Exception ex){System.out.println("===============unWell方法================"+ex.getMessage());}}在xml文件中配置<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd       http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" ><!-- 切面的bean --><bean id="adviceUtil" class="cn.csdn.advice.AdviceUtil"></bean><!-- 声明业务bean --><bean id="employeeServiceBean" class="cn.csdn.service.EmployeeServiceBean"></bean><!-- aop的配置     包括通知和切面--><aop:config><!-- 声明一个切入点 --><aop:pointcut expression="execution(* *.*work(..))" id="myPointcut"/><aop:pointcut expression="execution(* *.*work(..)) and args(name,pass)" id="myPointcut1"/><!--配置切面   引入的是具体实现的bean       order是优先级 --><aop:aspect ref="adviceUtil"><aop:before method="getUp" pointcut-ref="myPointcut"/><aop:before method="checkLogin" pointcut-ref="myPointcut1"/><aop:after method="goHome" pointcut-ref="myPointcut"/><aop:around method="punchCard" pointcut-ref="myPointcut"/><aop:after-returning method="back"  pointcut-ref="myPointcut"/><aop:after-throwing method="unWell"  pointcut-ref="myPointcut" throwing="ex"/><!-- 引入通知 --><aop:declare-parents types-matching="*..EmployeeServiceBean" implement-interface="cn.csdn.advice.Auditable" default-impl="cn.csdn.advice.AuditableAdvice"/></aop:aspect></aop:config></beans>

原创粉丝点击