LazySingletonAspectInstanceFactoryDecorator uses shared singleton mutex

Issue: SPR-14241
This commit is contained in:
Juergen Hoeller 2016-05-04 09:03:28 +02:00
parent e26478e3de
commit e6e3ca3e96
6 changed files with 44 additions and 14 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -82,12 +82,9 @@ public class BeanFactoryAspectInstanceFactory implements MetadataAwareAspectInst
@Override @Override
public ClassLoader getAspectClassLoader() { public ClassLoader getAspectClassLoader() {
if (this.beanFactory instanceof ConfigurableBeanFactory) { return (this.beanFactory instanceof ConfigurableBeanFactory ?
return ((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader(); ((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader() :
} ClassUtils.getDefaultClassLoader());
else {
return ClassUtils.getDefaultClassLoader();
}
} }
@Override @Override
@ -95,6 +92,12 @@ public class BeanFactoryAspectInstanceFactory implements MetadataAwareAspectInst
return this.aspectMetadata; 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 * Determine the order for this factory's target aspect, either
* an instance-specific order expressed through implementing the * an instance-specific order expressed through implementing the

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -46,9 +46,9 @@ public class LazySingletonAspectInstanceFactoryDecorator implements MetadataAwar
@Override @Override
public synchronized Object getAspectInstance() { public Object getAspectInstance() {
if (this.materialized == null) { if (this.materialized == null) {
synchronized (this) { synchronized (this.maaif.getAspectCreationMutex()) {
if (this.materialized == null) { if (this.materialized == null) {
this.materialized = this.maaif.getAspectInstance(); this.materialized = this.maaif.getAspectInstance();
} }
@ -71,6 +71,11 @@ public class LazySingletonAspectInstanceFactoryDecorator implements MetadataAwar
return this.maaif.getAspectMetadata(); return this.maaif.getAspectMetadata();
} }
@Override
public Object getAspectCreationMutex() {
return this.maaif.getAspectCreationMutex();
}
@Override @Override
public int getOrder() { public int getOrder() {
return this.maaif.getOrder(); return this.maaif.getOrder();

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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(); AspectMetadata getAspectMetadata();
/**
* Return the best possible creation mutex for this factory.
* @return the mutex object (never {@code null})
* @since 4.3
*/
Object getAspectCreationMutex();
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; return this.metadata;
} }
@Override
public Object getAspectCreationMutex() {
return this;
}
@Override @Override
protected int getOrderForAspectClass(Class<?> aspectClass) { protected int getOrderForAspectClass(Class<?> aspectClass) {
return OrderUtils.getOrder(aspectClass, Ordered.LOWEST_PRECEDENCE); return OrderUtils.getOrder(aspectClass, Ordered.LOWEST_PRECEDENCE);

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; return this.metadata;
} }
@Override
public Object getAspectCreationMutex() {
return this;
}
@Override @Override
protected int getOrderForAspectClass(Class<?> aspectClass) { protected int getOrderForAspectClass(Class<?> aspectClass) {
return OrderUtils.getOrder(aspectClass, Ordered.LOWEST_PRECEDENCE); return OrderUtils.getOrder(aspectClass, Ordered.LOWEST_PRECEDENCE);

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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"); return new AspectMetadata(PerTypeWithinAspect.class, "perTypeWithin");
} }
@Override
public Object getAspectCreationMutex() {
return this;
}
@Override @Override
public int getOrder() { public int getOrder() {
return Ordered.LOWEST_PRECEDENCE; return Ordered.LOWEST_PRECEDENCE;