diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/DeprecatedBeanWarner.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/DeprecatedBeanWarner.java new file mode 100644 index 00000000000..ecdef333478 --- /dev/null +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/DeprecatedBeanWarner.java @@ -0,0 +1,94 @@ +/* + * Copyright 2002-2010 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.beans.factory.config; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.springframework.beans.BeansException; +import org.springframework.util.StringUtils; + +/** + * Bean factory post processor that logs a warning for {@link Deprecated @Deprecated} beans. + * + * @author Arjen Poutsma + * @since 3.0.3 + */ +public class DeprecatedBeanWarner implements BeanFactoryPostProcessor { + + /** + * Logger available to subclasses. + */ + 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. + * @see org.apache.commons.logging.LogFactory#getLog(String) + * @see org.apache.log4j.Logger#getLogger(String) + * @see java.util.logging.Logger#getLogger(String) + */ + public void setLoggerName(String loggerName) { + this.logger = LogFactory.getLog(loggerName); + } + + + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + if (isLogEnabled()) { + String[] beanNames = beanFactory.getBeanDefinitionNames(); + for (String beanName : beanNames) { + Class beanType = beanFactory.getType(beanName); + if (beanType != null && beanType.isAnnotationPresent(Deprecated.class)) { + BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); + logDeprecatedBean(beanName, beanDefinition); + } + } + } + } + + /** + * Logs a warning for a bean annotated with {@link Deprecated @Deprecated}. + * + * @param beanName the name of the deprecated bean + * @param beanDefinition the definition of the deprecated bean + */ + protected void logDeprecatedBean(String beanName, BeanDefinition beanDefinition) { + StringBuilder builder = new StringBuilder(); + builder.append(beanDefinition.getBeanClassName()); + builder.append(" ['"); + builder.append(beanName); + builder.append('\''); + String resourceDescription = beanDefinition.getResourceDescription(); + if (StringUtils.hasLength(resourceDescription)) { + builder.append(" in "); + builder.append(resourceDescription); + } + builder.append(" ] has been deprecated"); + logger.warn(builder.toString()); + } + + /** + * 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. + */ + protected boolean isLogEnabled() { + return logger.isWarnEnabled(); + } + +} diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java b/org.springframework.beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java new file mode 100644 index 00000000000..e7c04d9ecde --- /dev/null +++ b/org.springframework.beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2002-2010 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +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; + +/** + * @author Arjen Poutsma + */ +public class DeprecatedBeanWarnerTests { + + private DefaultListableBeanFactory beanFactory; + + private String beanName; + + private BeanDefinition beanDefinition; + + private DeprecatedBeanWarner warner; + + @Test + public void postProcess() { + beanFactory = new DefaultListableBeanFactory(); + BeanDefinition def = BeanDefinitionBuilder + .genericBeanDefinition(MyDeprecatedBean.class) + .getBeanDefinition(); + String beanName = "deprecated"; + beanFactory.registerBeanDefinition(beanName, def); + + warner = new MyDeprecatedBeanWarner(); + warner.postProcessBeanFactory(beanFactory); + assertEquals(beanName, this.beanName); + assertEquals(def, this.beanDefinition); + + } + + private class MyDeprecatedBeanWarner extends DeprecatedBeanWarner { + + + @Override + protected void logDeprecatedBean(String beanName, BeanDefinition beanDefinition) { + DeprecatedBeanWarnerTests.this.beanName = beanName; + DeprecatedBeanWarnerTests.this.beanDefinition = beanDefinition; + } + } + + +} diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/factory/config/MyDeprecatedBean.java b/org.springframework.beans/src/test/java/org/springframework/beans/factory/config/MyDeprecatedBean.java new file mode 100644 index 00000000000..ef3071088ba --- /dev/null +++ b/org.springframework.beans/src/test/java/org/springframework/beans/factory/config/MyDeprecatedBean.java @@ -0,0 +1,22 @@ +/* + * Copyright 2002-2010 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.beans.factory.config; + +@Deprecated +public class MyDeprecatedBean { + +}