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

本文展示了如何使用XML方式实现Spring AOP,并通过配置UserService对象和通知类来实现前置、后置、返回和异常通知。实现了UserService接口的两个方法,并在saveUser方法上配置了环绕通知,演示了通知类方法的执行顺序。
接口类
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 ==
== 环绕通知(后) ==

用户查询

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值