Spring(九):Spring AOP Annotation

要使用注解方式的Spring AOP,首先,我们要在spring的xml配置文件中加上使用Spring AOP的标签'<aop:aspectj-autoproxy />'。

配置示例:

<?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-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<context:annotation-config/>
    <context:component-scan base-package="com.castiel"/>  
    <!-- 注解方式定义Spring AOP -->
    <aop:aspectj-autoproxy />
</beans>

接下来,我们定义一个剖面类,名称为'LogInterceptor',代码如下:

package com.castiel.aop;

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;

@Aspect
@Component
public class LogInterceptor {
	/**
	 * 如果类实现了接口,例如'UserDAOImpl'实现了'UserDAO',可以用这种方式生成代理,但是
	 * 如果像'UserService'这种没有实现接口的类,必须要用直接操作二进制代码的类库来产生代理,例如cglib
	 * 加入cglib的jar包即可解决这种问题,但是Spring 4.0之后已经封装了cglib(在spring-core-4.x.x.RELEASE.jar中),所以4.0之后的Spring不用导入
	 * 此包
	 */
	
	@Pointcut("execution(public void com.castiel.dao.impl.UserDAOImpl.save(com.castiel.model.User))")
	public void myMethod(){}
	
	//@Before("execution(public void com.castiel.dao.impl.UserDAOImpl.save(com.castiel.model.User))")
	@Before("myMethod()")
	public void before(){
		System.out.println("before...");
	}
	
	//@AfterReturning("execution(public void com.castiel.dao.impl.UserDAOImpl.save(com.castiel.model.User))")
	@AfterReturning("myMethod()")
	public void afterReturning(){
		System.out.println("after returning...");
	}
	
	//@AfterThrowing("execution(public void com.castiel.dao.impl.UserDAOImpl.save(com.castiel.model.User))")
	@AfterThrowing("myMethod()")
	public void afterThrowing(){
		System.out.println("after throwing...");
	}
	
	//@After("execution(public void com.castiel.dao.impl.UserDAOImpl.save(com.castiel.model.User))")
	@After("myMethod()")
	public void afterMethod(){
		System.out.println("finally...");
	}
	
	//@Around("execution(public void com.castiel.dao.impl.UserDAOImpl.save(com.castiel.model.User))")
	@Around("myMethod()")
	public void aroundMethod(ProceedingJoinPoint pdp) throws Throwable{
		System.out.println("around start...");
		pdp.proceed();
		System.out.println("around end...");
	}
	
	
	//测试是否是代理对象
	@Before("execution(public void com.castiel.service.UserService.add(com.castiel.model.User))")
	public void userServiceAddBefore(){
		System.out.println("userService add before...");
	}
}

使用注解@Component注解声明这是一个由Spring管理的bean,这样才能被Spring容器初始化,再使用@Aspect注解声明这是一个剖面类。

这里我们首先讲讲@Aspect的详细用法:

Spring AOP中目前只有执行方法这一个连接点,Spring AOP支持的AspectJ切入点指示符如下:

一些常见的切入点的例子 
execution(public * * (. .)) 任意公共方法被执行时,执行切入点函数。 
execution( * set* (. .)) 任何以一个“set”开始的方法被执行时,执行切入点函数。 
execution( * com.demo.service.AccountService.* (. .)) 当接口AccountService 中的任意方法被执行时,执行切入点函数。 
execution( * com.demo.service.. (. .)) 当service 包中的任意方法被执行时,执行切入点函数。 within(com.demo.service.) 在service 包里的任意连接点。 within(com.demo.service. .) 在service 包或子包的任意连接点。 
this(com.demo.service.AccountService) 实现了AccountService 接口的代理对象的任意连接点。 
target(com.demo.service.AccountService) 实现了AccountService 接口的目标对象的任意连接点。 
args(Java.io.Serializable) 任何一个只接受一个参数,且在运行时传入参数实现了 Serializable 接口的连接点 
增强的方式: 
@Before:方法前执行 
@AfterReturning:运行方法后执行 
@AfterThrowing:Throw后执行 
@After:无论方法以何种方式结束,都会执行(类似于finally) 
@Around:环绕执行

从上面的代码我们可以看出,如果有多个方法使用同一种'execution'定义,可以通过@Pointcut定义一个方法,然后在其他的方法使用该方法名即可完成定义,例如代码中的'myMethod'方法,我们在其他方法直接使用'myMethod()'即可完成定义,需要注意的是:在Spring 4.0版本以前,如果要生成一个没有实现接口的类的代理对象,需要导入cglib.jar这个包。

接下来,我们编写一个测试类,代码如下:

package com.castiel.service;

import org.junit.Test;
import org.springframework.beans.propertyeditors.ClassArrayEditor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.castiel.model.User;
import com.castiel.service.UserService;

public class UserServiceTest {
	/**测试类生成方式**/
	/**
	 * 1.选中src下com.castiel.service包中的UserService
	 * 2.右键new jUnit Test Case
	 * 3.点击NEXT
	 * 4.选择需要测试的方法
	 * 5.点击Finish
	 * 6.把生成出来的*Test.java文件拖到test下com.castiel.service包中
	 * @throws Exception 
	 */
	@Test
	public void testAdd() throws Exception {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		System.out.println("******************************");
		UserService userService = (UserService)ctx.getBean("userService");
		System.out.println(userService.getClass());
		userService.add(new User());
		/**
		 * 在web框架中,一般自动管理生命周期,自动调用destroy方法,这里我们模拟一下
		 * 注意:ApplicationContext中没有destroy方法,所以我们要调用ApplicationContext的实现类
		 * ClassPathXmlApplicationContext中的destroy方法
		 */
		/**
		 * 如果我们设置bean的scope为'prototype',是不会执行destroy方法,spring无法自动管理多例的整个生命周期
		 * 
		 * bean的构造函数执行完毕才会执行init方法
		 */
		ctx.destroy();
	}

}

运行测试代码,结果如下:


其中:'user saved!'这条输出是'UserService'的add()方法执行后的输出,这下我们就能明显看出这些注解的用法和执行的先后顺序了!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值