首页 > 代码库 > Spring:特殊数据类型的属性注入(基于配置文件)
Spring:特殊数据类型的属性注入(基于配置文件)
该处提到的特殊数据类型指的是除了基础数据类型和String以外的其他常用的数据类型,如:List、Map、Set、以及pojo对象等。则我们创建的Person类定义为:
package bjtu.wellhold.testSpring; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import org.springframework.stereotype.Repository; //@Repository("person") public class Person { private String name; private int age; private double money; private List<String> list; private Set<String> set; private Map<String,String> map; private String[] array; private Properties properties; private User user; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public String[] getArray() { return array; } public void setArray(String[] array) { this.array = array; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", money=" + money + ", list=" + list + ", set=" + set + ", map=" + map + ", array=" + Arrays.toString(array) + ", properties=" + properties + ", user=" + user + "]"; } }
因为还涉及到向我们的Person当中注入一个Pojo类,所以我们再定义一个Pojo类叫User:
package bjtu.wellhold.testSpring; public class User { private int age; private String username; private String password; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [age=" + age + ", username=" + username + ", password=" + password + "]"; } }
之后则需要到我们的配置文件当中去进行配置,配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 对要注入的User类进行注册和通过Setter进行属性注入 --> <bean id="user" class="bjtu.wellhold.testSpring.User"> <property name="username" value="wellhold"></property> </bean> <!-- 可以参考各个特殊数据类型是如何进行属性注入的 --> <bean id="person" class="bjtu.wellhold.testSpring.Person"> <property name="name"> <value>Tom</value> </property> <property name="age"> <value>28</value> </property> <property name="money"> <value>100.9</value> </property> <property name="list"> <list> <value>shandong</value> <value>杭州</value> <value>xihu</value> </list> </property> <property name="set"> <set> <value>13900</value> <value>13900</value> <value>13800</value> </set> </property> <property name="map"> <map> <entry> <key> <value>key1</value> </key> <value>value1</value> </entry> <entry key="key2" value="value2"> </entry> </map> </property> <property name="array"> <list> <value>xiaoh</value> <value>xiaob</value> </list> </property> <property name="properties"> <props> <prop key="pp1">pp1Value</prop> <prop key="pp2">pp2Value</prop> </props> </property> <property name="user"> <ref bean="user"/> </property> </bean> </beans>
以上是通过配置文件的形式去进行特殊数据类型的属性注入的。
本文参考http://blog.csdn.net/niuniu20008/article/details/12559797的部分,并且通过自己尝试之后,攒写该文用于记录,以便今后使用。
Spring:特殊数据类型的属性注入(基于配置文件)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。