|
| 1 | +/* |
| 2 | + * Copyright 2012-2014 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.boot.autoconfigure.jdbc; |
| 18 | + |
| 19 | +import javax.sql.DataSource; |
| 20 | + |
| 21 | +import org.springframework.beans.BeansException; |
| 22 | +import org.springframework.beans.factory.BeanFactory; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 25 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
| 26 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 27 | +import org.springframework.beans.factory.support.GenericBeanDefinition; |
| 28 | +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; |
| 29 | +import org.springframework.core.type.AnnotationMetadata; |
| 30 | + |
| 31 | +/** |
| 32 | + * {@link BeanPostProcessor} used to fire {@link DataSourceInitializedEvent}s. Should only |
| 33 | + * be registered via the inner {@link Registrar} class. |
| 34 | + * |
| 35 | + * @author Dave Syer |
| 36 | + * @since 1.1.0 |
| 37 | + */ |
| 38 | +class DataSourceInitializerPostProcessor implements BeanPostProcessor { |
| 39 | + |
| 40 | + @Autowired |
| 41 | + private BeanFactory beanFactory; |
| 42 | + |
| 43 | + @Override |
| 44 | + public Object postProcessBeforeInitialization(Object bean, String beanName) |
| 45 | + throws BeansException { |
| 46 | + return bean; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Object postProcessAfterInitialization(Object bean, String beanName) |
| 51 | + throws BeansException { |
| 52 | + if (bean instanceof DataSource) { |
| 53 | + // force initialization of this bean as soon as we see a DataSource |
| 54 | + this.beanFactory.getBean(DataSourceInitializer.class); |
| 55 | + } |
| 56 | + return bean; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * {@link ImportBeanDefinitionRegistrar} to register the |
| 61 | + * {@link DataSourceInitializerPostProcessor} without causing early bean instantiation |
| 62 | + * issues. |
| 63 | + */ |
| 64 | + static class Registrar implements ImportBeanDefinitionRegistrar { |
| 65 | + |
| 66 | + private static final String BEAN_NAME = "dataSourceInitializerPostProcessor"; |
| 67 | + |
| 68 | + @Override |
| 69 | + public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, |
| 70 | + BeanDefinitionRegistry registry) { |
| 71 | + if (!registry.containsBeanDefinition(BEAN_NAME)) { |
| 72 | + GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); |
| 73 | + beanDefinition.setBeanClass(DataSourceInitializerPostProcessor.class); |
| 74 | + beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); |
| 75 | + // We don't need this one to be post processed otherwise it can cause a |
| 76 | + // cascade of bean instantiation that we would rather avoid. |
| 77 | + beanDefinition.setSynthetic(true); |
| 78 | + registry.registerBeanDefinition(BEAN_NAME, beanDefinition); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments