目录
前言
本文供以下文章参考使用:
1 java类
package com.evenif.bean;
import java.util.*;
public class User {
private int id;
private String name;
private Address address;
private String[] phones;
private List<String> titleList;
private Map<String,Integer> tokens;
private Set<String> alias;
private Object achievement;
private Properties others;
public User() {
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getPhones() {
return phones;
}
public void setPhones(String[] phones) {
this.phones = phones;
}
public List<String> getTitleList() {
return titleList;
}
public void setTitleList(List<String> titleList) {
this.titleList = titleList;
}
public Set<String> getAlias() {
return alias;
}
public void setAlias(Set<String> alias) {
this.alias = alias;
}
public Object getAchievement() {
return achievement;
}
public void setAchievement(Object achievement) {
this.achievement = achievement;
}
public Properties getOthers() {
return others;
}
public void setOthers(Properties others) {
this.others = others;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", address=" + address +
", phones=" + Arrays.toString(phones) +
", titleList=" + titleList +
", tokens=" + tokens +
", alias=" + alias +
", achievement=" + achievement +
", others=" + others +
'}';
}
public Map<String, Integer> getTokens() {
return tokens;
}
public void setTokens(Map<String, Integer> tokens) {
this.tokens = tokens;
}
}
package com.evenif.bean;
public class Address {
private String province;
private String city;
private String address;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
", address='" + address + '\'' +
'}';
}
}
2 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!--
p命名空间使用需要导入:
xmlns:p="http://www.springframework.org/schema/p"
c命名空间使用需要导入:
xmlns:c="http://www.springframework.org/schema/c"
-->
<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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="addr" class="com.evenif.bean.Address">
<property name="province" value="xx省"/>
<property name="city" value="yy市"/>
<property name="address" value="zz街n号"/>
</bean>
<bean id="user" class="com.evenif.bean.User">
<!-- 1. 常量注入 -->
<property name="name" value="constName"/>
<!-- 2. bean注入 -->
<property name="address" ref="addr"/>
<!-- 3. 数组注入 -->
<property name="phones">
<array>
<value>13812345678</value>
<value>13987654321</value>
</array>
</property>
<!-- 4. List注入 -->
<property name="titleList">
<list>
<value>政治家</value>
<value>军事家</value>
</list>
</property>
<!-- 5. Map注入 -->
<property name="tokens">
<map>
<!-- 方式一(个人觉得该方式较好) -->
<entry key="A币" value="100"/>
<!-- 方式二 -->
<entry>
<key><value>C币</value></key>
<value>200</value>
</entry>
</map>
</property>
<!-- 6. Set注入,重复元素自动去重 -->
<property name="alias">
<set>
<value>aaa</value>
<value>bbb</value>
<value>bbb</value>
</set>
</property>
<!-- 7. null注入 -->
<property name="achievement"><null/></property>
<!-- 8. Properties注入 -->
<property name="others">
<props>
<prop key="sex">男</prop>
<prop key="age">18</prop>
<prop key="job">自由职业</prop>
</props>
</property>
</bean>
<!-- 9. p注入 -->
<bean id="p_user" class="com.evenif.bean.User" p:name="pName" p:id="2"/>
<!-- 10. c注入 -->
<bean id="c_user" class="com.evenif.bean.User" c:name="cName" c:id="3"/>
</beans>
3 主测试类
package com.evenif;
import com.evenif.bean.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user");
System.out.println(user);
user = (User) context.getBean("p_user");
System.out.println(user);
user = (User) context.getBean("c_user");
System.out.println(user);
}
}
//执行结果:
//User{id=0, name='constName',
// address=Address{province='xx省', city='yy市', address='zz街n号'},
// phones=[13812345678, 13987654321],
// titleList=[政治家, 军事家],
// tokens={A币=100, C币=200},
// alias=[aaa, bbb],
// achievement=null,
// others={sex=男, job=自由职业, age=18}}
//User{id=2, name='pName', address=null, phones=null, titleList=null, tokens=null, alias=null, achievement=null, others=null}
//User{id=3, name='cName', address=null, phones=null, titleList=null, tokens=null, alias=null, achievement=null, others=null}