|
| 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.bean.lifecycle; |
| 18 | + |
| 19 | +import org.geekbang.thinking.in.spring.ioc.overview.domain.User; |
| 20 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 21 | +import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; |
| 22 | +import org.springframework.context.support.ClassPathXmlApplicationContext; |
| 23 | + |
| 24 | +/** |
| 25 | + * Bean 实例化生命周期示例 |
| 26 | + * |
| 27 | + * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a> |
| 28 | + * @since |
| 29 | + */ |
| 30 | +public class BeanInstantiationLifecycleDemo { |
| 31 | + |
| 32 | + public static void main(String[] args) { |
| 33 | + executeBeanFactory(); |
| 34 | + |
| 35 | + System.out.println("--------------------------------"); |
| 36 | + |
| 37 | + executeApplicationContext(); |
| 38 | + } |
| 39 | + |
| 40 | + private static void executeBeanFactory() { |
| 41 | + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
| 42 | + // 方法一:添加 BeanPostProcessor 实现 MyInstantiationAwareBeanPostProcessor |
| 43 | + // beanFactory.addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor()); |
| 44 | + // 方法二:将 MyInstantiationAwareBeanPostProcessor 作为 Bean 注册 |
| 45 | + // 基于 XML 资源 BeanDefinitionReader 实现 |
| 46 | + XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); |
| 47 | + String[] locations = {"META-INF/dependency-lookup-context.xml", "META-INF/bean-constructor-dependency-injection.xml"}; |
| 48 | + int beanNumbers = beanDefinitionReader.loadBeanDefinitions(locations); |
| 49 | + System.out.println("已加载 BeanDefinition 数量:" + beanNumbers); |
| 50 | + // 通过 Bean Id 和类型进行依赖查找 |
| 51 | + User user = beanFactory.getBean("user", User.class); |
| 52 | + System.out.println(user); |
| 53 | + |
| 54 | + User superUser = beanFactory.getBean("superUser", User.class); |
| 55 | + System.out.println(superUser); |
| 56 | + |
| 57 | + // 构造器注入按照类型注入,resolveDependency |
| 58 | + UserHolder userHolder = beanFactory.getBean("userHolder", UserHolder.class); |
| 59 | + System.out.println(userHolder); |
| 60 | + } |
| 61 | + |
| 62 | + private static void executeApplicationContext() { |
| 63 | + ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); |
| 64 | + String[] locations = {"META-INF/dependency-lookup-context.xml", "META-INF/bean-constructor-dependency-injection.xml"}; |
| 65 | + applicationContext.setConfigLocations(locations); |
| 66 | + // 启动应用上下文 |
| 67 | + applicationContext.refresh(); |
| 68 | + |
| 69 | + User user = applicationContext.getBean("user", User.class); |
| 70 | + System.out.println(user); |
| 71 | + |
| 72 | + User superUser = applicationContext.getBean("superUser", User.class); |
| 73 | + System.out.println(superUser); |
| 74 | + |
| 75 | + // 构造器注入按照类型注入,resolveDependency |
| 76 | + UserHolder userHolder = applicationContext.getBean("userHolder", UserHolder.class); |
| 77 | + System.out.println(userHolder); |
| 78 | + |
| 79 | + // 关闭应用上下文 |
| 80 | + applicationContext.close(); |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | +} |
| 86 | + |
0 commit comments