先写一个address.class
package com.spring.pojo;
public class Address {
private String address;
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
在写一个student.class
package com.spring.pojo;
import java.util.*;
public class Student {
private Address address;
private String name;
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public Student() {
}
public Student(String name) {
this.name = name;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"address=" + address +
", name='" + name + '\'' +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
再创建一个beans1.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名方式-->
<bean id="address" class="com.spring.pojo.Address" p:address="山东"/>
<!--c命名方式
这两种都需要引入约束-->
<bean id="student" class="com.spring.pojo.Student" c:name="小申">
<!-- <!–第一种:普通值注入:value–>-->
<!-- <property name="name" value="小申"/>-->
<!-- 第二种,Bean注入,ref-->
<property name="address" ref="address"/>
<!-- 数组-->
<property name="books">
<array>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
<value>红楼梦</value>
</array>
</property>
<!-- List-->
<property name="hobbies">
<list>
<value>唱歌</value>
<value>游泳</value>
<value>画画</value>
</list>
</property>
<!-- Map-->
<property name="card">
<map>
<entry key="身份证" value="13213213"></entry>
<entry key="校园卡" value="1321546"></entry>
</map>
</property>
<!-- Set-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>CSGO</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<!-- Properties-->
<property name="info">
<props>
<prop key="性别">男</prop>
<prop key="password">1111111</prop>
<prop key="id">123456</prop>
</props>
</property>
</bean>
</beans>
最后创建一个TestStudent.class
import com.spring.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestStudent {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
/*
Student{
address=Address{address='山东'},
name='小申',
books=[西游记, 水浒传, 三国演义, 红楼梦],
hobbies=[唱歌, 游泳, 画画],
card={身份证=13213213, 校园卡=1321546},
games=[LOL, COC, CSGO],
wife='null',
info={性别=男, password=1111111, id=123456}}
*/
}
}