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 e2cb10cd2d3..06ac361ad5e 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-2015 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. @@ -82,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 @@ -95,6 +92,12 @@ public class BeanFactoryAspectInstanceFactory implements MetadataAwareAspectInst return this.aspectMetadata; } + @Override + 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 3baa84d03b0..3e76fb47a1e 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-2015 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. @@ -46,9 +46,9 @@ public class LazySingletonAspectInstanceFactoryDecorator implements MetadataAwar @Override - public synchronized Object getAspectInstance() { + public Object getAspectInstance() { if (this.materialized == null) { - synchronized (this) { + synchronized (this.maaif.getAspectCreationMutex()) { if (this.materialized == null) { this.materialized = this.maaif.getAspectInstance(); } @@ -71,6 +71,11 @@ public class LazySingletonAspectInstanceFactoryDecorator implements MetadataAwar return this.maaif.getAspectMetadata(); } + @Override + public Object getAspectCreationMutex() { + return this.maaif.getAspectCreationMutex(); + } + @Override public int getOrder() { return this.maaif.getOrder(); diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/MetadataAwareAspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/MetadataAwareAspectInstanceFactory.java index cb9a77d4e94..a001efbc330 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/MetadataAwareAspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/MetadataAwareAspectInstanceFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 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. @@ -39,4 +39,11 @@ public interface MetadataAwareAspectInstanceFactory extends AspectInstanceFactor */ AspectMetadata getAspectMetadata(); + /** + * Return the best possible creation mutex for this factory. + * @return the mutex object (never {@code null}) + * @since 4.3 + */ + Object getAspectCreationMutex(); + } diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SimpleMetadataAwareAspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SimpleMetadataAwareAspectInstanceFactory.java index 5b8113e7239..2aa2431af00 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SimpleMetadataAwareAspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SimpleMetadataAwareAspectInstanceFactory.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. @@ -50,6 +50,11 @@ public class SimpleMetadataAwareAspectInstanceFactory extends SimpleAspectInstan return this.metadata; } + @Override + public Object getAspectCreationMutex() { + return this; + } + @Override protected int getOrderForAspectClass(Class aspectClass) { return OrderUtils.getOrder(aspectClass, Ordered.LOWEST_PRECEDENCE); diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SingletonMetadataAwareAspectInstanceFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SingletonMetadataAwareAspectInstanceFactory.java index b16e8d1576d..15edfbbd29a 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SingletonMetadataAwareAspectInstanceFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/SingletonMetadataAwareAspectInstanceFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 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. @@ -55,6 +55,11 @@ public class SingletonMetadataAwareAspectInstanceFactory extends SingletonAspect return this.metadata; } + @Override + public Object getAspectCreationMutex() { + return this; + } + @Override protected int getOrderForAspectClass(Class aspectClass) { return OrderUtils.getOrder(aspectClass, Ordered.LOWEST_PRECEDENCE); diff --git a/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactoryTests.java b/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactoryTests.java index 3f34ff592e3..554be1ae81e 100644 --- a/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactoryTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 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. @@ -693,6 +693,11 @@ public abstract class AbstractAspectJAdvisorFactoryTests { return new AspectMetadata(PerTypeWithinAspect.class, "perTypeWithin"); } + @Override + public Object getAspectCreationMutex() { + return this; + } + @Override public int getOrder() { return Ordered.LOWEST_PRECEDENCE;