码迷,mamicode.com
首页 > 编程语言 > 详细

Spring-DI

时间:2015-05-30 17:58:05      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

1.用构造器注入
1>通过参数的顺序
<bean id="student" class="com.sn.domain.Student"></bean> <bean id="person" class="com.sn.domain.Person"> <constructor-arg index="0" value="bbbcccc"></constructor-arg> <constructor-arg index="1" ref="student"></constructor-arg> </bean> 2>通过参数的类型 <bean id="student" class="com.sn.domain.Student"></bean> <bean id="person" class="com.sn.domain.Person"> <constructor-arg type="java.lang.String"> <value>dddddddddd</value> </constructor-arg> <constructor-arg type="com.sn.domain.Student"> <ref bean="student"/> </constructor-arg> </bean>
2.使用属性setting方法进行注入
    
  2.1实体类中生成getter和setter方法
  2.2 applicationContext.xml中配置
  <bean id="person" class="com.sn.Person"> <property name="pid" value="1"></property> <property name="name" value="王二麻子"></property> <property name="student" ref="student"></property> <property name="lists"> <list> <value>list1</value> <value>list2</value> <ref bean="student"/> </list> </property> <property name="sets"> <set> <value>set1</value> <value>set2</value> <ref bean="student"/> </set> </property> <property name="map"> <map> <entry key="entry1"> <value>map1</value> </entry> <entry key="entry2"> <ref bean="student"/> </entry> </map> </property> <property name="properties"> <props> <!-- 不需要引用类型 --> <prop key="prop1">prop1</prop> <prop key="prop2">prop2</prop> </props> </property> <property name="objects"> <list> <value>aa</value> <value>bb</value> </list> </property> </bean>

 

Spring-DI

标签:

原文地址:http://www.cnblogs.com/jsnan/p/4540597.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!