Spring学习-第2天

来源:互联网 发布:淘宝模板免费的好吗 编辑:程序博客网 时间:2024/05/16 03:24

一、Spring模块示意图


二、IOC和DI的定义

1.IOC(Inversion of Control):其思想是反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源. 作为回应, 容器适时的返回资源. 而应用了 IOC 之后, 则是容器主动地将资源推送给它所管理的组件, 组件所要做的仅是选择一种合适的方式来接受资源. 这种行为也被称为查找的被动形式


2.DI(Dependency Injection) — IOC 的另一种表述方式:即组件以一些预先定义好的方式(例如: setter 方法)接受来自如容器的资源注入. 相对于 IOC 而言,这种表述更直接。


三、IOC --- 采用反转控制图示



四、ApplicationContext 的主要实现类图示



五、Spring的3种依赖注入的方式

1.属性注入
2.构造器注入
3.工厂方法注入(很少使用,不推荐)


以下两种方式,效果是一样的,最后得到的结果也是一样的。

属性注入方法的实例:

<bean id="studentinfo" class="com.red.spring.beans.StudentInfo">
<property name="id" value="1" />
<property name="name" value="zhangsan" />
<property name="age" value="22" />
</bean>


构造方法注入的实例:

<bean id="studentinfo" class="com.red.spring.beans.StudentInfo">
<constructor-arg value="1" index="0"></constructor-arg>
<constructor-arg value="zhangsan" index="1"></constructor-arg>
<constructor-arg value="22" index="2"></constructor-arg>
</bean>


六、Spring核心组件的关系图


0 0
原创粉丝点击