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 02e468b58820..49e31990f643 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-2013 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. @@ -18,9 +18,11 @@ import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.beans.factory.support.AbstractBeanFactory; import org.springframework.core.Ordered; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.Order; +import org.springframework.util.Assert; import org.springframework.util.ClassUtils; /** @@ -66,7 +68,9 @@ public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name) { * @param name the name of the bean * @param type the type that should be introspected by AspectJ */ - public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name, Class type) { + 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); @@ -78,18 +82,20 @@ public Object getAspectInstance() { } 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()); } public AspectMetadata getAspectMetadata() { return this.aspectMetadata; } + public Object getAspectCreationMutex() { + return (this.beanFactory instanceof AbstractBeanFactory ? + ((AbstractBeanFactory) 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 39d28fff7ed2..80788007c5c7 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-2009 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. @@ -42,9 +42,13 @@ public LazySingletonAspectInstanceFactoryDecorator(MetadataAwareAspectInstanceFa } - 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(); }