使用Spring IoC容器:選BeanFactory仍是ApplicationContext?

經過上一篇ApplicationContext特性分析,能夠得出結論BeanFactory 是 Bean容器,它不提供企業特性,好比AOP、事務以及事件等,這些都被ApplicationContext支持git

圖片

源碼案例分析 【https://gitee.com/icloud-iot/thinking-in-springspring

dependency-lookup-context.xmlapp

<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        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
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd"
>


<!--    <context:annotation-config/>-->

<!--    <context:component-scan base-package="org.acme" />-->

    <!-- Root BeanDefinition 不須要合併,不存在 parent -->
    <!-- 普通 beanDefinition GenericBeanDefinition -->
    <!-- 通過合併後 GenericBeanDefinition 變成 RootBeanDefinition -->
    <bean id="user" class="org.ibyte.thinking.in.spring.ioc.domain.User">
        <property name="id" value="1"/>
        <property name="name" value="小碼哥"/>
    </bean>

    <!-- 普通 beanDefinition GenericBeanDefinition -->
    <!-- 合併後 GenericBeanDefinition 變成 RootBeanDefinition,而且覆蓋 parent 相關配置-->
    <!-- primary = true , 增長了一個 address 屬性 -->
    <bean id="superUser" class="org.ibyte.thinking.in.spring.ioc.domain.SuperUser" parent="user"
          primary="true">

        <property name="address" value="深圳"/>
    </bean>

    <bean id="objectFactory" class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean">
        <property name="targetBeanName" value="user"/>
    </bean>

</beans>


BeanFactory 做爲IoC容器dom

/**
 * {@link BeanFactory} 做爲 IoC 容器示例
 *
 * @author lisz (shangzhi.ibyte@gmail.com)
 */

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);
    }

    private static void lookupCollectionByType(BeanFactory beanFactory) {
        if (beanFactory instanceof ListableBeanFactory) {
            ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
            Map<String, User> users = listableBeanFactory.getBeansOfType(User.class);
            System.out.println("查找到的全部的 User 集合對象:" + users);
        }
    }
}


ApplicationContext 做爲IoC容器ide

/**
 * 註解能力 {@link ApplicationContext} 做爲 IoC 容器示例
 *
 * @author lisz (shangzhi.ibyte@gmail.com)
 */

@Configuration
public class AnnotationApplicationContextAsIoCContainerDemo {

    public static void main(String[] args) {
        // 建立Bean容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 將當前類 AnnotationApplicationContextAsIoCContainerDemo 做爲配置類(Configuration Class)
        applicationContext.register(AnnotationApplicationContextAsIoCContainerDemo.class);
        // 啓動應用上下文
        applicationContext.refresh();

        // 依賴查找集合對象
        lookupCollectionByType(applicationContext);

        // 關閉應用上下文
        applicationContext.close();
    }

    /**
     * 經過 Java 註解的方式,定義了一個 Bean
     */

    @Bean
    public User user() {
        User user = new User();
        user.setId(1L);
        user.setName("小碼哥");
        return user;
    }

    private static void lookupCollectionByType(BeanFactory beanFactory) {
        if (beanFactory instanceof ListableBeanFactory) {
            ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
            Map<String, User> users = listableBeanFactory.getBeansOfType(User.class);
            System.out.println("查找到的全部的 User 集合對象:" + users);        }    }}
相關文章
相關標籤/搜索