参数下标赋值
<bean id="user" class="cn.kuang.pojo.User">
<constructor-arg index="0" value="zheng"/>
<constructor-arg index="1" value="13"/>
通过参数类型赋值(不推荐使用)
<bean id="user" class="cn.kuang.pojo.User">
<constructor-arg type="java.lang.String" value="郑智超"/>
<constructor-arg type="int" value="32"/>
通过参数名来赋值
<bean id="user" class="cn.kuang.pojo.User">
<constructor-arg name="name" value="詹青云" />
<constructor-arg name="age" value="29"/></bean>
依赖注入:set注入!
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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="address" class="com.kuang.pojo.Address">
<property name="address" value="绍兴"/>
</bean>
<bean id="student" class="com.kuang.pojo.Student">
<!--第一种,普通值注入-->
<property name="name" value="zheng"/>
<!--第二种,引用值注入-->
<property name="address" ref="address"/>
<!--第三种,集合注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>资治通鉴</value>
<value>三国</value>
<value>活着</value>
</array>
</property>
<!--第四种,List注入-->
<property name="hobbies">
<list>
<value>听音乐</value>
<value>敲代码</value>
</list>
</property>
<!--第五种,map注入-->
<property name="cards">
<map>
<entry value="身份证" key="303255555555"/>
<entry value="银行卡" key="248975723497332978"/>
</map>
</property>
<!--第六种,properties注入-->
<property name="info">
<props>
<prop key="url">百度</prop>
<prop key="username">root</prop>
<prop key="password">234234</prop>
</props>
</property>
<!--第七种,set集合注入-->
<property name="game">
<set>
<value>王者荣耀</value>
<value>天天象棋</value>
</set>
</property>
<!--第八种,null值注入-->
<property name="wife" >
<value>null</value>
</property>
</bean>
</beans>
我们可以使用p命名空间和c命名空间进行注入
官方解释:
注意事项:p命名和c命名空间不能直接使用,需要导入约束。
原文:https://www.cnblogs.com/tianxintang/p/13263653.html