Aop(基于XML配置文件实现)

来源:互联网 发布:假如没有石油 知乎 编辑:程序博客网 时间:2024/05/22 20:34
接口类public interface UserService {    void queryUsers();    void saveUser();}
实现类public class UserServiceImp implements UserService {    @Override    public void queryUsers() {        System.out.println("用户查询");    }    @Override    public void saveUser() {        System.out.println("保存用户");    }}
通知类public class LogAdvice{    public void before() {        System.out.println("== before ==");    }    public void after() {        System.out.println("== after ==");    }    public void afterReturning(){        System.out.println("== afterReturning ==");    }    public void afterThrows(){        System.out.println("== afterThrows ==");    }    // 环绕通知    public void around(ProceedingJoinPoint joinPoint){        try {            System.out.println("== 环绕通知(前) ==");            joinPoint.proceed(); // 执行原方法            System.out.println("== 环绕通知(后) ==");        } catch (Throwable e) {            throw new RuntimeException(e);        }    }}
applicationContext.xml<?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:context="http://www.springframework.org/schema/context"    xmlns:tx="http://www.springframework.org/schema/tx" 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.5.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">        <!-- 配置UserService对象-->        <bean id="userService" class="cn.com.spring.l_aop_xml.UserServiceImp"></bean>        <!-- 配置一个通知对象 -->        <bean id="logAdvice" class="cn.com.spring.l_aop_xml.LogAdvice"></bean>        <!-- AOP有关的配置都 在aop:config中 -->        <aop:config>            <!-- 声明一个切面 -->            <aop:aspect ref="logAdvice">                <!-- 声明切入点 -->                 <aop:pointcut expression="execution(* save*(..))" id="myPointCut"/>                <!-- 指定在某切入点执行某操作 -->                <aop:after method="after" pointcut-ref="myPointCut"/>                <aop:before method="before" pointcut-ref="myPointCut"/>                <aop:after-returning method="afterReturning" pointcut-ref="myPointCut"/>                <aop:after-throwing method="afterThrows" pointcut-ref="myPointCut"/>                <aop:around method="around" pointcut-ref="myPointCut"/>            </aop:aspect>        </aop:config></beans>
测试类public class test {    // 使用Xml方式实现AOP        @Test        public void test() throws Exception {            ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml", getClass());            UserService userService = (UserService) app.getBean("userService");            System.out.println(userService.getClass());            userService.saveUser();            System.out.println();            userService.queryUsers();            System.out.println();        }}

注:通知类方法全部写上后的执行顺序
== before ==
== 环绕通知(前) ==
保存用户
== after ==
== afterReturning ==
== 环绕通知(后) ==

用户查询

0 0
原创粉丝点击