|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.geekbang.thinking.in.spring.dependency.lookup; |
| 18 | + |
| 19 | +import org.springframework.beans.factory.BeanFactory; |
| 20 | +import org.springframework.beans.factory.HierarchicalBeanFactory; |
| 21 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 22 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 23 | +import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; |
| 24 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 25 | + |
| 26 | +/** |
| 27 | + * 层次性依赖查找示例 |
| 28 | + * |
| 29 | + * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a> |
| 30 | + * @since |
| 31 | + */ |
| 32 | +public class HierarchicalDependencyLookupDemo { |
| 33 | + |
| 34 | + public static void main(String[] args) { |
| 35 | + // 创建 BeanFactory 容器 |
| 36 | + AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); |
| 37 | + // 将当前类 ObjectProviderDemo 作为配置类(Configuration Class) |
| 38 | + applicationContext.register(ObjectProviderDemo.class); |
| 39 | + |
| 40 | + // 1. 获取 HierarchicalBeanFactory <- ConfigurableBeanFactory <- ConfigurableListableBeanFactory |
| 41 | + ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory(); |
| 42 | +// System.out.println("当前 BeanFactory 的 Parent BeanFactory : " + beanFactory.getParentBeanFactory()); |
| 43 | + |
| 44 | + // 2. 设置 Parent BeanFactory |
| 45 | + HierarchicalBeanFactory parentBeanFactory = createParentBeanFactory(); |
| 46 | + beanFactory.setParentBeanFactory(parentBeanFactory); |
| 47 | +// System.out.println("当前 BeanFactory 的 Parent BeanFactory : " + beanFactory.getParentBeanFactory()); |
| 48 | + |
| 49 | + displayContainsLocalBean(beanFactory, "user"); |
| 50 | + displayContainsLocalBean(parentBeanFactory, "user"); |
| 51 | + |
| 52 | + displayContainsBean(beanFactory, "user"); |
| 53 | + displayContainsBean(parentBeanFactory, "user"); |
| 54 | + |
| 55 | + // 启动应用上下文 |
| 56 | + applicationContext.refresh(); |
| 57 | + |
| 58 | + // 关闭应用上下文 |
| 59 | + applicationContext.close(); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + private static void displayContainsBean(HierarchicalBeanFactory beanFactory, String beanName) { |
| 64 | + System.out.printf("当前 BeanFactory[%s] 是否包含 Bean[name : %s] : %s\n", beanFactory, beanName, |
| 65 | + containsBean(beanFactory, beanName)); |
| 66 | + } |
| 67 | + |
| 68 | + private static boolean containsBean(HierarchicalBeanFactory beanFactory, String beanName) { |
| 69 | + BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory(); |
| 70 | + if (parentBeanFactory instanceof HierarchicalBeanFactory) { |
| 71 | + HierarchicalBeanFactory parentHierarchicalBeanFactory = HierarchicalBeanFactory.class.cast(parentBeanFactory); |
| 72 | + if (containsBean(parentHierarchicalBeanFactory, beanName)) { |
| 73 | + return true; |
| 74 | + } |
| 75 | + } |
| 76 | + return beanFactory.containsLocalBean(beanName); |
| 77 | + } |
| 78 | + |
| 79 | + private static void displayContainsLocalBean(HierarchicalBeanFactory beanFactory, String beanName) { |
| 80 | + System.out.printf("当前 BeanFactory[%s] 是否包含 Local Bean[name : %s] : %s\n", beanFactory, beanName, |
| 81 | + beanFactory.containsLocalBean(beanName)); |
| 82 | + } |
| 83 | + |
| 84 | + private static ConfigurableListableBeanFactory createParentBeanFactory() { |
| 85 | + // 创建 BeanFactory 容器 |
| 86 | + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
| 87 | + XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); |
| 88 | + // XML 配置文件 ClassPath 路径 |
| 89 | + String location = "classpath:/META-INF/dependency-lookup-context.xml"; |
| 90 | + // 加载配置 |
| 91 | + reader.loadBeanDefinitions(location); |
| 92 | + return beanFactory; |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments