一、字面量
1.注入空值
<!--利用null标签将name属性设置为空值-->
<property name="name">
<null/>
</property>
2.注入属性包含特殊符号
方法一:利用转义字符
方法二:利用CDATA标签
<!--利用CDATA标签将name属性赋值为"<<南京>>"-->
<property name="name">
<value>
<![CDATA[<<南京>>]]]>
</value>
</property>
二、对象属性
调用外部bean:一个实现类内定义一个另一个实现类的对象属性,就需要在配置文件中利用属性调用那个实现类对应的bean标签的id属性
<!--dao和service对象创建-->
<bean id="userDAO" class="com.atqjb.Spring5.dao.impl.UserDAOImpl"/>
<bean id="userService" class="com.atqjb.Spring5.service.impl.UserServiceImpl">
<!--
注入userDAO属性
name属性:类里面属性名称
ref属性:创建对象对应bena标签id值
-->
<property name="userDAO" ref="userDAO"/>
</bean>
内部bena:注入内部bean也是要完成一个实现类内定义一个另一个实现类的对象属性,而与注入外部bean不同,则是将原本的外部bean写在了内部
<!--内部bean-->
<bean id="employee" class="com.atqjb.Spring5.bean.Employee">
<!--普通属性注入-->
<property name="ename" value="Jack"/>
<property name="gender" value="男"/>
<!--对象属性注入-->
<property name="department">
<bean id="department" class="com.atqjb.Spring5.bean.Department">
<property name="dname" value="技术部"/>
</bean>
</property>
</bean>
三、集合属性注入
集合属性注入需要用到对应的集合标签进行注入
<!--集合类型属性注入-->
<bean id="student" class="com.atqjb.Spring5.collectiontype.Student">
<!--数组类型属性注入-->
<property name="arr">
<array>
<value>1</value>
<value>2</value>
</array>
</property>
<!--list集合属性注入-->
<property name="list">
<list>
<value>1</value>
<value>2</value>
</list>
</property>
<!--map集合属性注入-->
<property name="map">
<map>
<entry key="0" value="1"/>
<entry key="1" value="2"/>
</map>
</property>
<!--set集合注入-->
<property name="set">
<set>
<value>1</value>
<value>2</value>
</set>
</property>
</bean>
注意点一:集合属性对象值的注入,集合标签内用ref标签bean属性注入
<!--集合中对象值注入-->
<property name="list">
<list>
<ref bean="user1"/>
<ref bean="user2"/>
</list>
</property>
注意点二:如果多个对象有共同集合属性,就可以将共同集合提取出来共不同bean引用
通过util标签进行提取集合,冒号后面不同类型对应不同集合,提取的集合通过ref属性注入
<!--beans标签需要进行以下配置
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/beans/spring-util.xsd"
-->
<!--提取list集合类型属性注入-->
<util:list id="list">
<value>1</value>
<value>2</value>
<ref bean="user"/>
</util:list>
<!--注入提取的集合-->
<bean id="collection" class="com.atqjb.Spring5.collectiontype">
<property name="list" ref="list"/>
</bean>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)