Skip to content

Commit 8a56db6

Browse files
committed
SimpleAliasRegistry logs info message for alias overriding
Issue: SPR-16871 (cherry picked from commit 74fcdea)
1 parent 50d6d90 commit 8a56db6

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import java.util.concurrent.ConcurrentMap;
4040
import java.util.function.Supplier;
4141

42+
import org.apache.commons.logging.Log;
43+
4244
import org.springframework.beans.BeanUtils;
4345
import org.springframework.beans.BeanWrapper;
4446
import org.springframework.beans.BeanWrapperImpl;
@@ -1870,6 +1872,14 @@ protected void clearSingletonCache() {
18701872
}
18711873
}
18721874

1875+
/**
1876+
* Expose the logger to collaborating delegates.
1877+
* @since 5.0.7
1878+
*/
1879+
Log getLogger() {
1880+
return logger;
1881+
}
1882+
18731883

18741884
/**
18751885
* Special DependencyDescriptor variant for Spring's good old autowire="byType" mode.

spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.util.Map;
3333
import java.util.Set;
3434

35+
import org.apache.commons.logging.Log;
36+
3537
import org.springframework.beans.BeanMetadataElement;
3638
import org.springframework.beans.BeanWrapper;
3739
import org.springframework.beans.BeanWrapperImpl;
@@ -78,13 +80,16 @@ class ConstructorResolver {
7880

7981
private final AbstractAutowireCapableBeanFactory beanFactory;
8082

83+
private final Log logger;
84+
8185

8286
/**
8387
* Create a new ConstructorResolver for the given factory and instantiation strategy.
8488
* @param beanFactory the BeanFactory to work with
8589
*/
8690
public ConstructorResolver(AbstractAutowireCapableBeanFactory beanFactory) {
8791
this.beanFactory = beanFactory;
92+
this.logger = beanFactory.getLogger();
8893
}
8994

9095

@@ -193,9 +198,8 @@ public BeanWrapper autowireConstructor(final String beanName, final RootBeanDefi
193198
getUserDeclaredConstructor(candidate), autowiring);
194199
}
195200
catch (UnsatisfiedDependencyException ex) {
196-
if (this.beanFactory.logger.isTraceEnabled()) {
197-
this.beanFactory.logger.trace(
198-
"Ignoring constructor [" + candidate + "] of bean '" + beanName + "': " + ex);
201+
if (logger.isTraceEnabled()) {
202+
logger.trace("Ignoring constructor [" + candidate + "] of bean '" + beanName + "': " + ex);
199203
}
200204
// Swallow and try next constructor.
201205
if (causes == null) {
@@ -471,9 +475,8 @@ public BeanWrapper instantiateUsingFactoryMethod(
471475
beanName, mbd, resolvedValues, bw, paramTypes, paramNames, candidate, autowiring);
472476
}
473477
catch (UnsatisfiedDependencyException ex) {
474-
if (this.beanFactory.logger.isTraceEnabled()) {
475-
this.beanFactory.logger.trace("Ignoring factory method [" + candidate +
476-
"] of bean '" + beanName + "': " + ex);
478+
if (logger.isTraceEnabled()) {
479+
logger.trace("Ignoring factory method [" + candidate + "] of bean '" + beanName + "': " + ex);
477480
}
478481
// Swallow and try next overloaded factory method.
479482
if (causes == null) {
@@ -733,8 +736,8 @@ private ArgumentsHolder createArgumentArray(
733736

734737
for (String autowiredBeanName : autowiredBeanNames) {
735738
this.beanFactory.registerDependentBean(autowiredBeanName, beanName);
736-
if (this.beanFactory.logger.isDebugEnabled()) {
737-
this.beanFactory.logger.debug("Autowiring by type from bean name '" + beanName +
739+
if (logger.isDebugEnabled()) {
740+
logger.debug("Autowiring by type from bean name '" + beanName +
738741
"' via " + (executable instanceof Constructor ? "constructor" : "factory method") +
739742
" to bean named '" + autowiredBeanName + "'");
740743
}

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
import java.util.Set;
2727
import java.util.concurrent.ConcurrentHashMap;
2828

29-
import org.apache.commons.logging.Log;
30-
import org.apache.commons.logging.LogFactory;
31-
3229
import org.springframework.beans.factory.BeanCreationException;
3330
import org.springframework.beans.factory.BeanCreationNotAllowedException;
3431
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
@@ -73,9 +70,6 @@
7370
*/
7471
public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {
7572

76-
/** Logger available to subclasses */
77-
protected final Log logger = LogFactory.getLog(getClass());
78-
7973
/** Cache of singleton objects: bean name --> bean instance */
8074
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
8175

spring-core/src/main/java/org/springframework/core/SimpleAliasRegistry.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import java.util.Map;
2323
import java.util.concurrent.ConcurrentHashMap;
2424

25+
import org.apache.commons.logging.Log;
26+
import org.apache.commons.logging.LogFactory;
27+
2528
import org.springframework.util.Assert;
2629
import org.springframework.util.StringUtils;
2730
import org.springframework.util.StringValueResolver;
@@ -37,6 +40,9 @@
3740
*/
3841
public class SimpleAliasRegistry implements AliasRegistry {
3942

43+
/** Logger available to subclasses */
44+
protected final Log logger = LogFactory.getLog(getClass());
45+
4046
/** Map from alias to canonical name */
4147
private final Map<String, String> aliasMap = new ConcurrentHashMap<>(16);
4248

@@ -48,6 +54,9 @@ public void registerAlias(String name, String alias) {
4854
synchronized (this.aliasMap) {
4955
if (alias.equals(name)) {
5056
this.aliasMap.remove(alias);
57+
if (logger.isDebugEnabled()) {
58+
logger.debug("Alias definition '" + alias + "' ignored since it points to same name");
59+
}
5160
}
5261
else {
5362
String registeredName = this.aliasMap.get(alias);
@@ -57,12 +66,19 @@ public void registerAlias(String name, String alias) {
5766
return;
5867
}
5968
if (!allowAliasOverriding()) {
60-
throw new IllegalStateException("Cannot register alias '" + alias + "' for name '" +
69+
throw new IllegalStateException("Cannot define alias '" + alias + "' for name '" +
6170
name + "': It is already registered for name '" + registeredName + "'.");
6271
}
72+
if (this.logger.isInfoEnabled()) {
73+
logger.info("Overriding alias '" + alias + "' definition for registered name '" +
74+
registeredName + "' with new target name '" + name + "'");
75+
}
6376
}
6477
checkForAliasCircle(name, alias);
6578
this.aliasMap.put(alias, name);
79+
if (logger.isDebugEnabled()) {
80+
logger.debug("Alias definition '" + alias + "' registered for name '" + name + "'");
81+
}
6682
}
6783
}
6884
}

0 commit comments

Comments
 (0)