KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > support > ApplicationContextAwareProcessor


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.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  * BeanPostProcessor implementation that passes the ApplicationContext to beans
32  * that implement the {@link ResourceLoaderAware}, {@link ApplicationEventPublisherAware},
33  * {@link MessageSourceAware} and/or {@link ApplicationContextAware} interfaces.
34  * If all of them are implemented, they are satisfied in the given order.
35  *
36  * <p>Application contexts will automatically register this with their
37  * underlying bean factory. Applications do not use this directly.
38  *
39  * @author Juergen Hoeller
40  * @since 10.10.2003
41  * @see org.springframework.context.ResourceLoaderAware
42  * @see org.springframework.context.ApplicationEventPublisherAware
43  * @see org.springframework.context.MessageSourceAware
44  * @see org.springframework.context.ApplicationContextAware
45  * @see org.springframework.context.support.AbstractApplicationContext#refresh()
46  */

47 public class ApplicationContextAwareProcessor implements BeanPostProcessor {
48
49     protected final Log logger = LogFactory.getLog(getClass());
50
51     private final ApplicationContext applicationContext;
52
53
54     /**
55      * Create a new ApplicationContextAwareProcessor for the given context.
56      */

57     public ApplicationContextAwareProcessor(ApplicationContext applicationContext) {
58         this.applicationContext = applicationContext;
59     }
60
61
62     public Object JavaDoc postProcessBeforeInitialization(Object JavaDoc bean, String JavaDoc beanName) throws BeansException {
63         if (bean instanceof ResourceLoaderAware) {
64             if (logger.isTraceEnabled()) {
65                 logger.trace("Invoking setResourceLoader on ResourceLoaderAware bean '" + beanName + "'");
66             }
67             ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
68         }
69         if (bean instanceof ApplicationEventPublisherAware) {
70             if (logger.isTraceEnabled()) {
71                 logger.trace("Invoking setApplicationEventPublisher on ApplicationEventPublisherAware bean '" +
72                         beanName + "'");
73             }
74             ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
75         }
76         if (bean instanceof MessageSourceAware) {
77             if (logger.isTraceEnabled()) {
78                 logger.trace("Invoking setMessageSource on MessageSourceAware bean '" + beanName + "'");
79             }
80             ((MessageSourceAware) bean).setMessageSource(this.applicationContext);
81         }
82         if (bean instanceof ApplicationContextAware) {
83             if (logger.isTraceEnabled()) {
84                 logger.trace("Invoking setApplicationContext on ApplicationContextAware bean '" + beanName + "'");
85             }
86             ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
87         }
88         return bean;
89     }
90
91     public Object JavaDoc postProcessAfterInitialization(Object JavaDoc bean, String JavaDoc name) {
92         return bean;
93     }
94
95 }
96
Popular Tags