diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/DeprecatedBeanWarner.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/DeprecatedBeanWarner.java index ecdef33347..815173b6be 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/DeprecatedBeanWarner.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/DeprecatedBeanWarner.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2012 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. @@ -20,6 +20,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; /** @@ -36,9 +38,11 @@ public class DeprecatedBeanWarner implements BeanFactoryPostProcessor { protected transient Log logger = LogFactory.getLog(getClass()); /** - * Set the name of the logger to use. The name will be passed to the underlying logger implementation through - * Commons Logging, getting interpreted as log category according to the logger's configuration. - *
This can be specified to not log into the category of this warner class but rather into a specific named category. + * Set the name of the logger to use. + * The name will be passed to the underlying logger implementation through Commons Logging, + * getting interpreted as log category according to the logger's configuration. + *
This can be specified to not log into the category of this warner class but rather + * into a specific named category. * @see org.apache.commons.logging.LogFactory#getLog(String) * @see org.apache.log4j.Logger#getLogger(String) * @see java.util.logging.Logger#getLogger(String) @@ -52,10 +56,14 @@ public class DeprecatedBeanWarner implements BeanFactoryPostProcessor { if (isLogEnabled()) { String[] beanNames = beanFactory.getBeanDefinitionNames(); for (String beanName : beanNames) { - Class> beanType = beanFactory.getType(beanName); + String nameToLookup = beanName; + if (beanFactory.isFactoryBean(beanName)) { + nameToLookup = BeanFactory.FACTORY_BEAN_PREFIX + beanName; + } + Class> beanType = ClassUtils.getUserClass(beanFactory.getType(nameToLookup)); if (beanType != null && beanType.isAnnotationPresent(Deprecated.class)) { BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); - logDeprecatedBean(beanName, beanDefinition); + logDeprecatedBean(beanName, beanType, beanDefinition); } } } @@ -63,13 +71,13 @@ public class DeprecatedBeanWarner implements BeanFactoryPostProcessor { /** * Logs a warning for a bean annotated with {@link Deprecated @Deprecated}. - * * @param beanName the name of the deprecated bean + * @param beanType the user-specified type of the deprecated bean * @param beanDefinition the definition of the deprecated bean */ - protected void logDeprecatedBean(String beanName, BeanDefinition beanDefinition) { + protected void logDeprecatedBean(String beanName, Class> beanType, BeanDefinition beanDefinition) { StringBuilder builder = new StringBuilder(); - builder.append(beanDefinition.getBeanClassName()); + builder.append(beanType); builder.append(" ['"); builder.append(beanName); builder.append('\''); @@ -78,14 +86,23 @@ public class DeprecatedBeanWarner implements BeanFactoryPostProcessor { builder.append(" in "); builder.append(resourceDescription); } - builder.append(" ] has been deprecated"); - logger.warn(builder.toString()); + builder.append("] has been deprecated"); + writeToLog(builder.toString()); + } + + /** + * Actually write to the underlying log. + *
The default implementations logs the message at "warn" level. + * @param message the message to write + */ + protected void writeToLog(String message) { + logger.warn(message); } /** * Determine whether the {@link #logger} field is enabled. - *
Default is {@code true} when the "warn" level is enabled. Subclasses can override this to change the level - * under which logging occurs. + *
Default is {@code true} when the "warn" level is enabled. + * Subclasses can override this to change the level under which logging occurs. */ protected boolean isLogEnabled() { return logger.isWarnEnabled(); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java index e7c04d9ecd..6ae7cf7400 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2012 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. @@ -16,11 +16,12 @@ package org.springframework.beans.factory.config; -import static org.junit.Assert.*; import org.junit.Test; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.support.RootBeanDefinition; + +import static org.junit.Assert.*; /** * @author Arjen Poutsma @@ -35,12 +36,11 @@ public class DeprecatedBeanWarnerTests { private DeprecatedBeanWarner warner; + @Test public void postProcess() { beanFactory = new DefaultListableBeanFactory(); - BeanDefinition def = BeanDefinitionBuilder - .genericBeanDefinition(MyDeprecatedBean.class) - .getBeanDefinition(); + BeanDefinition def = new RootBeanDefinition(MyDeprecatedBean.class); String beanName = "deprecated"; beanFactory.registerBeanDefinition(beanName, def); @@ -51,15 +51,14 @@ public class DeprecatedBeanWarnerTests { } + private class MyDeprecatedBeanWarner extends DeprecatedBeanWarner { - @Override - protected void logDeprecatedBean(String beanName, BeanDefinition beanDefinition) { + protected void logDeprecatedBean(String beanName, Class> beanType, BeanDefinition beanDefinition) { DeprecatedBeanWarnerTests.this.beanName = beanName; DeprecatedBeanWarnerTests.this.beanDefinition = beanDefinition; } } - }