Java之——基于Spring AOP的注解实例

转载请注明出处:https://blog.csdn.net/l1028386804/article/details/80295629

一、Spring AOP切面基础知识

下面,我们先来讲解一下Spring AOP切面的基础知识。

 * 连接点(Joinpoint) :程序执行过程中的某一行为(方法),例如,UserService.get()的调用或者UserService.delete()抛出异常等行为。
      * 通知(Advice) :“切面”对于某个“连接点”所产生的动作,例如,TestAspect中对com.spring.service包下所有类的方法进行日志记录的动作就是一个Advice。其中,一个“切面”可以包含多个“Advice”,例如ServiceAspect。
      * 切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。例如,TestAspect中的所有通知所关注的连接点,都由切入点表达式execution(* com.spring.service.impl..*.*(..))来决定。
      * 目标对象(Target Object) :被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。
      * AOP代理(AOP Proxy) :在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将 <aop:config>的 proxy-target-class属性设为true 。

    通知(Advice)类型

      *前置通知(Before advice) :在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。

      *后通知(After advice) :当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。

      *返回后通知(After return advice) :在某连接点正常完成后执行的通知,不包括抛出异常的情况。

      *环绕通知(Around advice) :包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。

      *抛出异常后通知(After throwing advice) : 在方法抛出异常退出时执行的通知。

二、具体实现

1、定义接口UserService

package io.mykit.annotation.spring.aop.service;

/**
 * Spring AOP注解实例的Service
 * @author liuyazhuang
 *
 */
public interface UserService {
	
	void addUser();
}

2、接口实现类UserServiceImpl

package io.mykit.annotation.spring.aop.service.impl;

import org.springframework.stereotype.Service;

import io.mykit.annotation.spring.aop.service.UserService;

@Service("aopUserService")
public class UserServiceImpl implements UserService {
	@Override
	public void addUser() {
		System.out.println("执行addUser方法...");
	}

}

3、实现Spring AOP切面解析类Interceptor

package io.mykit.annotation.spring.aop.interceptor;

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

/**
 * 定义Spring AOP注解的
 * @author liuyazhuang
 *
 */
@Aspect//定义切面
@Component//声明这是一个组件
public class Interceptor {

	private static final Logger log = LoggerFactory.getLogger(Interceptor.class);
    
    /**
     * 这句话是方法切入点
     * 1 execution (* io.mykit.annotation.spring.aop.service.impl..*.*(..))
     * 2 execution : 表示执行
     * 3 第一个*号 : 表示返回值类型, *可以是任意类型
     * 4 io.mykit.annotation.spring.aop.service.impl : 代表扫描的包
     * 5 .. : 代表其底下的子包也进行拦截  
     * 6 第二个*号 : 代表对哪个类进行拦截,*代表所有类  
     * 7 第三个*号 : 代表方法  *代表任意方法
     * 8 (..) : 代表方法的参数有无都可以
     */
    //配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点
    @Pointcut("execution (* io.mykit.annotation.spring.aop.service.impl..*.*(..))")
    private void aspect() {
        System.out.println("============进入aspect方法==============");
    }
           
    //配置环绕通知,使用在方法aspect()上注册的切入点
    @Around("aspect()")
    public void around(JoinPoint joinPoint){
        long start = System.currentTimeMillis();
        try {
            ((ProceedingJoinPoint) joinPoint).proceed();
            long end = System.currentTimeMillis();
            if(log.isInfoEnabled()){
                log.info("around " + joinPoint + "\tUse time : " + (end - start) + " ms!");//这里顺便记录下执行速度,可以作为监控
            }
        } catch (Throwable e) {
            long end = System.currentTimeMillis();
            if(log.isInfoEnabled()){
                log.info("around " + joinPoint + "\tUse time : " + (end - start) + " ms with exception : " + e.getMessage());
            }
        }
    }
     //前置通知等可以没有JoinPoint参数
    @Before("aspect()")
    public void doBefore(JoinPoint joinPoint) {
        System.out.println("==========执行前置通知===============");
        if(log.isInfoEnabled()){
            log.info("before " + joinPoint);
        }
    }      
    //配置后置通知,使用在方法aspect()上注册的切入点
    @After("aspect()")
    public void doAfter(JoinPoint joinPoint) {
        System.out.println("===========执行后置通知==============");
        if(log.isInfoEnabled()){
            log.info("after " + joinPoint);
        }
    }  
    //配置后置返回通知,使用在方法aspect()上注册的切入点
    @AfterReturning("aspect()")
    public void afterReturn(JoinPoint joinPoint){
        System.out.println("===========执行后置返回通知==============");
        if(log.isInfoEnabled()){
            log.info("afterReturn " + joinPoint);
        }
    } 
    //配置抛出异常后通知,使用在方法aspect()上注册的切入点
    @AfterThrowing(pointcut="aspect()", throwing="ex")
    public void afterThrow(JoinPoint joinPoint, Exception ex){
         if(log.isInfoEnabled()){
             log.info("afterThrow " + joinPoint + "\t" + ex.getMessage());
         }
    }   
}

4、spring-aop-context.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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
 						http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 						http://www.springframework.org/schema/context 
 						http://www.springframework.org/schema/context/spring-context-4.0.xsd
 						http://www.springframework.org/schema/tx 
 						http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
 						http://www.springframework.org/schema/aop 
 						http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
 						http://www.springframework.org/schema/mvc 
						http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
						http://www.springframework.org/schema/cache 
						http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
						http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task-4.0.xsd"
						default-autowire="byName">
	 <mvc:annotation-driven />
     <!-- 激活组件扫描功能,在包io.mykit.annotation.spring.aop及其子包下面自动扫描通过注解配置的组件 -->
     <context:component-scan base-package="io.mykit.annotation.spring.aop" /> 
	
	 <!-- 启动对@AspectJ注解的支持 -->
     <!-- proxy-target-class等于true是强制使用cglib代理,proxy-target-class默认是false,如果你的类实现了接口 就走JDK代理,如果没有走cglib代理  -->
     <!-- 注:对于单利模式建议使用cglib代理,虽然JDK动态代理比cglib代理速度快,但性能不如cglib -->
     <aop:aspectj-autoproxy proxy-target-class="true"/>
     <context:annotation-config />
     <bean id="interceptor" class="io.mykit.annotation.spring.aop.interceptor.Interceptor"/> 
</beans>

5、创建测试类

package io.mykit.annotation.spring.aop.provider;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import io.mykit.annotation.spring.aop.service.UserService;

/**
 * 测试 Spring AOP注解
 * @author liuyazhuang
 *
 */
public class AOPAnnotationTest {
	
	@SuppressWarnings("resource")
	@Test
	public void testAOPAnnotation(){
		//启动Spring容器        
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"spring/spring-aop-annotation.xml"});
        UserService userService = (UserService) ctx.getBean("aopUserService");
        userService.addUser();
	}
}

6、pom.xml Jar包依赖

<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<skip_maven_deploy>false</skip_maven_deploy>
	<jdk.version>1.8</jdk.version>
	<spring.version>4.1.0.RELEASE</spring.version>
</properties>

<dependencies>
   
  <dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-expression</artifactId>
	    <version>${spring.version}</version>
	</dependency>	
	
		<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-messaging</artifactId>
	    <version>${spring.version}</version>
	</dependency>
	
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-jms</artifactId>
	    <version>${spring.version}</version>
	</dependency>
	
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aop</artifactId>
		<version>${spring.version}</version>
	</dependency>
	
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jdbc</artifactId>
		 <version>${spring.version}</version>
	</dependency>
	
	 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context-support</artifactId>
	    <version>${spring.version}</version>
	</dependency>
	
	<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    <dependency>
	    <groupId>org.aspectj</groupId>
	    <artifactId>aspectjtools</artifactId>
	    <version>1.9.1</version>
	</dependency>
	
	<dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>javax.servlet-api</artifactId>
	    <version>3.0.1</version>
	</dependency>

	<dependency>  
	    <groupId>org.slf4j</groupId>  
	    <artifactId>slf4j-log4j12</artifactId>  
	    <version>1.7.2</version>  
	</dependency> 
	
	  <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>
     <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.1_3</version>
        </dependency>
	
</dependencies>

7、log4j.properties

log4j.rootCategory=INFO,stdout,file,ERROR
#log4j.rootCategory=ERROR,stdout,file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout=org.apache.log4j.FileAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[mykit-annotation-spring-provider]%d %p [%t] %C{1}.%M(%L) | %m%n

#log4j.appender.file=org.apache.log4j.DailyRollingFileAppender 
#log4j.appender.file.MaxFileSize=100KB
#log4j.appender.file.DatePattern = '.'yyyy-MM-dd'.log'

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.file.File=/home/mykit/logs/mykit-annotation-spring-provider/mykit-annotation-spring-provider
log4j.appender.file.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} [%c-%L]-[%t]-[%p] %m%n

8、测试结果

==========执行前置通知===============
[mykit-annotation-spring-provider]2018-05-12 22:42:46,449 INFO [main] Interceptor.doBefore(66) | before execution(void io.mykit.annotation.spring.aop.service.impl.UserServiceImpl.addUser())
执行addUser方法...
[mykit-annotation-spring-provider]2018-05-12 22:42:46,461 INFO [main] Interceptor.around(52) | around execution(void io.mykit.annotation.spring.aop.service.impl.UserServiceImpl.addUser())	Use time : 14 ms!
===========执行后置通知==============
[mykit-annotation-spring-provider]2018-05-12 22:42:46,462 INFO [main] Interceptor.doAfter(74) | after execution(void io.mykit.annotation.spring.aop.service.impl.UserServiceImpl.addUser())
===========执行后置返回通知==============
[mykit-annotation-spring-provider]2018-05-12 22:42:46,462 INFO [main] Interceptor.afterReturn(82) | afterReturn execution(void io.mykit.annotation.spring.aop.service.impl.UserServiceImpl.addUser())

猜你喜欢

转载自blog.csdn.net/l1028386804/article/details/80295629