Spring——Spring依赖注入DI(各种类型的元素注入)、p命名空间、c命名空间
一、各种类型的元素注入
实体类pojo
User
package com.muhan.pojo;
import java.util.*;
public class User {
//常量
private String name;
//对象
private Teacher teacher;
//数组
private String[] books;
//list
private List list;
//Map
private Map map;
//Set
private Set set;
//null值
private String girlFriend=null;
//Properties
private Properties properties;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public String getGirlFriend() {
return girlFriend;
}
public void setGirlFriend(String girlFriend) {
this.girlFriend = girlFriend;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", teacher=" + teacher +
", books=" + Arrays.toString(books) +
", list=" + list +
", map=" + map +
", set=" + set +
", girlFriend='" + girlFriend + '\'' +
", properties=" + properties +
'}';
}
}
Teacher
package com.muhan.pojo;
public class Teacher {
private String name;
public Teacher() {
}
public Teacher(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
'}';
}
}
在我们日常开发中最常用的是property标签setter注入方式,那么我们就使用setter注入完成各种元素(常量、对象、数组、List、Map、Set、null值、Properties)的注入
<?xml version="1.0" encoding="UTF-8"?>
<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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--注册好要注入的对象teacher-->
<bean id="teacher" class="com.muhan.pojo.Teacher">
<property name="name" value="张老师"/>
</bean>
<!--注册User-->
<bean id="user" class="com.muhan.pojo.User">
<!--常量注入-->
<property name="name" value="蔡徐坤"/>
<!--Bean注入/对象注入-->
<property name="teacher" ref="teacher"/>
<!--数组注入-->
<property name="books">
<array>
<value>西游记</value>
<value>水浒传</value>
<value>红楼梦</value>
<value>三国演义</value>
</array>
</property>
<!--List注入-->
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
<value>list4</value>
</list>
</property>
<!--Map注入-->
<property name="map">
<map>
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
</map>
</property>
<!--Set注入-->
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<!--null值注入-->
<property name="girlFriend">
<null/>
</property>
<!--Properties注入-->
<property name="properties">
<props>
<prop key="学号">02165055</prop>
<prop key="姓名">张三</prop>
<prop key="班级">信管1602</prop>
</props>
</property>
</bean>
</beans>
二、c命名空间(构造器的注入)
<!--c:constructor构造器:命名空间注入-->
<bean id="user2" class="com.muhan.pojo.User" c:name="文章" c:age="19"/>
注意:
需要在头文件中配置:
xmlns:c="http://www.springframework.org/schema/c"
在xsi:schemaLocation中添加:
http://www.springframework.org/schema/c
http://www.springframework.org/schema/c/spring-c.xsd
二、p命名空间(属性的注入)
<!--c:constructor构造器:命名空间注入-->
<bean id="user3" class="com.muhan.pojo.User" p:name="马伊利" p:age="19"/>
注意:
需要在头文件中配置:
xmlns:c="http://www.springframework.org/schema/p"
在xsi:schemaLocation中添加:
http://www.springframework.org/schema/p
http://www.springframework.org/schema/p/spring-p.xsd