首先创建Student类
student.java
Test.java
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Set<String> games;
private Map<String,Long> cards;
private String wife;
private Properties info;
public void show() {
System.out.println
("Student's name = " + name + ",address = " + address.getAddress());
System.out.print("books = ");
for(String string:books) {
System.out.print(string + " ");
}
System.out.println();
System.out.println("hobbies = " + hobbies);
System.out.println("games = " + games);
System.out.println("bank cards = " + cards);
System.out.println("wife = " + wife);
System.out.println("info = " + info);
}
public void setName(String name) {
this.name = name;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void setGames(Set<String> games) {
this.games = games;
}
public void setCards(Map<String, Long> cards) {
this.cards = cards;
}
public void setWife(String wife) {
this.wife = wife;
}
public void setInfo(Properties info) {
this.info = info;
}
}
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("Beans.xml");
Student student = (Student) ac.getBean("student");
student.show();
}
}
1.常量注入
Beans.xml
<bean id="student" class="com.duxd.vo.Student">
<!-- 常量注入 -->
<property name="name" value="名字"/>
</bean>
2.类注入
Address.java
Beans.xml
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
<bean id="address" class="com.duxd.vo.Address">
<property name="address" value="上海"/>
</bean>
<bean id="student" class="com.duxd.vo.Student">
<!-- 类注入 -->
<property name="address" ref="address"/>
</bean>
3.数组注入
Beans.xml
<bean id="student" class="com.duxd.vo.Student">
<!-- 数组注入 -->
<property name="books">
<array>
<value>傲慢与偏见</value>
<value>基督山伯爵</value>
<value>钢铁是怎样炼成的</value>
</array>
</property>
</bean>
4.List注入
Beans.xml
<bean id="student" class="com.duxd.vo.Student">
<!-- list注入 -->
<property name="hobbies">
<list>
<value>看书</value>
<value>学习</value>
<value>上网</value>
<value>写作</value>
</list>
</property>
</bean>
5.Set注入
Beans.xml
<bean id="student" class="com.duxd.vo.Student">
<!-- Set注入 -->
<property name="games">
<set>
<value>使命召唤</value>
<value>少女卷轴</value>
<value>我的世界</value>
</set>
</property>
</bean>
6.Map注入
Beans.xml
<bean id="student" class="com.duxd.vo.Student">
<!-- Map注入 -->
<property name="cards">
<map>
<entry key="招商银行" value="6462131654486413213"/>
<entry>
<key><value>工商银行</value></key>
<value>132164612231684684</value>
</entry>
</map>
</property>
</bean>
7.Null注入
Beans.xml
<bean id="student" class="com.duxd.vo.Student">
<!-- Null注入 -->
<property name="wife">
<null/>
</property>
</bean>
8.Properties注入
Beans.xml
<bean id="student" class="com.duxd.vo.Student">
<!-- Properties注入 -->
<property name="info">
<props>
<prop key="学号">2017000001</prop>
<prop key="sex">男</prop>
<prop key="name">名字</prop>
</props>
</property>
</bean>
9.结果
Student's name = 名字,address = 上海
books = 傲慢与偏见 基督山伯爵 钢铁是怎样炼成的
hobbies = [看书, 学习, 上网, 写作]
games = [使命召唤, 少女卷轴, 我的世界]
bank cards = {招商银行=6462131654486413213, 工商银行=132164612231684684}
wife = null
info = {学号=2017000001, name=名字, sex=男}
10.P命名空间注入
xmlns:p="http://www.springframework.org/schema/p"
User.java 需要Set方法
public class User {
private String name;
private int age;
public void show() {
System.out.println("name = " + name + ", age = " + age);
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
Test.java
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("Beans.xml");
User user = (User) ac.getBean("user");
user.show();
}
}
Beans.xml
<!-- P命名空间注入需要set方法 -->
<bean id="user" class="com.duxd.vo.User" p:name="名字" p:age="20"/>
结果
User.java 需要带参构造方法
name = 名字, age = 20
11.C命名空间注入
xmlns:c="http://www.springframework.org/schema/c"
public class User {
private String name;
private int age;
public User(){
super();
}
public User(String name, int age) {
this();
this.name = name;
this.age = age;
}
public void show() {
System.out.println("name = " + name + ", age = " + age);
}
}
Test.java
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("Beans.xml");
User user = (User) ac.getBean("user");
user.show();
}
}
Beans.xml
<!-- C命名空间注入需要带参构造方法 -->
<bean id="user" class="com.duxd.vo.User" c:name="名字" c:age="20"/>
结果
name = 名字, age = 20