From 68cdd5d3589a1ae3b423c6383433dce0589853f9 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 4 May 2016 09:13:09 +0200 Subject: [PATCH] LazySingletonAspectInstanceFactoryDecorator uses shared singleton mutex Issue: SPR-14241 --- .../BeanFactoryAspectInstanceFactory.java | 19 ++++++++++++------- ...ngletonAspectInstanceFactoryDecorator.java | 10 +++++++--- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectInstanceFactory.java index 4ac2b0282c..713fb44fce 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectInstanceFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.core.Ordered; import org.springframework.core.annotation.OrderUtils; +import org.springframework.util.Assert; import org.springframework.util.ClassUtils; /** @@ -66,6 +67,8 @@ public class BeanFactoryAspectInstanceFactory implements MetadataAwareAspectInst * @param type the type that should be introspected by AspectJ */ public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name, Class type) { + Assert.notNull(beanFactory, "BeanFactory must not be null"); + Assert.notNull(name, "Bean name must not be null"); this.beanFactory = beanFactory; this.name = name; this.aspectMetadata = new AspectMetadata(type, name); @@ -79,12 +82,9 @@ public class BeanFactoryAspectInstanceFactory implements MetadataAwareAspectInst @Override public ClassLoader getAspectClassLoader() { - if (this.beanFactory instanceof ConfigurableBeanFactory) { - return ((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader(); - } - else { - return ClassUtils.getDefaultClassLoader(); - } + return (this.beanFactory instanceof ConfigurableBeanFactory ? + ((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader() : + ClassUtils.getDefaultClassLoader()); } @Override @@ -92,6 +92,11 @@ public class BeanFactoryAspectInstanceFactory implements MetadataAwareAspectInst return this.aspectMetadata; } + public Object getAspectCreationMutex() { + return (this.beanFactory instanceof ConfigurableBeanFactory ? + ((ConfigurableBeanFactory) this.beanFactory).getSingletonMutex() : this); + } + /** * Determine the order for this factory's target aspect, either * an instance-specific order expressed through implementing the diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/LazySingletonAspectInstanceFactoryDecorator.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/LazySingletonAspectInstanceFactoryDecorator.java index 36bb4eeb42..78df54658c 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/LazySingletonAspectInstanceFactoryDecorator.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/LazySingletonAspectInstanceFactoryDecorator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,9 +43,13 @@ public class LazySingletonAspectInstanceFactoryDecorator implements MetadataAwar @Override - public synchronized Object getAspectInstance() { + public Object getAspectInstance() { if (this.materialized == null) { - synchronized (this) { + Object mutex = this; + if (this.maaif instanceof BeanFactoryAspectInstanceFactory) { + mutex = ((BeanFactoryAspectInstanceFactory) this.maaif).getAspectCreationMutex(); + } + synchronized (mutex) { if (this.materialized == null) { this.materialized = this.maaif.getAspectInstance(); }