Spring中List、Set、Map、陣列注入方式中標籤的使用
阿新 • • 發佈:2018-12-30
在這裡不多說,直接進入正題,在使用Spring時,如果物件例項含有資料或集合屬性時,那我們該如何去配置Spring.xml呢?我們就需要property下的子元素list,set,map等子元素。示例為:
<bean>
<property>
<list>……</list>
or
<set>……</set>
or
<map>……</map>
</property>
</bean>
操作步驟:
1、 建立Topic物件。
public class Topic { /**內容 必須提供 getter 與 setter 方法*/ public String context; public String getContext() { return context; } public void setContext(String context) { this.context = context; } /** * 有參的建構函式 ,可選 * @param context */ public Topic(String context) { this.context = context; } /** * 無引數的建構函式 , 必須提供一個無參的建構函式 */ public Topic() { }
2、 建立Speaker物件。
public class Speaker { private List<Topic> topicLists; private Set<Topic> topicSets; private Map<Integer, Topic> topicMaps; private Topic[] topics; private String[] topicStrings; /** * 演講 */ public void teach() { System.out.println("====List===="); for(Topic topic:topicLists){ System.out.println(topic.getContext()); } System.out.println("====Set===="); for(Topic topic:topicSets){ System.out.println(topic.getContext()); } System.out.println("====Topic[]===="); for(Topic topic:topics){ System.out.println(topic.getContext()); } System.out.println("====Map===="); for(Entry<Integer, Topic> entry:topicMaps.entrySet()){ System.out.println(topicMaps.get(entry.getKey()).getContext()); } System.out.println(topicStrings[0]+"==="+topicStrings[1]); } /*Getter 和 Setter*/ public List<Topic> getTopicLists() { return topicLists; } public void setTopicLists(List<Topic> topicLists) { this.topicLists = topicLists; } public Set<Topic> getTopicSets() { return topicSets; } public void setTopicSets(Set<Topic> topicSets) { this.topicSets = topicSets; } public Map<Integer, Topic> getTopicMaps() { return topicMaps; } public void setTopicMaps(Map<Integer, Topic> topicMaps) { this.topicMaps = topicMaps; } public Topic[] getTopics() { return topics; } public void setTopics(Topic[] topics) { this.topics = topics; } public String[] getTopicStrings() { return topicStrings; } public void setTopicStrings(String[] topicStrings) { this.topicStrings = topicStrings; } }
3、建立Spring配置檔案highLearn02.xml。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Spring中陣列,List,Map,Set注入方式 --> <bean id="highSpeaker02" class="com.mahaochen.spring.high.learn02.Speaker" scope="singleton" > <!-- List注入方式 --> <property name="topicLists"> <list> <ref bean="highTopic01" /> <ref bean="highTopic02" /> </list> </property> <!-- Set注入方式 --> <property name="topicSets"> <set> <ref bean="highTopic01" /> <ref bean="highTopic02" /> </set> </property> <!-- Map注入方式 --> <property name="topicMaps"> <map> <entry key="1"> <ref bean="highTopic01" /> </entry> <entry key="2"> <ref bean="highTopic02" /> </entry> </map> </property> <!-- 陣列注入方式 --> <property name="topics"> <list> <ref bean="highTopic01" /> <ref bean="highTopic02" /> </list> </property> <property name="topicStrings"> <list> <value>Happy</value> <value>Smell</value> </list> </property> </bean> <bean id="highTopic01" class="com.mahaochen.spring.high.learn02.Topic"> <constructor-arg index="0" value="Left" /> </bean> <bean id="highTopic02" class="com.mahaochen.spring.high.learn02.Topic"> <constructor-arg index="0" value="Right" /> </bean> </beans>
4、將Spring配置檔案highLearn02.xml引入到主配置檔案beans.xml中。
<import resource="com/mahaochen/spring/learn03/beanLearn03.xml"/>
5、編寫測試類TestSpringHigh02.java。
public class TestSpringHigh02 {
// Spring中List、Set、Map、陣列注入方式
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("beans.xml");
Speaker speakerHigh02 = (Speaker) beanFactory.getBean("highSpeaker02");
speakerHigh02.teach();
}
}