001/* 002 * Copyright 2002-2012 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 * https://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 017package org.springframework.context.weaving; 018 019import org.springframework.beans.BeansException; 020import org.springframework.beans.factory.BeanFactory; 021import org.springframework.beans.factory.BeanFactoryAware; 022import org.springframework.beans.factory.config.BeanPostProcessor; 023import org.springframework.context.ConfigurableApplicationContext; 024import org.springframework.instrument.classloading.LoadTimeWeaver; 025import 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 {@link BeanFactory bean factory}, 034 * provided that a default {@code LoadTimeWeaver} is actually available. 035 * 036 * <p>Applications should not use this class directly. 037 * 038 * @author Juergen Hoeller 039 * @since 2.5 040 * @see LoadTimeWeaverAware 041 * @see org.springframework.context.ConfigurableApplicationContext#LOAD_TIME_WEAVER_BEAN_NAME 042 */ 043public class LoadTimeWeaverAwareProcessor implements BeanPostProcessor, BeanFactoryAware { 044 045 private LoadTimeWeaver loadTimeWeaver; 046 047 private BeanFactory beanFactory; 048 049 050 /** 051 * Create a new {@code LoadTimeWeaverAwareProcessor} that will 052 * auto-retrieve the {@link LoadTimeWeaver} from the containing 053 * {@link BeanFactory}, expecting a bean named 054 * {@link ConfigurableApplicationContext#LOAD_TIME_WEAVER_BEAN_NAME "loadTimeWeaver"}. 055 */ 056 public LoadTimeWeaverAwareProcessor() { 057 } 058 059 /** 060 * Create a new {@code LoadTimeWeaverAwareProcessor} for the given 061 * {@link LoadTimeWeaver}. 062 * <p>If the given {@code loadTimeWeaver} is {@code null}, then a 063 * {@code LoadTimeWeaver} will be auto-retrieved from the containing 064 * {@link BeanFactory}, expecting a bean named 065 * {@link ConfigurableApplicationContext#LOAD_TIME_WEAVER_BEAN_NAME "loadTimeWeaver"}. 066 * @param loadTimeWeaver the specific {@code LoadTimeWeaver} that is to be used 067 */ 068 public LoadTimeWeaverAwareProcessor(LoadTimeWeaver loadTimeWeaver) { 069 this.loadTimeWeaver = loadTimeWeaver; 070 } 071 072 /** 073 * Create a new {@code LoadTimeWeaverAwareProcessor}. 074 * <p>The {@code LoadTimeWeaver} will be auto-retrieved from 075 * the given {@link BeanFactory}, expecting a bean named 076 * {@link ConfigurableApplicationContext#LOAD_TIME_WEAVER_BEAN_NAME "loadTimeWeaver"}. 077 * @param beanFactory the BeanFactory to retrieve the LoadTimeWeaver from 078 */ 079 public LoadTimeWeaverAwareProcessor(BeanFactory beanFactory) { 080 this.beanFactory = beanFactory; 081 } 082 083 084 @Override 085 public void setBeanFactory(BeanFactory beanFactory) { 086 this.beanFactory = beanFactory; 087 } 088 089 090 @Override 091 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 092 if (bean instanceof LoadTimeWeaverAware) { 093 LoadTimeWeaver ltw = this.loadTimeWeaver; 094 if (ltw == null) { 095 Assert.state(this.beanFactory != null, 096 "BeanFactory required if no LoadTimeWeaver explicitly specified"); 097 ltw = this.beanFactory.getBean( 098 ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME, LoadTimeWeaver.class); 099 } 100 ((LoadTimeWeaverAware) bean).setLoadTimeWeaver(ltw); 101 } 102 return bean; 103 } 104 105 @Override 106 public Object postProcessAfterInitialization(Object bean, String name) { 107 return bean; 108 } 109 110}