5、依赖注入
5.1、构造器注入
前面已经说过了
5.2、set方法注入
-
依赖注入:set 注入
-
依赖:Bean对象的从创建依赖于容器
-
注入:Bean所有对象的属性,由容器来注入
-
环境搭建:
public class Student
{
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
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[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
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;
}
}
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
-
第一种注入方法:value值注入
<bean id="student" class="com.an.pojo.Student" >
<!-- 第一种注入方式 value值进行注入-->
<property name="name" value="张三"></property>
</bean>
-
第二种方式:Bean注入
<!-- 第二种注入方式 Bean注入-->
<property name="address" ref="address"></property>
-
数组注入
<!-- 数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>水浒传</value>
<value>三国演义</value>
<value>西游记</value>
</array>
</property> -
List集合注入
<!-- list集合注入-->
<property name="hobbys">
<list>
<value>唱歌</value>
<value>跳舞</value>
<value>篮球</value>
</list>
</property> -
Map注入
<!-- Map注入-->
<property name="card" >
<map>
<entry key="身份证" value="111111111111111"></entry>
<entry key="卡号" value="1231231231231231"></entry>
</map>
</property> -
Set集合注入
<!-- set集合注入-->
<property name="games">
<set>
<value>LOL</value>
<value>CF</value>
</set>
</property> -
Properties注入
<!-- Properties注入-->
<property name="info">
<props>
<prop key="url">jdbc:mysql://localhost:3306</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props> -
5.3、扩展方法注入
-
P命名空间注入 直接注入值
引入P命名空间: xmlns:p="http://www.springframework.org/schema/p"
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<beans>
<bean id="user" class="com.an.pojo.User" p:name="张三" p:age="18"></bean>
</beans>
</beans> -
C命名空间注入 通过构造器进行注入 需要有有参构造器
-
引入C命名空间 xmlns:c="http://www.springframework.org/schema/c"
<bean id="user2" class="com.an.pojo.User" c:name="李四" c:age="20"></bean>