Introduce "Aware" superinterface

All existing *Aware interfaces have been refactored to extend this
new marker interface, serving two purposes:

    * Easy access to a type hierarchy that can answer the question
      "What *Aware interfaces are available?", without requiring
      text-based searches. Also clearly excludes false positives like
      TargetClassAware and ParamAware, which while similarly named,
      are not semantically similar to traditional *Aware interfaces
      in Spring.

    * Minor potential performance improvements in
      AbstractAutowireCapableBeanFactory and
      ApplicationContextAwareProcessor. Both have blocks of sequential
      instanceof checks in order to invoke any *Aware interface callback
      methods. For a bean that implements none of these interfaces,
      the whole sequence can be avoided by guarding first with
          if (bean instanceof Aware) {
              ...
          }

Implementors of custom *Aware-style interfaces (and presumably
the BeanPostProcessors that handle them), are encouraged to refactor to
extending this interface for consistency with the framework as well as
the points above.
This commit is contained in:
Chris Beams 2011-01-03 10:13:57 +00:00
parent b3ff9be78f
commit 5e6912302a
21 changed files with 144 additions and 62 deletions

View File

@ -0,0 +1,39 @@
/*
* Copyright 2002-2011 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;
/**
* Marker superinterface indicating that a bean is eligible to be
* notified by the Spring container of a particular framework object
* through a callback-style method. Actual method signature is
* determined by individual subinterfaces, but should typically
* consist of just one void-returning method that accepts a single
* argument.
*
* <p>Note that merely implementing {@link Aware} provides no default
* functionality. Rather, processing must be done explicitly, for example
* in a {@link org.springframework.beans.factory.config.BeanPostProcessor BeanPostProcessor}.
* Refer to {@link org.springframework.context.support.ApplicationContextAwareProcessor}
* and {@link org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory}
* for examples of processing {@code *Aware} interface callbacks.
*
* @author Chris Beams
* @since 3.1
*/
public interface Aware {
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2006 the original author or authors.
* Copyright 2002-2011 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.
@ -29,12 +29,13 @@ package org.springframework.beans.factory;
* {@link BeanFactory BeanFactory javadocs}.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 2.0
* @see BeanNameAware
* @see BeanFactoryAware
* @see InitializingBean
*/
public interface BeanClassLoaderAware {
public interface BeanClassLoaderAware extends Aware {
/**
* Callback that supplies the bean {@link ClassLoader class loader} to

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* 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.
@ -31,13 +31,14 @@ import org.springframework.beans.BeansException;
* {@link BeanFactory BeanFactory javadocs}.
*
* @author Rod Johnson
* @author Chris Beams
* @since 11.03.2003
* @see BeanNameAware
* @see BeanClassLoaderAware
* @see InitializingBean
* @see org.springframework.context.ApplicationContextAware
*/
public interface BeanFactoryAware {
public interface BeanFactoryAware extends Aware {
/**
* Callback that supplies the owning factory to a bean instance.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2011 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.
@ -27,12 +27,13 @@ package org.springframework.beans.factory;
* {@link BeanFactory BeanFactory javadocs}.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 01.11.2003
* @see BeanClassLoaderAware
* @see BeanFactoryAware
* @see InitializingBean
*/
public interface BeanNameAware {
public interface BeanNameAware extends Aware {
/**
* Set the name of the bean in the bean factory that created this bean.

View File

@ -48,6 +48,7 @@ import org.springframework.beans.PropertyAccessorUtils;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.TypeConverter;
import org.springframework.beans.factory.Aware;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
@ -1428,6 +1429,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
}
private void invokeAwareMethods(final String beanName, final Object bean) {
if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
@ -1438,6 +1440,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}
/**
* Give a bean a chance to react now all its properties are set,

View File

@ -203,7 +203,6 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
*/
public void setAutowireCandidateResolver(final AutowireCandidateResolver autowireCandidateResolver) {
Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null");
// TODO SPR-7515: should also do EnvironmentAware injection here?
if (autowireCandidateResolver instanceof BeanFactoryAware) {
if (System.getSecurityManager() != null) {
final BeanFactory target = this;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2006 the original author or authors.
* Copyright 2002-2011 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.
@ -17,6 +17,7 @@
package org.springframework.scheduling.quartz;
import org.quartz.SchedulerContext;
import org.springframework.beans.factory.Aware;
/**
* Callback interface to be implemented by Spring-managed
@ -27,11 +28,12 @@ import org.quartz.SchedulerContext;
* that are passed in via Spring's SchedulerFactoryBean.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 2.0
* @see org.quartz.spi.JobFactory
* @see SchedulerFactoryBean#setJobFactory
*/
public interface SchedulerContextAware {
public interface SchedulerContextAware extends Aware {
/**
* Set the SchedulerContext of the current Quartz Scheduler.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2011 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.
@ -17,6 +17,7 @@
package org.springframework.context;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;
/**
* Interface to be implemented by any object that wishes to be notified
@ -48,13 +49,14 @@ import org.springframework.beans.BeansException;
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Chris Beams
* @see ResourceLoaderAware
* @see ApplicationEventPublisherAware
* @see MessageSourceAware
* @see org.springframework.context.support.ApplicationObjectSupport
* @see org.springframework.beans.factory.BeanFactoryAware
*/
public interface ApplicationContextAware {
public interface ApplicationContextAware extends Aware {
/**
* Set the ApplicationContext that this object runs in.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2005 the original author or authors.
* Copyright 2002-2011 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,16 +16,19 @@
package org.springframework.context;
import org.springframework.beans.factory.Aware;
/**
* Interface to be implemented by any object that wishes to be notified
* of the ApplicationEventPublisher (typically the ApplicationContext)
* that it runs in.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 1.1.1
* @see ApplicationContextAware
*/
public interface ApplicationEventPublisherAware {
public interface ApplicationEventPublisherAware extends Aware {
/**
* Set the ApplicationEventPublisher that this object runs in.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 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,6 +16,7 @@
package org.springframework.context;
import org.springframework.beans.factory.Aware;
import org.springframework.util.StringValueResolver;
/**
@ -26,10 +27,11 @@ import org.springframework.util.StringValueResolver;
* ApplicationContextAware/BeanFactoryAware interfaces.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 3.0.3
* @see org.springframework.beans.factory.config.ConfigurableBeanFactory#resolveEmbeddedValue
*/
public interface EmbeddedValueResolverAware {
public interface EmbeddedValueResolverAware extends Aware {
/**
* Set the StringValueResolver to use for resolving embedded definition values.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 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,6 +16,7 @@
package org.springframework.context;
import org.springframework.beans.factory.Aware;
import org.springframework.core.env.Environment;
/**
@ -25,7 +26,7 @@ import org.springframework.core.env.Environment;
* @author Chris Beams
* @since 3.1
*/
public interface EnvironmentAware {
public interface EnvironmentAware extends Aware {
/**
* Set the {@code Environment} that this object runs in.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2005 the original author or authors.
* Copyright 2002-2011 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,6 +16,8 @@
package org.springframework.context;
import org.springframework.beans.factory.Aware;
/**
* Interface to be implemented by any object that wishes to be notified
* of the MessageSource (typically the ApplicationContext) that it runs in.
@ -25,10 +27,11 @@ package org.springframework.context;
* it is defined as bean with name "messageSource" in the application context.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 1.1.1
* @see ApplicationContextAware
*/
public interface MessageSourceAware {
public interface MessageSourceAware extends Aware {
/**
* Set the MessageSource that this object runs in.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2006 the original author or authors.
* Copyright 2002-2011 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,6 +16,7 @@
package org.springframework.context;
import org.springframework.beans.factory.Aware;
import org.springframework.core.io.ResourceLoader;
/**
@ -47,6 +48,7 @@ import org.springframework.core.io.ResourceLoader;
* automatic type conversion by the bean factory.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 10.03.2004
* @see ApplicationContextAware
* @see org.springframework.beans.factory.InitializingBean
@ -57,7 +59,7 @@ import org.springframework.core.io.ResourceLoader;
* @see org.springframework.core.io.support.PathMatchingResourcePatternResolver
* @see org.springframework.context.support.ReloadableResourceBundleMessageSource
*/
public interface ResourceLoaderAware {
public interface ResourceLoaderAware extends Aware {
/**
* Set the ResourceLoader that this object runs in.

View File

@ -21,6 +21,7 @@ import java.security.AccessController;
import java.security.PrivilegedAction;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContextAware;
@ -97,6 +98,7 @@ class ApplicationContextAwareProcessor implements BeanPostProcessor {
}
private void invokeAwareInterfaces(Object bean) {
if (bean instanceof Aware) {
if (bean instanceof EmbeddedValueResolverAware) {
((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(
new EmbeddedValueResolver(this.applicationContext.getBeanFactory()));
@ -117,6 +119,7 @@ class ApplicationContextAwareProcessor implements BeanPostProcessor {
((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
}
}
}
public Object postProcessAfterInitialization(Object bean, String beanName) {
return bean;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2011 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,6 +16,7 @@
package org.springframework.context.weaving;
import org.springframework.beans.factory.Aware;
import org.springframework.instrument.classloading.LoadTimeWeaver;
/**
@ -23,10 +24,11 @@ import org.springframework.instrument.classloading.LoadTimeWeaver;
* of the application context's default {@link LoadTimeWeaver}.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 2.5
* @see org.springframework.context.ConfigurableApplicationContext#LOAD_TIME_WEAVER_BEAN_NAME
*/
public interface LoadTimeWeaverAware {
public interface LoadTimeWeaverAware extends Aware {
/**
* Set the {@link LoadTimeWeaver} of this object's containing

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2011 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,6 +16,8 @@
package org.springframework.jmx.export.notification;
import org.springframework.beans.factory.Aware;
/**
* Interface to be implemented by any Spring-managed resource that is to be
* registered with an {@link javax.management.MBeanServer} and wishes to send
@ -33,10 +35,11 @@ package org.springframework.jmx.export.notification;
* interface (or implementing a full {@link javax.management.modelmbean.ModelMBean}).
*
* @author Rob Harrop
* @author Chris Beams
* @since 2.0
* @see NotificationPublisher
*/
public interface NotificationPublisherAware {
public interface NotificationPublisherAware extends Aware {
/**
* Set the {@link NotificationPublisher} instance for the current managed resource instance.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2011 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.
@ -18,16 +18,19 @@ package org.springframework.jca.context;
import javax.resource.spi.BootstrapContext;
import org.springframework.beans.factory.Aware;
/**
* Interface to be implemented by any object that wishes to be
* notified of the BootstrapContext (typically determined by the
* {@link ResourceAdapterApplicationContext}) that it runs in.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 2.5
* @see javax.resource.spi.BootstrapContext
*/
public interface BootstrapContextAware {
public interface BootstrapContextAware extends Aware {
/**
* Set the BootstrapContext that this object runs in.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2005 the original author or authors.
* Copyright 2002-2011 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.
@ -18,16 +18,19 @@ package org.springframework.web.portlet.context;
import javax.portlet.PortletConfig;
import org.springframework.beans.factory.Aware;
/**
* Interface to be implemented by any object that wishes to be notified
* of the PortletConfig (typically determined by the PortletApplicationContext)
* that it runs in.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 2.0
* @see PortletContextAware
*/
public interface PortletConfigAware {
public interface PortletConfigAware extends Aware {
/**
* Set the PortletConfigthat this object runs in.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2005 the original author or authors.
* Copyright 2002-2011 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.
@ -18,6 +18,8 @@ package org.springframework.web.portlet.context;
import javax.portlet.PortletContext;
import org.springframework.beans.factory.Aware;
/**
* Interface to be implemented by any object that wishes to be notified
* of the PortletContext (typically determined by the PortletApplicationContext)
@ -25,10 +27,11 @@ import javax.portlet.PortletContext;
*
* @author Juergen Hoeller
* @author William G. Thompson, Jr.
* @author Chris Beams
* @since 2.0
* @see PortletConfigAware
*/
public interface PortletContextAware {
public interface PortletContextAware extends Aware {
/**
* Set the PortletContext that this object runs in.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2006 the original author or authors.
* Copyright 2002-2011 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.
@ -18,6 +18,8 @@ package org.springframework.web.context;
import javax.servlet.ServletConfig;
import org.springframework.beans.factory.Aware;
/**
* Interface to be implemented by any object that wishes to be notified
* of the ServletConfig (typically determined by the WebApplicationContext)
@ -28,10 +30,11 @@ import javax.servlet.ServletConfig;
* elsewhere, an exception will be thrown on bean creation.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 2.0
* @see ServletContextAware
*/
public interface ServletConfigAware {
public interface ServletConfigAware extends Aware {
/**
* Set the ServletConfig that this object runs in.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2005 the original author or authors.
* Copyright 2002-2011 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.
@ -18,16 +18,19 @@ package org.springframework.web.context;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.Aware;
/**
* Interface to be implemented by any object that wishes to be notified
* of the ServletContext (typically determined by the WebApplicationContext)
* that it runs in.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 12.03.2004
* @see ServletConfigAware
*/
public interface ServletContextAware {
public interface ServletContextAware extends Aware {
/**
* Set the ServletContext that this object runs in.