标签:
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>
标签:
原文地址:http://www.cnblogs.com/jsnan/p/4540597.html