首页 > 代码库 > Spring_DI利用set方法赋值Demo
Spring_DI利用set方法赋值Demo
Person.java
public class Person { private Long pid; private String pname; private Student student; private List list; private Set set; private Map map; private Properties properties;//get和set方法}
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 6 7 <bean name="person" class="cn.test.di.Person"> 8 <!-- 9 property就是代表属性10 在spring中基本类型(包装类型和String)都可以用value来赋值11 引用类型用ref赋值12 -->13 <property name="pid" value="5"></property>14 <property name="pname" value="张三"></property><!-- 基本数据类型 -->15 <property name="student" ref="student"></property><!-- 引用类型 -->16 <property name="list">17 <list>18 <value>list1</value>19 <value>list2</value>20 <ref bean="student"/>21 </list> 22 </property>23 24 <property name="set">25 <set>26 <value>set1</value>27 <value>set2</value>28 <ref bean="student"/>29 </set>30 </property>31 <property name="map">32 <map>33 <entry key="map1">34 <value>map1</value>35 </entry>36 <entry key="map2">37 <value>map2</value>38 </entry>39 <entry key="map3">40 <ref bean="student"/>41 </entry>42 </map>43 </property>44 <property name="properties">45 <props>46 <prop key="pro1">47 pro148 </prop>49 </props>50 </property>51 </bean>52 <bean name="student" class="cn.test.di.Student"></bean>53 </beans>
测试
1 @Test 2 public void doSome(){ 3 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("cn/test/di/applicationContext.xml"); 4 Person person= (Person) applicationContext.getBean("person"); 5 System.err.println(person.getPid()); 6 System.err.println(person.getPname()); 7 person.getStudent().say(); 8 List list=person.getList(); 9 for(int i=0;i<list.size();i++){10 System.err.println(list.get(i).toString());11 }
Spring_DI利用set方法赋值Demo
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。