Spring IOC基础小总结-2
2019-12-17 本文已影响0人
AJobs_
基于构造器的依赖注入:
a.构造参数的解析:当使用的是简单类型时,Spring不能决定值得类型,所以在没有帮助的情况下不能匹配。
比如类

就不能简单的指定构造参数的值;
b.构造参数类型匹配:当你指定了构造参数的类型属性,容器就能够匹配简单的数据类型,

c.构造参数下标:


d.构造参数参数名:

构造器注入允许您将应用程序组件实现为不可变的对象,并确保所需的依赖项不是空的。
p-namespace:
使用 p-namespace 来简化 XML配置文件的属性,例子:

idref:容器中引用Bean的id;
使用例子:

该配置同下列配置形式等价:

但是官方推荐使用第一种形式;
ref parent:
ref 使用 parent Bean:引用当前容器的父容器的bean:

父容器?怎么得到父容器嘞?
先来了解事件监听器:
listener元素必须在 filter 和 filter-mapping 元素之后;在servlet 元素之前,并且可以指定多个listner。
创建自定义的事件监听类时,需要实现合适的接口,例如:



配置父容器:

ContextLoaderListner:加载spring root 应用上下文 文档解释:


context-param元素:

在 spring web 容器中,contextConfigLocation 指的是root 上下文的地址(文件)。