Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3343 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
3 changed files with 181 additions and 0 deletions
@ -0,0 +1,94 @@
@@ -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. |
||||
* <p>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. |
||||
* <p>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(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,65 @@
@@ -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; |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
||||
@ -0,0 +1,22 @@
@@ -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 { |
||||
|
||||
} |
||||
Loading…
Reference in new issue