spring的Bean注入和P标签使用

半腔热情 提交于 2020-02-13 08:14:05

1.构造方法参数 对应 配置文件 <constructor-arg> 元素

可以index|name|type 三选一 、三选二  ; ref|value 二选一

2. setter方法注入(开发推荐)

为需要注入属性提供setter方法

配置 每个注入属性, 对应<property> 元素

3. p名称空间的使用

spring2.5以后,为了简化setter方法属性注入,引用p名称空间的概念,可以将<property> 子元素,简化为<bean>元素属性配置 !!

a.

在applicationContext.xml 引入p 名称空间

<beans xmlns="http://www.springframework.org/schema/beans"

          xmlns:p="http://www.springframework.org/schema/p"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

b.  简化注入的配置

4. 集合类型的属性注入(了解)

Spring 对每个常用集合对象,提供单独元素完成注入

       List 对象 ---- <list> 元素

       Set 对象 ---- <set> 元素

       Map对象 ----<map>元素

       Properties 对象 ---- <props> 元素

集合属性的注入,主要用于框架的配置 !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!