Spring 的set方法(属性)注入
UserAction类中设置属性和get、set方法。(实际上只需要set方法)
private List list = null; private Set set = null; private Map map = null; private Properties props = null; //get、set方法省略。
applicationContext.xml
<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.java
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); }
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1255528