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> 
      
      <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

 

转载于:https://www.cnblogs.com/zhangzonghua/p/8711368.html

你可能感兴趣的:(SpringAOP 使用注解的简单使用)