001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.context.weaving;
018:
019: import org.springframework.beans.BeansException;
020: import org.springframework.beans.factory.BeanFactory;
021: import org.springframework.beans.factory.BeanFactoryAware;
022: import org.springframework.beans.factory.config.BeanPostProcessor;
023: import org.springframework.context.ConfigurableApplicationContext;
024: import org.springframework.instrument.classloading.LoadTimeWeaver;
025: import org.springframework.util.Assert;
026:
027: /**
028: * {@link org.springframework.beans.factory.config.BeanPostProcessor}
029: * implementation that passes the context's default {@link LoadTimeWeaver}
030: * to beans that implement the {@link LoadTimeWeaverAware} interface.
031: *
032: * <p>{@link org.springframework.context.ApplicationContext Application contexts}
033: * will automatically register this with their underlying
034: * {@link BeanFactory bean factory}, provided that a default
035: * <code>LoadTimeWeaver</code> is actually available.
036: *
037: * <p>Applications should not use this class directly.
038: *
039: * @author Juergen Hoeller
040: * @since 2.5
041: * @see LoadTimeWeaverAware
042: * @see org.springframework.context.ConfigurableApplicationContext#LOAD_TIME_WEAVER_BEAN_NAME
043: */
044: public class LoadTimeWeaverAwareProcessor implements BeanPostProcessor,
045: BeanFactoryAware {
046:
047: private LoadTimeWeaver loadTimeWeaver;
048:
049: private BeanFactory beanFactory;
050:
051: /**
052: * Create a new <code>LoadTimeWeaverAwareProcessor</code> that will
053: * auto-retrieve the {@link LoadTimeWeaver} from the containing
054: * {@link BeanFactory}, expecting a bean named
055: * {@link ConfigurableApplicationContext#LOAD_TIME_WEAVER_BEAN_NAME "loadTimeWeaver"}.
056: */
057: public LoadTimeWeaverAwareProcessor() {
058: }
059:
060: /**
061: * Create a new <code>LoadTimeWeaverAwareProcessor</code> for the given
062: * {@link LoadTimeWeaver}.
063: * <p>If the given <code>loadTimeWeaver</code> is <code>null</code>, then a
064: * <code>LoadTimeWeaver</code> will be auto-retrieved from the containing
065: * {@link BeanFactory}, expecting a bean named
066: * {@link ConfigurableApplicationContext#LOAD_TIME_WEAVER_BEAN_NAME "loadTimeWeaver"}.
067: * @param loadTimeWeaver the specific <code>LoadTimeWeaver</code> that is to be used; can be <code>null</code>
068: */
069: public LoadTimeWeaverAwareProcessor(LoadTimeWeaver loadTimeWeaver) {
070: this .loadTimeWeaver = loadTimeWeaver;
071: }
072:
073: public void setBeanFactory(BeanFactory beanFactory) {
074: this .beanFactory = beanFactory;
075: }
076:
077: public Object postProcessBeforeInitialization(Object bean,
078: String beanName) throws BeansException {
079: if (bean instanceof LoadTimeWeaverAware) {
080: LoadTimeWeaver ltw = this .loadTimeWeaver;
081: if (ltw == null) {
082: Assert
083: .state(this .beanFactory != null,
084: "BeanFactory required if no LoadTimeWeaver explicitly specified");
085: ltw = (LoadTimeWeaver) this .beanFactory
086: .getBean(
087: ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME,
088: LoadTimeWeaver.class);
089: }
090: ((LoadTimeWeaverAware) bean).setLoadTimeWeaver(ltw);
091: }
092: return bean;
093: }
094:
095: public Object postProcessAfterInitialization(Object bean,
096: String name) {
097: return bean;
098: }
099:
100: }
|