The various attributes in the person class are spelled as follows:
PackageCom.swift.person;Importjava.util.Arrays;Importjava.util.List;ImportJava.util.Map;Importjava.util.Properties; Public classperson {
Normal stringPrivateString name;
Array of stringsPrivatestring[] arr;
List of stringsPrivateList<string>list;
Map with key-value pairs as stringsPrivateMap<string,string>map; Public voidSetmap (map<string, string>map) { This. Map =Map }
This also has a property class for key-value pairsPrivateProperties Properties; Public voidsetName (String name) { This. Name =name; } Public voidSetarr (string[] arr) { This. arr =arr; } Public voidSetlist (list<string>list) { This. List =list; } Public voidSetProperties (Properties properties) { This. Properties =properties; } PublicString Fun () {return"Person [name=" + name + ", arr=" + arrays.tostring (arr) + ", list=" + list + ", map=" +Map+ ", properties=" + Properties + "]"; }}
Configure it in the XML configuration file as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "><!--IoC control inversion Spring injects complex type properties based on XML configuration file -<BeanID= "Person"class= "Com.swift.person.Person">< Propertyname= "Name"value= "Swift"></ Property>< Propertyname= "Arr"><List><value>Xishi</value><value>Mink Cicada</value><value>Wang Zhaojun</value><value>Yang Yuhuan</value></List></ Property>< Propertyname= "List"><List><value>Wang Jingwei</value><value>Zhou enlai</value><value>Mei lanfang</value><value>Chang</value></List></ Property>< Propertyname= "Map"><Map><entryKey= "Name"value= "Swift"></entry><entryKey= "Profession"value= "Programmer"></entry><entryKey= "City"value= "Beijing"></entry></Map></ Property>< Propertyname= "Properties"><Props><propKey= "1">A fire in the winter</prop><propKey= "2">Countercurrent into the river</prop><propKey= "3">Man when self-improvement</prop></Props></ Property></Bean></Beans>
The implementation classes called are as follows:
PackageCom.swift.servlet;Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.swift.person.Person; @WebServlet ("/person") Public classServletpersonextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; PublicServletperson () {Super(); } protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {response.setcharacterencoding ("Utf-8"); Response.setcontenttype ("Text/html;charset=utf-8"); Response.getwriter (). Append ("Served at:"). Append (Request.getcontextpath ()); ApplicationContext Context=NewClasspathxmlapplicationcontext ("Context.xml"); Person per= (person) context.getbean (' person '); String PersonInfo=Per.fun (); Response.getwriter (). append (PersonInfo); } protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response); }}
The browser displays the following effects:
Spring Framework XML configuration file Complex Type attribute injection--array list Map properties