您当前的位置:首页 >> 家居百科

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的下述解构模式,同学们可以自己方法论一下。

眼睛视疲劳用哪个眼药水
吃什么药物能缓解肌肉拉伤疼
半夜上吐下泻应急措施
南京妇科检查哪些项目
济南男科医院排名
相关阅读
友情链接