spring的List<String> List<Object> Map屬性xml注入_程式碼
阿新 • • 發佈:2020-11-18
bean:
@Data public class Food { private String color; }
import lombok.Data; @Data public class Mouse { private String mouseName; private List<String> alias; private List<Food> foods; private Map<String, Food> sortFoods; }
xml:
<!-- Food bean用於給mouse注入--> <bean id="food1" class="com.readspring.bean.Food" p:color="yellow"/> <bean id="food2" class="com.readspring.bean.Food" p:color="green"/> <!-- mouse --> <bean id="mouse" class="com.readspring.bean.Mouse"> <property name="mouseName" value="Sam" /> <!-- List<String> --><property name="alias"> <list> <value>sam1</value> <value>sam2</value> </list> </property> <!-- List<Object> --> <property name="foods"> <list> <ref bean="food1" /> <ref bean="food2" /> </list> </property> <!-- Map<String,Object> --><property name="sortFoods"> <map> <entry key="1" value-ref="food1"/> <entry key="2" value-ref="food2"/> </map> </property> </bean>
測試類:
public class GetBeanTest { public static void main(String[] args) throws Exception{ ApplicationContext ac = new ClassPathXmlApplicationContext("/WEB-INF/applicationcontext.xml"); Mouse mouse = (Mouse)ac.getBean("mouse"); System.out.println(mouse); } }
結果: