标签:setname on() oca etl xml配置 .class utf-8 new util
Person类中的各种属性写法如下:
package com.swift.person; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; public class Person {
//普通字符串 private String name;
//字符串数组 private String[] arr;
//字符串列表 private List<String> list;
//键值对都为字符串的地图 private Map<String,String> map; public void setMap(Map<String, String> map) { this.map = map; }
//这还有一个键值对的属性类 private Properties properties; public void setName(String name) { this.name = name; } public void setArr(String[] arr) { this.arr = arr; } public void setList(List<String> list) { this.list = list; } public void setProperties(Properties properties) { this.properties = properties; } public String fun() { return "Person [name=" + name + ", arr=" + Arrays.toString(arr) + ", list=" + list + ", map=" + map + ", properties=" + properties + "]"; } }
在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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- IoC 控制反转 Spring根据XML配置文件注入复杂类型属性 --> <bean id="person" class="com.swift.person.Person"> <property name="name" value="swift"></property> <property name="arr"> <list> <value>西施</value> <value>貂蝉</value> <value>王昭君</value> <value>杨玉环</value> </list> </property> <property name="list"> <list> <value>汪精卫</value> <value>周恩来</value> <value>梅兰芳</value> <value>张学良</value> </list> </property> <property name="map"> <map> <entry key="name" value="swift"></entry> <entry key="profession" value="programmer"></entry> <entry key="city" value="beijing"></entry> </map> </property> <property name="properties"> <props> <prop key="1">冬天里的一把火</prop> <prop key="2">逆流成河</prop> <prop key="3">男儿当自强</prop> </props> </property> </bean> </beans>
调用的实现类如下:
package com.swift.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.swift.person.Person; @WebServlet("/person") public class ServletPerson extends HttpServlet { private static final long serialVersionUID = 1L; public ServletPerson() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); response.getWriter().append("Served at: ").append(request.getContextPath()); ApplicationContext context=new ClassPathXmlApplicationContext("context.xml"); Person per=(Person) context.getBean("person"); String personInfo=per.fun(); response.getWriter().append(personInfo); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
浏览器显示效果如下:
Spring框架xml配置文件 复杂类型属性注入——数组 list map properties
标签:setname on() oca etl xml配置 .class utf-8 new util
原文地址:http://www.cnblogs.com/qingyundian/p/7843504.html