使用 Spring IoC 容器-12

使用 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>
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只因为你温柔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值