This demonstrates the effect of injecting the property of a bean using a Moonlightpoet class.
Packagecom.moonlit.myspring;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Map.Entry;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importjava.util.Properties; Public classMoonlightpoet {PrivateString name; Private intAge ; PrivatePoem Poem; PrivateList<string>list; PrivateMap<string, string>map; PrivateProperties Properties; Public voidperform () {System.out.println ("Name:" +name); System.out.println ("Age:" +Age ); Poem.recite (); for(String val:list) System.out.println ("In list:" +val); for(Entry<string, string>Entry:map.entrySet ()) System.out.println ("In map:" + entry.getkey () + "--" +Entry.getvalue ()); for(Entry<object, object>Entry:properties.entrySet ()) System.out.println ("In Properties:" + entry.getkey () + "--" +Entry.getvalue ()); } Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Spring-idol.xml"); Moonlightpoet Moonlightpoet= (Moonlightpoet) context.getbean ("Moonlightpoet"); Moonlightpoet.perform (); } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicPoem Getpoem () {returnpoem; } Public voidsetpoem (Poem Poem) { This. Poem =poem; } PublicList<string>getList () {returnlist; } Public voidSetlist (list<string>list) { This. List =list; } PublicMap<string, string>Getmap () {returnmap; } Public voidSetmap (map<string, string>map) { This. Map =map; } PublicProperties getProperties () {returnproperties; } Public voidSetProperties (Properties properties) { This. Properties =properties; }}
The bean is defined in the XML file as follows:
<BeanID= "Moonlightpoet"class= "Com.moonlit.myspring.MoonlightPoet"> < Propertyname= "Name"value= "Moonlit" /> < Propertyname= "Age"value= "All" /> < Propertyname= "Poem"ref= "Sonnet29" /> < Propertyname= "List"> <List> <value>Hello</value> <value>World</value> <!--if bean, use <ref bean= "XX" > - </List> </ Property> < Propertyname= "Map"> <Map> <entryKey= "Key1"value= "Value1" /> <entryKey= "Key2"value= "value2" /> <entryKey= "Key3"value= "Value3" /> </Map> </ Property> < Propertyname= "Properties"> <Props> <propKey= "GUITAR">Strum strum strum</prop> <propKey= "Cymbal">CRASH CRASH CRASH</prop> <propKey= "Harmonica">HUM HUM HUM</prop> </Props> </ Property> </Bean>
Output Result:
Name:moonlitage:22when, in disgrace with fortune and men's eyes,i all alone beweep my outcast state,and trouble Deaf H Eaven with my bootless cries,and look upon myself, and curse my fate,wishing me like to one more rich in Hope,featur ' d lik e him, like him with friends possess ' d,desiring this man's art and that man's Scope,with what I most enjoy contented least ; Yet in these thoughts myself almost despising,haply I think on thee, and then my state,like to the lark at break of day AR Isingfrom Sullen Earth, sings Hymns at Heaven ' s gate; For thy sweet love remember ' d such wealth bringsthat then I scorn to change my state with kings.in List:helloin list:w Orldin map:key1--value1in Map:key2--value2in Map:key3--value3in Properties:harmonica--HUM HUM Humin Proper Ties:cymbal--CRASH CRASH crashin Properties:guitar--strum strum strum
Understand:
Inject Simple values:
<property name= "XX" value= "YY"/>
where xx is the variable name, yy is the value.
To reference another bean:
<property name= "XX" ref= "YY" >
where xx is the variable name, YY is the ID of the referenced bean.
You can also inject internal classes:
<property name= "XX" >
<bean class= "YY"/>
</preperty>
where xx is the variable name, YY is the class name corresponding to the inner class.
Assemble list, set, and array:
<property name= "XX" >
<value>YY</value>
Or
<ref bean= "ZZ" >
</property>
where xx is the variable name, yy is the value, and ZZ is the reference bean.
Assembly Map:
<map>
<entry key= "XX" value= "YY"/>
Or
<entry key= "XX" value-ref= "YY"/>
Or
<entry key-ref= "XX" value= "YY"/>
Or
<entry key-ref= "XX" value-ref= "YY"/>
</map>
Because the map key and value can correspond to a value of the underlying type, or to a bean, so key,value corresponds to the value, key-ref,value-ref the corresponding bean.
Assembly Properties Collection:
<property name= "XX" >
<props>
<prop key= "AA" >BB</prop>
....
</props>
</property>
Where AA corresponds to KEY,BB corresponds to value.
Assembly NULL: Use the <null/> element.
Assemble properties using Spring's namespace P
We can add in beans
xmlns:p= "Http:www.springframework.org/schema/beans"
To use P: to assemble the Bean's properties as a prefix to all attributes of the <bean> element. Use the following:
<id= "Kenny" class= "XX" = "Jingle Bells" = " saxphone"/>
The-ref suffix is used as an identifier to tell spring to assemble a reference instead of a literal value.
Spring Learning notes--injecting bean properties