Spring AOP实现(API实现)

本文介绍了使用Spring框架实现AOP(面向切面编程)的三种方式:通过Spring API、前置通知与后置通知。首先引入必要的依赖jar包并配置命名空间。接着,通过实现MethodBeforeAdvice接口创建前置通知,并通过AfterReturningAdvice接口创建后置通知。最后,在配置文件中定义切入点和通知。

三种方式使用Spring实现AOP(实际上以后要自己单独写aop的机会也不会太多,但是也要了解Spring的aop)

第一种方式——使用Spring API实现


需要导入Spring AOP api的依赖jar包,可以在老的Spring lib(如2.5)里面找到
aopalliance.jar
aspectjweaver.jar
在beans.xml里面需要导入aop的命名空间并在XML catalog里面配置好

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

以上是准备工作,现在开始code
前置通知 实现MethodBeforeAdvice接口,里面有一个before方法
切面

public class Log implements  MethodBeforeAdvice{
    @Override
    public void before(Method method, Object[] arg1, Object target)
            throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"的方法被执行");

    }
}

目标对象

public class UserServiceImpl implements  UserService{

    @Override
    public void add() {
        System.out.println("增加用户");
    }

    @Override
    public void update() {
        System.out.println("修改用户");
    }

    @Override
    public void delete() {
        System.out.println("删除用户");
    }
    @Override
    public void search() {
        System.out.println("查询用户");

    }

}

配置文件

<bean id="userService"  class="cn.sxt.service.UserServiceImpl"></bean>
    <bean id="log"  class="cn.sxt.log.Log"></bean>  
    <aop:config>
        <aop:pointcut expression="execution(*  cn.sxt.service.UserServiceImpl.add())"  id="pointcut"></aop:pointcut>
        <aop:advisor advice-ref="log"  pointcut-ref="pointcut"/>
    </aop:config>

后置通知 实现AfterReturningAdvice接口

public class AfterLog implements  AfterReturningAdvice{
    //returnvalue  返回值
    @Override
    public void afterReturning(Object returnvalue, Method method, Object[] arg2,
            Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"的方法被成功执行"+"返回值是"+returnvalue);

    }
}

配置文件只需要增加一些配置

<bean id="afterlog"  class="cn.sxt.log.AfterLog"></bean>    
    <aop:config>
        <aop:pointcut expression="execution(*  cn.sxt.service.UserServiceImpl.add())"  id="pointcut"></aop:pointcut>
        <aop:advisor advice-ref="log"  pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterlog"  pointcut-ref="pointcut"/>
    </aop:config>

测试

public class Test {

    public static void main(String[] args) {
        ApplicationContext ac=new  ClassPathXmlApplicationContext("beans.xml");
        UserService  userService=(UserService)ac.getBean("userService");
        userService.add();
    }

}

结果

cn.sxt.service.UserServiceImpladd的方法被执行
增加用户
cn.sxt.service.UserServiceImpladd的方法被成功执行返回值是null

总结:

1 . 配置文件中

<aop:config>
        <aop:pointcut expression="execution(*  cn.sxt.service.UserServiceImpl.add())"  id="pointcut"></aop:pointcut>
        <aop:advisor advice-ref="log"  pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterlog"  pointcut-ref="pointcut"/>
    </aop:config>

aop:pointcut expression设置连接点,就是切入点。
第一个参数是返回值,*代表所有,类里面也可以设置成*代表所有,add()括号里面是参数,没有代表没有参数类型,..代表所有。也可以写成参数类型。所以可以写成

execution(*  cn.sxt.service.*.add())
execution(int  cn.sxt.service.*.add(int))

aop:advisor 配置通知对象 和切入点

2 . spring aop就是将公共的业务和领域业务结合。当执行领域业务时将会把公共业务加进来。实现公共业务的重复利用。领域业务更加纯粹。程序员专注于领域业务。其本质还是动态代理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值