依赖注入(Dependency Injection,DI)。
依赖 : 指Bean对象的创建依赖于容器,Bean对象的依赖资源。
注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配。
要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型, 没有set方法 , 则为 is .
pojo文件下构建实体类
Address.java
package com.lanyy.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
Student.java
package com.lanyy.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
// 数组
private String[] books;
// List集合
private List<String> hobbies;
// 键值对
private Map<String,String> card;
// Set集合
private Set<String> games;
private String wife;
private Properties info;
// public Student(String name) {
// this.name = name;
// }
//
// public Student() {
//
// }
public String getName() {
return name;
}
// setter注入
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
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 String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
注:这里的值是一个引用,ref
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.lanyy.pojo.Address">
<property name="address" value="北京"/>
bean>
<bean id="student" class="com.lanyy.pojo.Student">
<property name="name" value="kyh"/>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>西游记value>
<value>水浒传value>
<value>红楼梦value>
array>
property>
<property name="hobbies">
<list>
<value>lanyyvalue>
<value>kkkkkvalue>
<value>lan66value>
list>
property>
<property name="card">
<map>
<entry key="kang" value="123456789"/>
<entry key="lanyy" value="99999999"/>
<entry key="kkkkk" value="88888888"/>
map>
property>
<property name="games">
<set>
<value>CFvalue>
<value>CSvalue>
<value>LOLvalue>
set>
property>
<property name="wife">
<null/>
property>
<property name="info">
<props>
<prop key="cards">19311080123prop>
<prop key="name">懒羊羊prop>
<prop key="sex">男prop>
props>
property>
bean>
beans>
Test类编写
import com.lanyy.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
// 只打印 Student实体类中getName()下的name值
// System.out.println(student.getName());
System.out.println(student.toString());
/*
* Student{name='kyh', address=Address{address='北京'},
* books=[西游记, 水浒传, 红楼梦], hobbies=[lanyy, kkkkk, lan66],
* card={kang=123456789, lanyy=99999999, kkkkk=88888888},
* games=[CF, CS, LOL], wife='null',
* info={name=懒羊羊, sex=男, cards=19311080123}}
* */
}
}
hobbies=[lanyy, kkkkk, lan66],
* card={kang=123456789, lanyy=99999999, kkkkk=88888888},
* games=[CF, CS, LOL], wife='null',
* info={name=懒羊羊, sex=男, cards=19311080123}}
* */
}
}

了解更多知识请戳下: