Skip to content

Commit 9b6ec5b

Browse files
jhoellerunknown
authored and
unknown
committed
DeprecatedBeanWarner detects deprecated FactoryBean classes and always logs user-specified bean type
1 parent 9deaefe commit 9b6ec5b

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/config/DeprecatedBeanWarner.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,8 @@
2020
import org.apache.commons.logging.LogFactory;
2121

2222
import org.springframework.beans.BeansException;
23+
import org.springframework.beans.factory.BeanFactory;
24+
import org.springframework.util.ClassUtils;
2325
import org.springframework.util.StringUtils;
2426

2527
/**
@@ -36,9 +38,11 @@ public class DeprecatedBeanWarner implements BeanFactoryPostProcessor {
3638
protected transient Log logger = LogFactory.getLog(getClass());
3739

3840
/**
39-
* Set the name of the logger to use. The name will be passed to the underlying logger implementation through
40-
* Commons Logging, getting interpreted as log category according to the logger's configuration.
41-
* <p>This can be specified to not log into the category of this warner class but rather into a specific named category.
41+
* Set the name of the logger to use.
42+
* The name will be passed to the underlying logger implementation through Commons Logging,
43+
* getting interpreted as log category according to the logger's configuration.
44+
* <p>This can be specified to not log into the category of this warner class but rather
45+
* into a specific named category.
4246
* @see org.apache.commons.logging.LogFactory#getLog(String)
4347
* @see org.apache.log4j.Logger#getLogger(String)
4448
* @see java.util.logging.Logger#getLogger(String)
@@ -52,24 +56,28 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
5256
if (isLogEnabled()) {
5357
String[] beanNames = beanFactory.getBeanDefinitionNames();
5458
for (String beanName : beanNames) {
55-
Class<?> beanType = beanFactory.getType(beanName);
59+
String nameToLookup = beanName;
60+
if (beanFactory.isFactoryBean(beanName)) {
61+
nameToLookup = BeanFactory.FACTORY_BEAN_PREFIX + beanName;
62+
}
63+
Class<?> beanType = ClassUtils.getUserClass(beanFactory.getType(nameToLookup));
5664
if (beanType != null && beanType.isAnnotationPresent(Deprecated.class)) {
5765
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
58-
logDeprecatedBean(beanName, beanDefinition);
66+
logDeprecatedBean(beanName, beanType, beanDefinition);
5967
}
6068
}
6169
}
6270
}
6371

6472
/**
6573
* Logs a warning for a bean annotated with {@link Deprecated @Deprecated}.
66-
*
6774
* @param beanName the name of the deprecated bean
75+
* @param beanType the user-specified type of the deprecated bean
6876
* @param beanDefinition the definition of the deprecated bean
6977
*/
70-
protected void logDeprecatedBean(String beanName, BeanDefinition beanDefinition) {
78+
protected void logDeprecatedBean(String beanName, Class<?> beanType, BeanDefinition beanDefinition) {
7179
StringBuilder builder = new StringBuilder();
72-
builder.append(beanDefinition.getBeanClassName());
80+
builder.append(beanType);
7381
builder.append(" ['");
7482
builder.append(beanName);
7583
builder.append('\'');
@@ -78,14 +86,23 @@ protected void logDeprecatedBean(String beanName, BeanDefinition beanDefinition)
7886
builder.append(" in ");
7987
builder.append(resourceDescription);
8088
}
81-
builder.append(" ] has been deprecated");
82-
logger.warn(builder.toString());
89+
builder.append("] has been deprecated");
90+
writeToLog(builder.toString());
91+
}
92+
93+
/**
94+
* Actually write to the underlying log.
95+
* <p>The default implementations logs the message at "warn" level.
96+
* @param message the message to write
97+
*/
98+
protected void writeToLog(String message) {
99+
logger.warn(message);
83100
}
84101

85102
/**
86103
* Determine whether the {@link #logger} field is enabled.
87-
* <p>Default is {@code true} when the "warn" level is enabled. Subclasses can override this to change the level
88-
* under which logging occurs.
104+
* <p>Default is {@code true} when the "warn" level is enabled.
105+
* Subclasses can override this to change the level under which logging occurs.
89106
*/
90107
protected boolean isLogEnabled() {
91108
return logger.isWarnEnabled();

spring-beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,12 @@
1616

1717
package org.springframework.beans.factory.config;
1818

19-
import static org.junit.Assert.*;
2019
import org.junit.Test;
2120

22-
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2321
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
22+
import org.springframework.beans.factory.support.RootBeanDefinition;
23+
24+
import static org.junit.Assert.*;
2425

2526
/**
2627
* @author Arjen Poutsma
@@ -35,12 +36,11 @@ public class DeprecatedBeanWarnerTests {
3536

3637
private DeprecatedBeanWarner warner;
3738

39+
3840
@Test
3941
public void postProcess() {
4042
beanFactory = new DefaultListableBeanFactory();
41-
BeanDefinition def = BeanDefinitionBuilder
42-
.genericBeanDefinition(MyDeprecatedBean.class)
43-
.getBeanDefinition();
43+
BeanDefinition def = new RootBeanDefinition(MyDeprecatedBean.class);
4444
String beanName = "deprecated";
4545
beanFactory.registerBeanDefinition(beanName, def);
4646

@@ -51,15 +51,14 @@ public void postProcess() {
5151

5252
}
5353

54-
private class MyDeprecatedBeanWarner extends DeprecatedBeanWarner {
5554

55+
private class MyDeprecatedBeanWarner extends DeprecatedBeanWarner {
5656

5757
@Override
58-
protected void logDeprecatedBean(String beanName, BeanDefinition beanDefinition) {
58+
protected void logDeprecatedBean(String beanName, Class<?> beanType, BeanDefinition beanDefinition) {
5959
DeprecatedBeanWarnerTests.this.beanName = beanName;
6060
DeprecatedBeanWarnerTests.this.beanDefinition = beanDefinition;
6161
}
6262
}
6363

64-
6564
}

0 commit comments

Comments
 (0)