Spring AOP实现(自定义类)

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

第二种方式——自定义类 相对来说这种用的多点,文档中好多用这种方式
需要导入Spring AOP api的依赖jar包,可以在老的Spring lib(如2.5)里面找到
aopalliance.jar
aspectjweaver.jar
在beans.xml里面需要导入aop的命名空间并在XML catalog里面配置好

切面不用实现任何接口


public class Log{
    public void before(){
        System.out.println("------方法执行前--------");
    }
    public void after(){
        System.out.println("------方法执行后--------");
    }

}

beans.xml

<bean id="userService"  class="cn.sxt.service.UserServiceImpl"></bean>
    <bean id="log"  class="cn.sxt.log.Log"></bean>  
    <aop:config>
        <aop:aspect  ref="log">
            <aop:pointcut expression="execution(*  cn.sxt.service.*.*(.. ))"  id="pointcut"/>
            <aop:before method="before"  pointcut-ref="pointcut"/>
            <aop:after method="after"  pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

配置文件解析:

<aop:config>

里面

    <aop:aspect  ref="log">

ref引用了通知对象

<aop:pointcut expression="execution(*  cn.sxt.service.*.*(.. ))"  id="pointcut"/>

表示横切点

<aop:before method="before"  pointcut-ref="pointcut"/>

前置通知 通知对象里的方法 横切点引用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值