Spring 的set方法(屬性)注入java
UserAction類中設置屬性和get、set方法。(實際上只須要set方法)spring
private List list = null; private Set set = null; private Map map = null; private Properties props = null; //get、set方法省略。
applicationContext.xmlapp
<bean id="userAction" class="com.umgsai.spring.UserAction" scope="singleton"> <property name="manager"> <ref bean="userManager"> </property> <property name="list"> <list> <value>123</value> <ref local="cur"> </list> </property> <property name="set"> <set> <value>Hello</value> <value>12.36</value> <value>124</value> <value type="java.lang.String">true</value><!--指定type--> </set> </property> <property name="map"> <map> <entry key="a1"> <value>umgsai</value> </entry> <entry key="a2"> <value>umgsai2</value> </entry> </map> </property> <property name="props"> <props> <prop key="x1">as</prop> <prop key="x2">1265.3</prop> </props> </property> </bean>
MainClass.javaide
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); UserAction ua = (UserAction)factory.getBean("userAction"); for(Object o : ua.getList()){ System.out.println(o); } for(Object o : ua.getSet()){ System.out.println(o); } for(Iterator iter = ua.getMap().entrySet.iterator();iter.hasNext();){ Entry entry = (Entry)iter.next(); System.out.println(entry.getKey()+":"+entry.getValue()s); }