Spring框架xml配置文件 复杂类型属性注入——数组 list map properties
Posted Advancing Swift
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring框架xml配置文件 复杂类型属性注入——数组 list map properties相关的知识,希望对你有一定的参考价值。
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;
//键值对都为字符串的Map集合 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"> <array> <value>西施</value> <value>貂蝉</value> <value>王昭君</value> <value>杨玉环</value> </array> </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); } }
浏览器显示效果如下:
重新编辑上边内容
User类
注意有多个有参构造
package cn.itcast.entity; public class User { private String name; private Integer age; private Car car; public User() { super(); // TODO Auto-generated constructor stub } public User(String name, Integer age) { super(); this.name = name; this.age = age; } public User(String name, Integer age, Car car) { System.out.println("public User(String name, Integer age, Car car)"); this.name = name; this.age = age; this.car = car; } public User(Integer age,String name, Car car) { System.out.println("public User(Integer age,String name, Car car)"); this.name = name; this.age = age; this.car = car; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [name=" + name + ", age=" + age + ", car=" + car + "]"; } }
Car类
package cn.itcast.entity; public class Car { private String name; public Car() { super(); // TODO Auto-generated constructor stub } public Car(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Car [name=" + name + "]"; } }
乱七八糟集合类 (等待注入)
package cn.itcast.entity; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; public class InjectionCollectionMap { private Object[] array; private List list; private Map map; private Properties properties; public Object[] getArray() { return array; } public void setArray(Object[] array) { this.array = array; } public List getList() { return list; } public void setList(List list) { this.list = list; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "InjectionCollectionMap [array=" + Arrays.toString(array) + ", list=" + list + ", map=" + map + ", properties=" + properties + "]"; } }
分模块配置文件
<?xml version="1.0" encoding="UTF-8"?> <!-- 导入schema 约束的位置在: ..\\spring-framework-4.2.4.RELEASE\\docs\\spring-framework-reference\\html\\xsd-configuration.html 文件中。 注意:要导入schema约束 --> <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"> <!-- <bean name="user" class="cn.itcast.entity.User"> <property name="name" value="spring"></property> <property name="age" value="18"></property> <property ref="car" name="car"></property> </bean> --> <bean name="user" class="cn.itcast.entity.User"> <constructor-arg name="name" value="gaoyang" index="0" type="java.lang.String"></constructor-arg> <constructor-arg name="age" value="26" index="1" type="java.lang.Integer"></constructor-arg> <constructor-arg name="car" ref="car" index="2" ></constructor-arg> </bean> <bean name="car" class="cn.itcast.entity.Car" p:name="布加迪威航"> </bean> <bean name="injectionCollectionMap" class="cn.itcast.entity.InjectionCollectionMap"> <property name="array"> <array> <value>1</value> <value>2</value> <value>3</value> <value>4</value> <value>5</value> </array> </property> <property name="list"> <list> <value>zhangsan</value> <value>lisi</value> <value>wangwu</value> <value>zhaoliu</value> <value>tianqi</value> </list> </property> <property name="map"> <map> <entry key="name" value="gaoyang"></entry> <entry key="car" value-ref="car"></entry> </map> </property> <property name="properties"> <props> <prop key="cold">outside is raining</prop> <prop key="humid">outside is raining</prop> </props> </property> </bean> </beans>
主配置文件中引入分模块
<?xml version="1.0" encoding="UTF-8"?> <!-- 导入schema 约束的位置在: ..\\spring-framework-4.2.4.RELEASE\\docs\\spring-framework-reference\\html\\xsd-configuration.html 文件中。 注意:要导入schema约束 --> <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"> <bean name="iCustomerDao" class="cn.itcast.dao.impl.CustomerDaoImpl"></bean> <bean name="iCustomerService" class="cn.itcast.service.impl.CustomerServiceImpl"></bean> <import resource="/cn/itcast/property/property_injection.xml"/> </beans>
测试文件
package cn.itcast.property; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.entity.InjectionCollectionMap; import cn.itcast.entity.User; public class TestProperty { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); User user = (User) context.getBean("user"); InjectionCollectionMap injectionCollectionMap = (InjectionCollectionMap) context.getBean("injectionCollectionMap"); System.out.println(user); System.out.println(injectionCollectionMap); } }
以上是关于Spring框架xml配置文件 复杂类型属性注入——数组 list map properties的主要内容,如果未能解决你的问题,请参考以下文章