spring中IOC(依赖注入)详解

Spring Bean 的属性注入:

【构造方法的方式注入属性】

<bean id="car" class="cn.itcast.spring.demo4.Car">

<constructor-arg name="name" value="保时捷"/>

<constructor-arg name="price" value="1000000"/>

<bean>

set 方法的方式注入属性】

<bean id="car2" class="cn.itcast.spring.demo4.Car2">

<property name="name" value="奇瑞 QQ"/>

<property name="price" value="40000"/>

<bean>

Spring 的属性注入:对象类型的注入:

<bean id="person" class="cn.itcast.spring.demo4.Person">

<property name="name" value="会希"/>

<property name="car2" ref="car2"/>

<bean>

名称空间 p 的属性注入的方式:Spring2.x 版本后提供的方式.

第一步:引入 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">

第二步:使用 p 名称空间.

* 普通属性: p:属性名称=””

* 对象类型属性: p:属性名称-ref=””

<bean id="car2" class="cn.itcast.spring.demo4.Car2" p:name=" 宝 马 7"

p:price="1200000"/>

p:name=" 思 聪 "

p:car2-ref="car2"/>

1.2.6.5 SpEL 的方式的属性注入:Spring3.x 版本后提供的方式.

SpELSpring Expression Language.

语法:#{ SpEL }

<bean id="car2" class="cn.itcast.spring.demo4.Car2">

<property name="name" value="#{'奔驰'}"/>

<property name="price" value="#{800000}"/>

bean>

<bean id="person" class="cn.itcast.spring.demo4.Person">

<property name="name" value="#{'冠希'}"/>

<property name="car2" value="#{car2}"/>

bean>

<bean id="carInfo" class="cn.itcast.spring.demo4.CarInfo">bean>

引用了另一个类的属性

<bean id="car2" class="cn.itcast.spring.demo4.Car2">

 

 

bean>

1.2.6.6 注入复杂类型:

<bean id="collectionBean" class="cn.itcast.spring.demo5.CollectionBean">

<property name="arrs">

<list>

<value>会希value>

<value>冠希value>

<value>天一value>

list>传智播客——专注于 Java.Net Php、网页平面设计工程师的培训

property>

<property name="list">

<list>

<value>芙蓉value>

<value>如花value>

<value>凤姐value>

list>

property>

<property name="map">

<map>

<entry key="aaa" value="111"/>

<entry key="bbb" value="222"/>

<entry key="ccc" value="333"/>

map>

property>

<property name="properties">

<props>

<prop key="username">rootprop>

<prop key="password">123prop>

props>

property>

bean>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值