赞
踩
目录
- <bean id="hello" class="com.lys.pojo.Hello">
- <property name="str" value="Spring"/>
- </bean>
- <!--第一种-->
- <beans>
- <bean id="beanOne" class="x.y.ThingOne">
- <constructor-arg name="bean" ref="beanTwo"/>
- <constructor-arg name="name" value="value"/>
- </bean>
- </beans>
-
- <!--第二种,通过类型创建,type表示参数类型-->
- <bean id="exampleBean" class="examples.ExampleBean">
- <constructor-arg type="int" value="7500000"/>
- <constructor-arg type="java.lang.String" value="42"/>
- </bean>
-
- <!--第三种,通过下标赋值创建,index=0表示第一个参数,1为第二个参数,依次类推-->
- <bean id="exampleBean" class="examples.ExampleBean">
- <constructor-arg index="0" value="7500000"/>
- <constructor-arg index="1" value="42"/>
- </bean>

- <!--第一种,普通值注入,value-->
- <property name="name" value="lys"/>
-
- <!--第二种,Bean注入-->
- <property name="address" ref="Address"/>
-
- <!--数组注入,ref-->
- <property name="books">
- <array>
- <value>红楼梦</value>
- <value>西游记</value>
- <value>水浒传</value>
- <value>三国演义</value>
- </array>
- </property>
-
- <!--List注入-->
- <property name="hobbys">
- <list>
- <value>听歌</value>
- <value>敲代码</value>
- <value>看电影</value>
- </list>
- </property>
-
- <!--Map注入-->
- <property name="card">
- <map>
- <entry key="身份证" value="1381516546"/>
- <entry key="银行卡" value="13814545874546"/>
- </map>
- </property>
-
- <!--Set注入-->
- <property name="games">
- <set>
- <value>LOL</value>
- <value>COC</value>
- <value>BOB</value>
- </set>
- </property>
-
- <!--null注入-->
- <property name="wife">
- <null/>
- </property>
-
- <!--Properties注入-->
- <property name="info">
- <props>
- <prop key="driver">21564564654</prop>
- <prop key="url">adadad</prop>
- <prop key="username">小明</prop>
- <prop key="password">dsad4454</prop>
- </props>
- </property>

- <!--p命名空间注入,可以直接注入属性的值-->
- <bean id="user" class="com.lys.pojo.User" p:name="lys" p:age="18"/>
- <!--c命名空间注入,通过构造器注入:constructor-args;bean里必须有有参构造参数-->
- <bean id="user2" class="com.lys.pojo.User" c:age="18" c:name="lys"/>
PS:p命名空间和c命名空间不能直接使用,需要导入xml约束
p命名空间约束
c命名空间约束
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。