Returning one logger per class in Spring JavaConfig's logger @Resource injection support

This commit is contained in:
Andrew Phillips 2012-04-11 10:36:47 -04:00
parent 4e5fe4ece3
commit b161fcc48e
1 changed files with 18 additions and 5 deletions

View File

@ -19,6 +19,7 @@
package org.jclouds.demo.tweetstore.config; package org.jclouds.demo.tweetstore.config;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.String.format;
import static org.jclouds.logging.LoggingModules.firstOrJDKLoggingModule; import static org.jclouds.logging.LoggingModules.firstOrJDKLoggingModule;
import java.util.Set; import java.util.Set;
@ -66,17 +67,29 @@ abstract class LoggingConfig implements BeanFactoryAware {
public Object resolveDependency(DependencyDescriptor descriptor, public Object resolveDependency(DependencyDescriptor descriptor,
String beanName, Set<String> autowiredBeanNames, String beanName, Set<String> autowiredBeanNames,
TypeConverter typeConverter) throws BeansException { TypeConverter typeConverter) throws BeansException {
Object bean;
if (descriptor.getDependencyType().equals(Logger.class)) { if (descriptor.getDependencyType().equals(Logger.class)) {
Class<?> requestingType = getType(beanName); Class<?> requestingType = getType(beanName);
LOGGER.trace("About to create logger for bean '%s' of type '%s'", LOGGER.trace("About to resolve logger for bean '%s' of type '%s'",
beanName, requestingType); beanName, requestingType);
bean = LOGGER_FACTORY.getLogger(requestingType.getName()); Logger logger = resolveLogger(requestingType, autowiredBeanNames);
LOGGER.trace("Successfully created logger."); LOGGER.trace("Successfully resolved logger.");
return bean; return logger;
} }
return super.resolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter); return super.resolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter);
} }
private Logger resolveLogger(Class<?> type, Set<String> autowiredBeanNames) {
String loggerBeanName = format("%s#logger", type);
if (autowiredBeanNames.contains(loggerBeanName)) {
LOGGER.trace("Returning existing bean '%s'", loggerBeanName);
return (Logger) getBean(loggerBeanName);
}
LOGGER.trace("About to create logger for type '%s'", type);
Logger logger = LOGGER_FACTORY.getLogger(type.getName());
LOGGER.trace("Successfully created logger.");
return logger;
}
} }
@Override @Override