Spring框架提供通过注解方式配置的生命是SOP功能,是基于AspectJ实现的。
AspectJ提供整套AOP理论的完整实现,很多其他的AOP实现(包括其他语言)都借鉴了AspectJ的一些思想,AspectJ的很多实现方式已经成为AOP领域的事实标准。即使不使用Spring框架,也可直接使用AspectJ进行AOP编程,AspectJ允许使用注解来定义切面,切入点和增强,而Spring框架可以识别并根据这些注解生成AOP代理。
准备Aspect注解:
为了使用AspectJ注解方式,需要配置Spring AOP,首先要在Spring配置文件中声明<aop:aspectj-autoproxy>元素。同<aop:config>元素类似,<aop:aspectj-autoproxy>元素也具有proxy-target-class和expose-proxy属性。
配置切面说明:
实施增强的TransactionManager类需要配置为切面,AspectJ提供了下列注解用于配置切面,这些注解都位于org.aspectj.lang.annotation包下面。
●@Aspect:配置一个类为切面类
●@Before:配置一个方法为前置增强
●@After:配置一个方法为后置增强
●@AfterReturning:配置一个方法为返回后增强
●@AfterThrowing:配置一个方法为抛出异常后增强
●@Around:配置一个方法为环绕增强
●@DeclareParents:配置一个类为引介增强
下面通过例子进行演示:
(1)首先编写业务类,业务类GoodService,没有实现任何接口,代码如下:
<span style="font-size:18px;"><strong>package com.loster.li.spring_aop;
public class GoodsService {
private static int ORDER_NO = 1000; //用于生成订单编号
public int buyGoods(String user,String productName){
ORDER_NO++;
System.out.println("【数据库插入订单】购买者"+user+",商品:"+productName
+",订单编号:"+ORDER_NO);
return ORDER_NO;
}
public void returnGoods(int orderNo){
System.out.println("【数据库修改订单状态】订单编号:"+orderNo);
}
}</strong></span>
(2)编写切面类,通过注解使事务管理类TransactionManager用于对业务类进行增强:
<span style="font-size:18px;"><strong>package com.loster.li.spring_aop;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class TransactionManager_AspectJ {
@Before("execution(* com.loster.li.spring_aop.*Service.*(..))")
public void beginTransaction(){
System.out.println("【事务管理器】开始事务");
}
@AfterReturning("execution(* com.loster.li.spring_aop.*Service.*(..))")
public void commit(){
System.out.println("【事务管理器】提交事务");
}
@AfterThrowing("execution(* com.loster.li.spring_aop.*Service.*(..))")
public void rollback(){
System.out.println("【事务管理器】回滚事务");
}
}</strong></span>
(3)在Spring配置文件中配置AOP,命名为applicationContext_AspectJ.xml,其代码如下:
<span style="font-size:18px;"><strong><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 业务类 -->
<bean id = "service" class = "com.loster.li.spring_aop.GoodsService"></bean>
<!-- 切面类 -->
<bean id = "transactionManager" class = "com.loster.li.spring_aop.TransactionManager_AspectJ"></bean>
<!-- 开启AspectJ注释 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans></strong></span>
(4)编写测试类test2.java:
<span style="font-size:18px;"><strong>package com.loster.li.spring_aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test2 {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext(
"/com/loster/li/spring_aop/applicationContext_AspectJ.xml");
GoodsService service = (GoodsService)context.getBean("service");
service.buyGoods("June", "Python"); //购买商品
service.returnGoods(3100); //退货
}
}</strong></span>
(5)运行test2.java,结果如下: