Spring Framework BeanDefinition的实例解构
2023-05-01 12:16:09
ser(); } @Override public Class getObjectType() { return User.class; }}/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.eleven.thinking.in.spring.bean.factory;import org.eleven.thinking.in.spring.ioc.overview.domain.User;/** * {@link User} 加工厂类 * * @author eleven * @since */public interface UserFactory { default User createUser() { return User.createUser(); }}/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.eleven.thinking.in.spring.bean.factory;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;/** * 默认 {@link UserFactory} 解决问题 * * @author eleven * @since */public class DefaultUserFactory implements UserFactory, InitializingBean, DisposableBean { // 1. 基于 @PostConstruct 批注 @PostConstruct public void init() { System.out.println("@PostConstruct : UserFactory 模板中的..."); } public void initUserFactory() { System.out.println("内置模板原理 initUserFactory() : UserFactory 模板中的..."); } @Override public void afterPropertiesSet() throws Exception { System.out.println("InitializingBean#afterPropertiesSet() : UserFactory 模板中的..."); } @PreDestroy public void preDestroy() { System.out.println("@PreDestroy : UserFactory 丢弃中的..."); } @Override public void destroy() throws Exception { System.out.println("DisposableBean#destroy() : UserFactory 丢弃中的..."); } public void doDestroy() { System.out.println("内置丢弃原理 doDestroy() : UserFactory 丢弃中的..."); } @Override public void finalize() throws Throwable { System.out.println("当前 DefaultUserFactory 对象正在被废料重复使用..."); }}/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.eleven.thinking.in.spring.bean.definition;import org.eleven.thinking.in.spring.ioc.overview.domain.User;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Bean 下述解构下述 * * @author eleven * @since */public class BeanInstantiationDemo { public static void main(String[] args) { // 的设计 XML 运用于程序 // 叫停 Spring 运用于上下文 BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:/META-INF/bean-instantiation-context.xml"); User user = beanFactory.getBean("user-by-static-method", User.class); User userByInstanceMethod = beanFactory.getBean("user-by-instance-method", User.class); User userByFactoryBean = beanFactory.getBean("user-by-factory-bean", User.class); System.out.println(user); System.out.println(userByInstanceMethod); System.out.println(userByFactoryBean); System.out.println(user == userByInstanceMethod); System.out.println(user == userByFactoryBean); }}org.eleven.thinking.in.spring.bean.factory.DefaultUserFactory/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.eleven.thinking.in.spring.bean.definition;import org.eleven.thinking.in.spring.bean.factory.DefaultUserFactory;import org.eleven.thinking.in.spring.bean.factory.UserFactory;import org.eleven.thinking.in.spring.ioc.overview.domain.User;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.config.AutowireCapableBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.Iterator;import java.util.ServiceLoader;import static java.util.ServiceLoader.load;/** * 一般来说的 Bean 下述解构下述 * * @author eleven * @since */public class SpecialBeanInstantiationDemo { public static void main(String[] args) { // 的设计 XML 运用于程序 // 叫停 Spring 运用于上下文 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/META-INF/special-bean-instantiation-context.xml"); // 通过 ApplicationContext 获取 AutowireCapableBeanFactory AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory(); ServiceLoader serviceLoader = beanFactory.getBean("userFactoryServiceLoader", ServiceLoader.class); displayServiceLoader(serviceLoader);// demoServiceLoader(); // 创建 UserFactory 对象,通过 AutowireCapableBeanFactory UserFactory userFactory = beanFactory.createBean(DefaultUserFactory.class); System.out.println(userFactory.createUser()); } public static void demoServiceLoader() { ServiceLoader serviceLoader = load(UserFactory.class, Thread.currentThread().getContextClassLoader()); displayServiceLoader(serviceLoader); } private static void displayServiceLoader(ServiceLoader serviceLoader) { Iterator iterator = serviceLoader.iterator(); while (iterator.hasNext()) { UserFactory userFactory = iterator.next(); System.out.println(userFactory.createUser()); } }}
吃什么药物能缓解肌肉拉伤疼
半夜上吐下泻应急措施
南京妇科检查哪些项目
济南男科医院排名
以上我们独自一人方法论了相同的BeanDefinition的下述解构模式,同学们可以自己方法论一下。
。眼睛视疲劳用哪个眼药水吃什么药物能缓解肌肉拉伤疼
半夜上吐下泻应急措施
南京妇科检查哪些项目
济南男科医院排名
相关阅读
- 安徽男子结婚,6个姐姐随天价礼金,网友却发掘出姐夫一个都没来!
- “被领导骂出生理反应后,我复职了”
- 绿色动力环保(01330)公布2022年去年 归属于上市公司股东的净利润约7.45亿元 同比增长6.73%
- 小舅子借五万八年未还,如今岳母身故他就来还钱,我立马撵走了他
- 如果你的话语带着叉,让人不舒服,如果是你你还会继续听下去吗?
- 中国神秘女富豪首次崭露头角
- 独子在我家养老9年,没花一分钱,去世后,给我留下9个酸菜坛子
- 给妈妈煮一锅花样饭,健脾胃易消化,晚餐坚持吃,身材换成S码
- 新股消息丨永联丰控股(08617)申请创业板转主板主板 为华南最大的回转支承制造商
- 回娘家玩几天,半夜渴了去喝水,听见祖父母房间传来不可描述的声音
- 原来,当女人们真心爱上你,真的很不一样
- 大众公用(01635)拟公开发行公司债券及境外债
- 28岁女子,每天接30多个,被抓后哭诉:我不卖怎么买房!
- 女人不和你谈论这些问题,说明没打算和你有更进一步
- 中国交通建设(01800)附属三航局对舟山群岛开发减资1.47亿元
- 男医生动手体检时30岁妇女,医生:太漂亮了 没忍住
- 女人的这些举动,证明她之前厌倦你了
- 时代电气(03898)拟对闲置自有资金进行现金管理,使用利息不超100亿元
- 六旬老汉车祸猝死,女被抓后哭诉:让他戴套可他就是不听
- 为什么你追勉强女生