码迷,mamicode.com
首页 > 编程语言 > 详细

spring2——IOC之Bean的装配

时间:2017-02-21 01:14:30      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:标签   技术分享   java   数据   strong   1.0   实例   mvc   port   

  spring容器对于bean的装配提供了两个接口容器分别是"ApplicationContext接口容器"和"BeanFactory接口容器",其中"BeanFactory接口容器"是spring的顶级接口容器,"ApplicationContext接口容器"继承了"BeanFactory接口容器",并且两个接口容器对于Bean的装配时机是不同的,ApplicationContext接口容器是在容器初始化阶段就对容器中所有的bean进行了装配,而BeanFactory接口容器接口容器是在容器对象调用getBean方法是才对bean进行装配的。

  另外,spring的这两种接口容器在对bean进行装配时,都是先去调用类的无参构造方法,创建一个空值对象。

  下面我们通过实例来进行说明。

  添加maven依赖。

<properties>
        <org.springframework.version>4.1.4.RELEASE</org.springframework.version>
    </properties>
    <dependencies>
        <!-- spring相关jar -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    </dependencies>

 

Student类

public class Student {

    public Student() {
        System.out.println("spring加载String类");
    }
    
}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
                    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <bean id="student" class="com.opensource.bean.Student"></bean>
    
</beans>

测试类:

public class MyTest {

    public static void main(String[] args) {
        
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-bean.xml");
        
        BeanFactory bf= new XmlBeanFactory(new ClassPathResource("spring-bean.xml"));
        bf.getBean("student");

    }

}

我们分别来看一下两个容器对于创建Student类的实例时的日志:

(1)ApplicationContext接口容器

技术分享

(2) BeanFactory接口容器,只有在调用getBean("student")方法时才会调用Student的无参构造方法,在容器中创建Studnet的实例。

技术分享

调用getBean("student");

技术分享

  在这里,我们可以狭义的把spring的配置文件看作是spring容器的显式表现,因为在配置文件中我们可以看到容器对那些bean进行了装配。从容器的顶级接口BeanFactory我们不难看出,spring容器对于bean的创建使用了工厂模式,从<bean id="student" class="com.opensource.bean.Student"></bean>标签的class属性使用了全类名,我们也可以知道对于实例的创建使用了java的反射机制。id属性具有唯一性,我们从容器中取得实例使用了getBean("student"),也可以看出来spring容器对于本的装配使用的数据结构应该是Map集合的数据形式。

  最后说一点,我们作为程序员,研究问题还是要仔细深入一点的。当你对原理了解的有够透彻,开发起来也就得心应手了,很多开发中的问题和疑惑也就迎刃而解了,而且在面对其他问题的时候也可做到触类旁通。当然在开发中没有太多的时间让你去研究原理,开发中要以实现功能为前提,可等项目上线的后,你有大把的时间或者空余的时间,你大可去刨根问底,深入的去研究一项技术,为觉得这对一名程序员的成长是很重要的事情。

 

spring2——IOC之Bean的装配

标签:标签   技术分享   java   数据   strong   1.0   实例   mvc   port   

原文地址:http://www.cnblogs.com/cdf-opensource-007/p/6422113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!