1 /*
2 * Copyright 2002-2007 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
17 package org.springframework.context.support;
18
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.ApplicationEventPublisherAware;
24 import org.springframework.context.MessageSourceAware;
25 import org.springframework.context.ResourceLoaderAware;
26
27 /**
28 * {@link org.springframework.beans.factory.config.BeanPostProcessor}
29 * implementation that passes the ApplicationContext to beans that
30 * implement the {@link ResourceLoaderAware}, {@link MessageSourceAware},
31 * {@link ApplicationEventPublisherAware} and/or
32 * {@link ApplicationContextAware} interfaces.
33 * If all of them are implemented, they are satisfied in the given order.
34 *
35 * <p>Application contexts will automatically register this with their
36 * underlying bean factory. Applications do not use this directly.
37 *
38 * @author Juergen Hoeller
39 * @since 10.10.2003
40 * @see org.springframework.context.ResourceLoaderAware
41 * @see org.springframework.context.MessageSourceAware
42 * @see org.springframework.context.ApplicationEventPublisherAware
43 * @see org.springframework.context.ApplicationContextAware
44 * @see org.springframework.context.support.AbstractApplicationContext#refresh()
45 */
46 class ApplicationContextAwareProcessor implements BeanPostProcessor {
47
48 private final ApplicationContext applicationContext;
49
50
51 /**
52 * Create a new ApplicationContextAwareProcessor for the given context.
53 */
54 public ApplicationContextAwareProcessor(ApplicationContext applicationContext) {
55 this.applicationContext = applicationContext;
56 }
57
58
59 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
60 if (bean instanceof ResourceLoaderAware) {
61 ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
62 }
63 if (bean instanceof ApplicationEventPublisherAware) {
64 ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
65 }
66 if (bean instanceof MessageSourceAware) {
67 ((MessageSourceAware) bean).setMessageSource(this.applicationContext);
68 }
69 if (bean instanceof ApplicationContextAware) {
70 ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
71 }
72 return bean;
73 }
74
75 public Object postProcessAfterInitialization(Object bean, String name) {
76 return bean;
77 }
78
79 }