给cc.springIoc.test.TestDI类中的属性注入值
- 如果对象的属性的值是一个对象类型,
<property name="ink" ref="gi"></property>
用ref赋值,如果是String类型或基本数据类型,用value 赋值
name是属性的名字,ref或value是属性的值 - 数组类型和List集合类型的注入方式一样
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!-- 注入不同类型的值 -->
<bean id="tdi" class="cn.cc.springIoc.test.TestDI">
<!-- XML有些字符是关键字
使用ExceptionLanguage的<![CDATA[特殊值]]>
-->
<property name="scharacter1">
<value><![CDATA[P&G]]></value>
</property>
<property name="scharacter2">
<value>P&G</value>
</property>
<!-- 对象类型 -->
<property name="printer">
<bean class="cn.cc.springIoc.printer.Printer">
</bean>
</property>
<!-- 集合对象类型 -->
<!-- List<String> -->
<property name="listStr">
<list>
<value>篮球</value>
<value>足球</value>
<value>排球</value>
</list>
</property>
<!-- List<Printer> listPrinter -->
<property name="listPrinter">
<list>
<bean id="h1" class="cn.cc.springIoc.printer.Printer"></bean>
<bean id="h2" class="cn.cc.springIoc.printer.Printer">
<property name="ink" ref="gi"></property>
<property name="paper" ref="a4"></property>
</bean>
</list>
</property>
<!-- 数组类型 String[] aryStr -->
<property name="aryStr">
<list>
<value>篮球</value>
<value>足球</value>
<value>排球</value>
</list>
</property>
<!-- 对象数组类型 Printer[] aryObj-->
<property name="aryObj">
<list>
<bean id="hp1" class="cn.cc.springIoc.printer.Printer"></bean>
<bean id="hp2" class="cn.cc.springIoc.printer.Printer">
<property name="ink" ref="gi"></property>
<property name="paper" ref="a4"></property>
</bean>
</list>
</property>
<!-- Map类型 Map<String, String> mapStr -->
<property name="mapStr">
<map>
<entry>
<key>
<value>bb</value>
</key>
<value>篮球</value>
</entry>
<entry>
<key>
<value>fb</value>
</key>
<value>足球</value>
</entry>
</map>
</property>
<!-- 值为对象的Map类型 Map<String, Printer> mapPrinter -->
<property name="mapPrinter">
<map>
<entry>
<key>
<value>c1</value>
</key>
<bean id="canon1" class="cn.cc.springIoc.printer.Printer"></bean>
</entry>
<entry>
<key>
<value>c2</value>
</key>
<bean id="canon2" class="cn.cc.springIoc.printer.Printer"></bean>
</entry>
</map>
</property>
<!-- 字符串值"" String emptyValue-->
<property name="emptyValue">
<value></value>
</property>
<!-- 字符串值null String nullValue -->
<property name="nullValue">
<value>null</value>
</property>
</bean>
</beans>
构造注入方式
applicationContext.xml
UserServiceImol.java