Spring依赖注入 DI(二)

3.p命名空间注入

使用p命名空間注入,先在約束上面引入p標籤(紅色部分)

<?xml version="1.0" encoding="UTF-8"?>

<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">

        

  <!-- 配置部门 -->

  <bean id="department" class="cn.zj.spring.pojo.Department"

  p:id="123"

  p:name="销售部"/>

 

  <!-- 配置员工 -->

  <bean id="emp" class="cn.zj.spring.pojo.Employee"

  p:id="111"

  p:name="西门吹雪"

  p:dept-ref="department"/>

</beans>

4.  集合类型值注入

在處理的數據中

有標量類型=基礎數據類型以及包裝類+String       ----value屬性

也有Spring容器裡面的對象 ----ref屬性

還要很多數據JDK內置的數據結構:

1.鍵值對Map、Properties

2.數組

3.集合Set,List

获得properties文件的值

Spring配置文件支持通過xxx.properties文件的Key獲得對應的值。實現該功能是通過

通過${Key}來獲得properties文件對應key的值

使用spring讀取配置文件必須導入新的命名空間context

導入命名空間方法

將命名空間和約束重新拷貝一份,將對於的全部換成context,然後關聯context本地schema約束

猜你喜欢

转载自www.cnblogs.com/weishenme/p/11028763.html