Spring AOP及代理类执行顺序

关于 Spring AOP和Aspectj的关系,两个都实现了切面编程,Spring AOP更多地是为了Spring框架本身服务的,而Aspectj具有更强大、更完善的切面功能,我们在写业务时一般使用AspectJ。不过他们的概念、原理都差不多。
Spring AOP说:

the Spring Framework’s AOP functionality is normally used in conjunction with the Spring IoC container. Aspects are configured using normal bean definition syntax (although this allows powerful “autoproxying” capabilities): this is a crucial difference from other AOP implementations. There are some things you cannot do easily or efficiently with Spring AOP, such as advise very fine-grained objects (such as domain objects typically): AspectJ is the best choice in such cases. However, our experience is that Spring AOP provides an excellent solution to most problems in enterprise Java applications that are amenable to AOP

Spring AOP currently supports only method execution join points (advising the execution of methods on Spring beans). Field interception is not implemented, although support for field interception could be added without breaking the core Spring AOP APIs. If you need to advise field access and update join points, consider a language such as AspectJ.

Spring AOP will never strive to compete with AspectJ to provide a comprehensive AOP solution

可参考 Spring AOP vs AspectJ

AspectJ项目中提供了@AspecJ注解,Spring interprets the same annotations @Aspect as AspectJ 5

Spring AOP提供了定义切面的两种方式

  • schema-based approach : XML-based format 通常不用了
  • @AspectJ annotation style:注解形式,起这个名字是因为借鉴了AspectJ项目,其实用的Spring AOP的注解是@Aspect没有J

注解案例:

@Aspect
@Component 
public class NotVeryUsefulAspect {
	@Pointcut("execution(* transfer(..))")// the pointcut expression
	private void anyOldTransfer() {}// the pointcut signature

	@Before("execution(* com.xyz.myapp.dao.*.*(..))")
    public void doAccessCheck() {
        // ...
    }
	
 	@AfterReturning(
        pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",
        returning="retVal")
    public void doAccessCheck(Object retVal) {
        // ...
    }


	@AfterThrowing(
        pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",
        throwing="ex")
    public void doRecoveryActions(DataAccessException ex) {
        // ...
    }

	@After("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")
    public void doReleaseLock() {
        // ...
    }
	
	@Around("com.xyz.myapp.SystemArchitecture.businessService()")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
        // start stopwatch
        Object retVal = pjp.proceed();
        // stop stopwatch
        return retVal;
    }
}

AOP概念

  • Aspect:cut cross multiple classes. Spring AOP通过 schema-based approach@AspectJ annotation style 两种方式定义切面。
  • Join Point:程序执行点,通常代表一个方法的执行。
  • Advice:Aspect中针对不同Join Point执行的操作,包括 around before after 三种advice。很多AOP框架将advice建模成interceptor,在Join Point外层维护一个 chain of interceptors。
  • Pointcut:匹配 Join Points的规则,如@PointCut("pointcut expression"),满足point expression的将进行切面。
  • Target object:被切面的对象。
  • AOP proxy:代理,Spring Framework通常使用JDK 动态代理或CGLIB动态代理。
  • Weaving:linking aspects with other application types or objects to create an advised object. 发生在编译、加载或运行时。

Type of advice

  • before : @Before
  • after returning : @AfterReturning
  • after throwing : @AfterThrowing
  • after (finally) : @After
  • around : @Around

在这里插入图片描述
在这里插入图片描述

代理顺序

当一个target object有多个Spring AOP代理时,代理类执行的顺序有时很重要,比如分布式锁代理和事务代理的顺序,分布式锁必须包裹住事务。

先下结论:
Spring通过接口或注解来判断代理的顺序,顺序级别越低,代理越靠内层。顺序级别获取步骤如下:

  1. 是否实现org.springframework.core.Ordered接口
  2. 是否注解了org.springframework.core.annotation.Order注解
  3. 是否注解了javax.annotation.Priority注解
  4. 如果都没有,默认最低优先级LOWEST_PRECEDENCE = Integer.MAX_VALUE,如@Transanctional注解就是最低级别

优先级定义范围在Integer.MIN_VALUEInteger.MAX_VALUE(参见Orderd接口)。越小,优先级越高,越在外层先执行。

注解方式生成代理类是通过BeanPostProcessor实现的,由AspectJAwareAdvisorAutoProxyCreator完成,该类实现了BeanPostProcessor
注意到该类有一个Comparator类字段。最终优先级由AnnotationAwareOrderComparator来判断:

public class AnnotationAwareOrderComparator extends OrderComparator {

	/**
	 * Shared default instance of {@code AnnotationAwareOrderComparator}.
	 */
	public static final AnnotationAwareOrderComparator INSTANCE = new AnnotationAwareOrderComparator();


		@Override
	@Nullable
	protected Integer findOrder(Object obj) {
		// 父类,判断是否实现了Orderd接口
		// (obj instanceof Ordered ? ((Ordered) obj).getOrder() : null)
		Integer order = super.findOrder(obj);
		if (order != null) {
			return order;
		}
		// 没有实现则继续搜寻@Order和@Priority注解
		// 如果返回null,表示找不到,会返回父类执行getOrder方法返回Ordered.LOWEST_PRECEDENCE
		return findOrderFromAnnotation(obj);
	}
	
	@Nullable
	private Integer findOrderFromAnnotation(Object obj) {
		...
		// 找@Order和@Priority注解
		Integer order = OrderUtils.getOrderFromAnnotations(element, annotations);
		// 如果该类没有注解,递归找被代理类
		if (order == null && obj instanceof DecoratingProxy) {
			return findOrderFromAnnotation(((DecoratingProxy) obj).getDecoratedClass());
		}
		// 被代理类有则返回优先级,没有则返回null
		return order;
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring IOC 控制反转:把创建对象的权利交给Spring 创建对象 1.无参构造<bean class=""> 2.静态工厂<bean class="" factory-method=""> 3.实例工厂 <bean bean-factory="" factory-method=""> 管理对象 对象关系DI 构造器注入<construct-arg> set注入<property> 生命周期 scope:prototype/singleton init-method destroy-method API BeanFactory:使用这个工厂创建对象的方式都是懒加载,在调用的时候再创建 ClassPathXmlApplicationContext:使用这个工厂创建对象,他会根据scope智能判断是否懒加载,如果是单例则创建容器时就会创建里面bean的实例,如果是多例在获取使用时才会创建bean实例 FileSystemXmlApplicationContext磁盘路径 AnnotationConfigApplicationContext注解 WebApplicationContext:web环境使用的容器 注解 创建对象 Component:不分层的注解 Controller:web层 Service:service层 Repository:dao层 管理对象 注入 AutoWired Qualifier Resource Value 声明周期 Scope PostConstruct PreDestroy 新注解 Bean:写方法上,将方法的返回值 Configuration:标记配置 ComponentScan包扫描 PropertySource:加载配置文件 Import:导入其他配置 AOP 概念:面向切面编程,在不改变源码的情况下对方法进行增强,抽取横切关注点(日志处理,事务管理,安全检查,性能测试等等),使用AOP进行增强,使程序员只需要关注与业务逻辑编写. 专业术语 目标Target:需要增强的 连接点JoinPoint:目标中可被增强的方法 切入点PointCut:被增强的方法 增强Advice:增强代码 切面Aspect:切点加通知 织入weaving:讲切面加载进内存形成代理对象的过程 代理Proxy 底层实现 JDK动态代理(默认) 基于接口:代理对象与目标对象是兄弟关系,目标必须实现接口 CGLIB动态代理 基于父:代理对象与目标对象是父子关系.目标不能被final修饰 修改默认代理方法:<aop:aspectj-autoproxy proxy-target-class="true"/> 增强种 前置通知 后置通知 异常通知 最终通知 环绕通知 注意:使用注解的方式,最终通知和后置通知顺序换了,建议使用环绕通知 注解 配置 声明式事务管理 PlatFormTransactionManager:平台事务管理器:定义了commit/rollback Mybatis/jdbc:DataSourceTransactionManager Hibernater:HibernaterTransactionManager TransactionManagerDifinition 传播行为:A-->B,在B上声明是否一定需要事务管理 requerd:必须的(默认),如果A有事务那么就加入A的事务,如果A没有事务那么单独创建一个事务 supports,如果A有事务则加入,如果没有就算了 隔离级别 default:使用数据库默认的隔离级别(mysql:可重复读,oracle:读已提交) readuncommited:读未提交,不可以解决任何问题 readcommited:读已提交,可以解决脏读问题 repeatableRead:可重复读,可以解决脏读,不可重复读问题 Serializbler:串行化,可以解决所有问题 超时时间: 默认-1(永不超时),事务一直不提交也不回滚的时间 是否只读: 默认false TransactionManagerStatus: 事务的一些状态 整合 Spring整合Junit 1.导入依赖spring-test 2.加注解:RunWith、ContextConfiguration 3.注入对象进行测试 Spring整合web 1.导入依赖spring-web 2.配置ContextLoadListener 3.配置 <!--全局初始化参数--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> 4.在Servlet中使用WebApplicationContextUtils获取容器对象 5.使用容器对象去获取Service对象

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

T.Y.Bao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值