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