7.5.8: Injection Set Value

Source: Internet
Author: User
Tags chop

If the bean property is a set, you can use the set element.<List.../>, <set.../>, <map.../>And<Props.../>Set attribute values of the list, set, map, and properties types.

Axe. Java:

public interface Axe {public String chop();}

Stoneaxe. Java:

Public class stoneaxe implements axe {@ overridepublic string chop () {return "" ;}@ overridepublic string tostring () {return "";}}

Steelaxe. Java:

Public class steelaxe implements axe {@ overridepublic string chop () {return "Steel axe cutting firewood fast" ;}@ overridepublic string tostring () {return "Steel Axe ";}}

Chinese. Java:

public class Chinese {private List<String> schools;private Map scores;private Map<String,Axe> phaseAxes;private Properties health;private Set axes;private String[] books;public Chinese() {}public void setSchools(List<String> schools) {this.schools = schools;}public void setScores(Map scores) {this.scores = scores;}public void setPhaseAxes(Map<String, Axe> phaseAxes) {this.phaseAxes = phaseAxes;}public void setHealth(Properties health) {this.health = health;}public void setAxes(Set axes) {this.axes = axes;}public void setBooks(String[] books) {this.books = books;}public void test(){System.out.println(schools);System.out.println(scores);System.out.println(phaseAxes);System.out.println(health);System.out.println(axes);System.out.println(Arrays.toString(books));}}

Bean. XML Core Configuration:

<Bean id = "Chinese" class = "com. bean. chinese "> <property name =" Schools "> <list> <value> Primary School </value> <value> secondary school </value> <value> university </value> </ list> </property> <property name = "scores"> <map> <Entry key = "Mathematics" value = "87"/> <Entry key = "English" value = "89"/> <Entry key = "" value = "82"/> </map> </property> <property name = "phaseaxes"> <map> <entry key = "primitive society" value-ref = "stoneaxe"/> <Entry key = "Agricultural Society" value-ref = "steelaxe "/> </Map> </property> <property name =" health "> <props> <prop key =" blood pressure "> normal </prop> <prop key =" height "> 175 </prop> </props> </property> <property name =" axes "> <set> <! -- Each value, bean, and ref is configured with a set element --> <value> common string </value> <Bean class = "com. bean. steelaxe "/> <ref local =" stoneaxe "/> </set> </property> <property name =" books "> <list> <value> crazy Java handouts </ value> <value> detailed description of Java Web and tomcat development technologies </value> <value> lightweight Java EE Enterprise Application practices </value> </List> </property> </bean> <bean id = "stoneaxe" class = "com. bean. stoneaxe "/> <bean id =" steelaxe "class =" com. bean. steelaxe "/>

Test. Java:

public class Test {public static void main(String[] args) {ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");Chinese chinese=(Chinese) ctx.getBean("chinese");chinese.test();}}

Because the set element can also be a basic type value, reference other beans in the container, nested beans, or set attributes, so <list... />, <key... /> and <set... /> the following elements are acceptable:Child Element:

① Value:The Set element is of the basic type or string type.

② Ref:The collection element is another bean instance in the container.

③ Bean:The Set element is a nested bean.

④ List, set, map, and props:The Set element is a set.

<Props.../>Element is used to configure properties of the properties type. The properties type is a special type, and its key and value can only be strings.

Use<Map.../>It is complex to configure the map attribute for an element. Because each element of the map set is composed of two parts: Key and value, each <entry... /> Configure a map element. The entry supports the following four attributes:

① Key:The key of a map is a basic type or string.

② Key-Ref:The map key is another bean instance in the container.

③ Value:The value of map is a basic type or string.

④ Value-Ref:The value of map is another bean instance in the container.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.