使用 Spring IoC 容器
-
BeanFactory 是 Spring 底层 IoC 容器
package org.xiaoge.thinking.in.spring.ioc.overview.dependency.container; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.xiaoge.thinking.in.spring.ioc.overview.domain.User; import java.util.Map; /** * {@like BeanFactory} 作为 Ioc 容器示例 * @Classname IoCContainer * @Date 2022/11/2 13:29 * @Created by ZhangXiao * @Description TODO * * 1. BeanFactory容器 就 没有ApplicationContext提供的那些特性了 * 2. 这是一个典型的IoC底层容器 * 3. 如果你是xml场景的时候, 并且并且不需要那么多复杂路径的时候, 你用这个东西完全足以胜任你的工作 * */ public class BeanFactoryAsIoCContainerDemo { public static void main(String[] args) { // 创建BeanFactory容器 DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); // XML 配置文件 ClassPath 路径 String location = "classpath:META-INF/dependency-lookup-context.xml"; // 加载配置 int beanDefinitionsCount = reader.loadBeanDefinitions(location); System.out.println("Bean 定义加载的数量: " + beanDefinitionsCount); // 依赖查找集合对对象 lookupCollectionByType(beanFactory); } /** * 根据类型查找集合对象 * @param beanFactory */ private static void lookupCollectionByType(BeanFactory beanFactory) { if (beanFactory instanceof ListableBeanFactory) { ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory; Map<String, User> map = listableBeanFactory.getBeansOfType(User.class); System.out.println("查找到所有的 user 集合对象: " + map); } } } /* 运行结果 Bean 定义加载的数量: 3 查找到所有的 user 集合对象: {user=User{id=1, name='xiaoge'}, superUser=SuperUser{address='武汉'} User{id=1, name='xiaoge'}} */
-
ApplicationContext 是具备应用特性的 BeanFactory 超集
package org.xiaoge.thinking.in.spring.ioc.overview.dependency.container; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableMBeanExport; import org.xiaoge.thinking.in.spring.ioc.overview.domain.User; import javax.jws.soap.SOAPBinding; import java.util.Map; /** * {@like ApplicationContext} 作为 Ioc 容器示例 * @Classname IoCContainer * @Date 2022/11/2 13:29 * @Created by ZhangXiao * @Description TODO * */ public class AnnotationApplicationContextAsIoCContainerDemo { public static void main(String[] args) { // 创建 ApplicationContext 容器 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); // 讲当前类 AnnotationApplicationContextAsIoCContainerDemo 作为配置类 (Configuration Class) applicationContext.register(AnnotationApplicationContextAsIoCContainerDemo.class); // 启动应用上下文 applicationContext.refresh(); // 依赖查找集合对象 lookupCollectionByType(applicationContext); } /** * 通过 Java 注解的方式, 定义了一个 Bean * @return */ @Bean public User user() { User user = new User(); user.setId(1L); user.setName("xiaoge"); return user; } /** * 根据类型查找集合对象 * @param beanFactory */ private static void lookupCollectionByType(BeanFactory beanFactory) { if (beanFactory instanceof ListableBeanFactory) { ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory; Map<String, User> map = listableBeanFactory.getBeansOfType(User.class); System.out.println("查找到所有的 user 集合对象: " + map); } } } /* 运行结果 查找到所有的 user 集合对象: {user=User{id=1, name='xiaoge'}} */
-
dependency-lookup-context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 实施加载bean--> <bean id="user" class="org.xiaoge.thinking.in.spring.ioc.overview.domain.User"> <property name="id" value="1" /> <property name="name" value="xiaoge"/> </bean> <!-- 延迟bean 是沟通objectFactory简介创建的 --> <bean id="objectFactory" class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean"> <property name="targetBeanName" value="user" /> </bean> <!-- parent继承 --> <bean id="superUser" class="org.xiaoge.thinking.in.spring.ioc.overview.domain.SuperUser" parent="user" primary="true"> <property name="address" value="武汉" /> </bean> </beans>