01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: * Create a new ApplicationContextAwareProcessor for the given context.
52: */
53: public ApplicationContextAwareProcessor(
54: ApplicationContext applicationContext) {
55: this .applicationContext = applicationContext;
56: }
57:
58: public Object postProcessBeforeInitialization(Object bean,
59: String beanName) throws BeansException {
60: if (bean instanceof ResourceLoaderAware) {
61: ((ResourceLoaderAware) bean)
62: .setResourceLoader(this .applicationContext);
63: }
64: if (bean instanceof ApplicationEventPublisherAware) {
65: ((ApplicationEventPublisherAware) bean)
66: .setApplicationEventPublisher(this .applicationContext);
67: }
68: if (bean instanceof MessageSourceAware) {
69: ((MessageSourceAware) bean)
70: .setMessageSource(this .applicationContext);
71: }
72: if (bean instanceof ApplicationContextAware) {
73: ((ApplicationContextAware) bean)
74: .setApplicationContext(this .applicationContext);
75: }
76: return bean;
77: }
78:
79: public Object postProcessAfterInitialization(Object bean,
80: String name) {
81: return bean;
82: }
83:
84: }
|