spring aop 功能初次使用(注解方式)

前期准备

spring3.0版本发布包里没有包含相应的依赖包,所以需手动加入,如下
aspectjrt.jar
aspectjweaver.jar
aopalliance-1.0.jar

要想使用spring aop 须配置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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.0.xsd  
           http://www.springframework.org/schema/aop  
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

<!--

开启自动扫描功能,如果类包含 @Component, @Repository, @Service, and @Controller,@Required, @Autowired, @PostConstruct, @PreDestroy, @Resource, @PersistenceContext and
 @PersistenceUnit这些注解,将会被spring容易管理,并且提供相应的注入

-->
    <context:component-scan base-package="sping.*"></context:component-scan>

<!--

开启基于@AspectJ 的aop,proxy-target-class默认值为false,基于接口的代理,否则是基于类的代理,依赖cglib包

-->
    <aop:aspectj-autoproxy  proxy-target-class="false"></aop:aspectj-autoproxy>
</beans>

声明一个aop类

java代码

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component(value="logIntercept")
@Aspect()
public class InterceptOfAspectj {

    /**
     * 声明一个切入点,表达式为 "拦截所有返回值类型,所有包下所有方法"
     */
    @Pointcut(value="execution(* sping.beans.service..*.*(..))")
    public void anyMothed(){
       
    }
   
    @Before(value="anyMothed()")
    public void before(){
        System.out.println("前置通知");
    }
   
    @Before(value="anyMothed()&&args(myname)")
    public void beforeParam(String myname){
        System.out.println("前置通知"+myname);
    }
    @AfterReturning(pointcut="anyMothed()",returning="result")
    public void afrerReturn(Object result){
        System.out.println("后置通知"+result);
    }
    @After(value="anyMothed()")
    public void after(){
        System.out.println("最后后置通知");
    }
    @AfterThrowing(pointcut="anyMothed()",throwing="e")
    public void afterThrows(Exception e){
        System.out.println("异常通知:"+e.getMessage());
    }
    @Around("anyMothed()") 
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{ 
        System.out.println("环绕通知  开始"); 
        Object obj = pjp.proceed(); 
        System.out.println("环绕通知  结束"); 
        return obj; 
    } 
   
}

转载于:https://my.oschina.net/jiangtao1314/blog/38629

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值