SpringAOP 使用注解的简单使用

Posted zhangzonghua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringAOP 使用注解的简单使用相关的知识,希望对你有一定的参考价值。

1. 导入jar包

/SpringAOPmy/lib/com.springsource.net.sf.cglib-2.2.0.jar
/SpringAOPmy/lib/com.springsource.org.aopalliance-1.0.0.jar
/SpringAOPmy/lib/com.springsource.org.aspectj.tools-1.6.6.RELEASE.jar
/SpringAOPmy/lib/commons-logging.jar
/SpringAOPmy/lib/junit-4.8.2.jar
/SpringAOPmy/lib/spring-aop-4.1.0.RELEASE.jar
/SpringAOPmy/lib/spring-aspects-4.1.0.RELEASE.jar
/SpringAOPmy/lib/spring-beans-4.1.0.RELEASE.jar
/SpringAOPmy/lib/spring-context-4.1.0.RELEASE.jar
/SpringAOPmy/lib/spring-core-4.1.0.RELEASE.jar
/SpringAOPmy/lib/spring-expression-4.1.0.RELEASE.jar

 

2.配置文件

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
      <!-- 开启自动扫描 -->
      <context:component-scan base-package="com.aspect.annonaction"></context:component-scan> 
      <!--Spring 中使用aspectj 包中的@Aspect 注解标注的当前组件为切面 
       -->
      <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

3. 注解切面方法

package com.aspect.annonaction;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
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.springframework.stereotype.Component;

@Component
@Aspect
public class AOP {
    @Before("execution(* com.aspect.annonaction.Testss.say())")
    public void before() {
        System.out.println("前置通知");
    }
    
    
    @AfterReturning(pointcut="within(com.aspect.annonaction.Testss)",returning="obj")
    public void afterreturning(JoinPoint jp,Object obj) {
        System.out.println(jp.getSignature());
        System.out.println("返回值的类型:"+obj);
    }
    
    @AfterThrowing(pointcut="execution(* com.aspect.annonaction.Testss.say())",throwing="e")
    public void exception(Throwable e) {
        System.out.println("异常通知:"+e.getMessage());
    }
    
    @Around(value="within(com.aspect.annonaction.Testss)")
    public void around(ProceedingJoinPoint pjp) {
        System.out.println("前置通知");
        try {
            pjp.proceed();
            System.out.println("后置通知");
        } catch (Throwable e) {
            System.out.println("异常通知");
            e.printStackTrace();
        }
        System.out.println("最终通知");
        
        
    }
    
    
}

4. 测试

package com.aspect.annonaction;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;


@Component
public class Testss {

    public void say() {
        System.out.println("张韶涵");
    }
    
    
    @Test
    public void test1 () {
        ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext2.xml");
        Testss t=(Testss) ac.getBean("testss");
        System.out.println(t);

        t.say();
        
    }

}

end

 











以上是关于SpringAOP 使用注解的简单使用的主要内容,如果未能解决你的问题,请参考以下文章

SpringAOP 使用注解的简单使用

springAop 使用@Around,@After等注解时,代码运行两边的问题

SpringBoot使用SpringAOP实现日志审计等功能

SpringAOP02 自定义注解

SpringAOP+自定义注解实现日志记录

SpringAOP+自定义注解实现日志记录