1
1
/*
2
- * Copyright 2002-2010 the original author or authors.
2
+ * Copyright 2002-2012 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
import org .apache .commons .logging .LogFactory ;
21
21
22
22
import org .springframework .beans .BeansException ;
23
+ import org .springframework .beans .factory .BeanFactory ;
24
+ import org .springframework .util .ClassUtils ;
23
25
import org .springframework .util .StringUtils ;
24
26
25
27
/**
@@ -36,9 +38,11 @@ public class DeprecatedBeanWarner implements BeanFactoryPostProcessor {
36
38
protected transient Log logger = LogFactory .getLog (getClass ());
37
39
38
40
/**
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.
42
46
* @see org.apache.commons.logging.LogFactory#getLog(String)
43
47
* @see org.apache.log4j.Logger#getLogger(String)
44
48
* @see java.util.logging.Logger#getLogger(String)
@@ -52,24 +56,28 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
52
56
if (isLogEnabled ()) {
53
57
String [] beanNames = beanFactory .getBeanDefinitionNames ();
54
58
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 ));
56
64
if (beanType != null && beanType .isAnnotationPresent (Deprecated .class )) {
57
65
BeanDefinition beanDefinition = beanFactory .getBeanDefinition (beanName );
58
- logDeprecatedBean (beanName , beanDefinition );
66
+ logDeprecatedBean (beanName , beanType , beanDefinition );
59
67
}
60
68
}
61
69
}
62
70
}
63
71
64
72
/**
65
73
* Logs a warning for a bean annotated with {@link Deprecated @Deprecated}.
66
- *
67
74
* @param beanName the name of the deprecated bean
75
+ * @param beanType the user-specified type of the deprecated bean
68
76
* @param beanDefinition the definition of the deprecated bean
69
77
*/
70
- protected void logDeprecatedBean (String beanName , BeanDefinition beanDefinition ) {
78
+ protected void logDeprecatedBean (String beanName , Class <?> beanType , BeanDefinition beanDefinition ) {
71
79
StringBuilder builder = new StringBuilder ();
72
- builder .append (beanDefinition . getBeanClassName () );
80
+ builder .append (beanType );
73
81
builder .append (" ['" );
74
82
builder .append (beanName );
75
83
builder .append ('\'' );
@@ -78,14 +86,23 @@ protected void logDeprecatedBean(String beanName, BeanDefinition beanDefinition)
78
86
builder .append (" in " );
79
87
builder .append (resourceDescription );
80
88
}
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 );
83
100
}
84
101
85
102
/**
86
103
* 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.
89
106
*/
90
107
protected boolean isLogEnabled () {
91
108
return logger .isWarnEnabled ();
0 commit comments