1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.context.support;
  17. import org.apache.commons.logging.Log;
  18. import org.apache.commons.logging.LogFactory;
  19. import org.springframework.beans.BeansException;
  20. import org.springframework.beans.factory.config.BeanPostProcessor;
  21. import org.springframework.context.ApplicationContext;
  22. import org.springframework.context.ApplicationContextAware;
  23. import org.springframework.context.ResourceLoaderAware;
  24. /**
  25. * BeanPostProcessor implementation that passes the application context to
  26. * beans that implement the ApplicationContextAware or ResourceLoaderAware
  27. * interfaces. If both are implemented, the latter is satisfied first.
  28. *
  29. * <p>Application contexts will automatically register this with their
  30. * underlying bean factory. Applications do not use this directly.
  31. *
  32. * @author Juergen Hoeller
  33. * @since 10.10.2003
  34. * @see org.springframework.context.ApplicationContextAware
  35. * @see org.springframework.context.support.AbstractApplicationContext#refresh
  36. */
  37. public class ApplicationContextAwareProcessor implements BeanPostProcessor {
  38. protected final Log logger = LogFactory.getLog(getClass());
  39. private final ApplicationContext applicationContext;
  40. /**
  41. * Create a new ApplicationContextAwareProcessor for the given context.
  42. */
  43. public ApplicationContextAwareProcessor(ApplicationContext applicationContext) {
  44. this.applicationContext = applicationContext;
  45. }
  46. public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
  47. if (bean instanceof ResourceLoaderAware) {
  48. if (logger.isDebugEnabled()) {
  49. logger.debug("Invoking setResourceLoader on ResourceLoaderAware bean '" + name + "'");
  50. }
  51. ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
  52. }
  53. if (bean instanceof ApplicationContextAware) {
  54. if (logger.isDebugEnabled()) {
  55. logger.debug("Invoking setApplicationContext on ApplicationContextAware bean '" + name + "'");
  56. }
  57. ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
  58. }
  59. return bean;
  60. }
  61. public Object postProcessAfterInitialization(Object bean, String name) {
  62. return bean;
  63. }
  64. }