一、依赖注入-Dependency Injection
依赖:指bean对象的创建依赖于容器;bean对象的依赖资源。
注入:指bean对象依赖的资源由容器来设置和装配。
二、Spring的注入
- 构造器注入
见“第二讲 IoC创建对象的三种方式”。 |
- setter注入(重点)
要求被注入的属性
必须有setter方法。setter方法的方法名由:set+属性名首字母大写。如果属性是boolean类型,返回没有getter方法是
is。
|
常量注入:
public
class
Student {
private
String
name
;
public
void
setName(String
name
) {
this
.
name
=
name
;
}
public
void
show() {
System.
out
.println(
"name="
+
name
);
}
}
<
bean
id
=
"student"
class
=
"com.liujie.model.Student"
>
<property name="name" value="张三"></property>
</
bean
>
bean注入(对象注入):
public
void
setAddress(Address
address
) {
this
.
address
=
address
;
}
<
bean
id
=
"address"
class
=
"com.liujie.model.Address"
>
<
property
name
=
"address"
value
=
"北京"
></
property
>
</
bean
>
<
bean
id
=
"student"
class
=
"com.liujie.model.Student"
>
<
property
name
=
"name"
value
=
"张三"
></
property
>
<
property
name
=
"address"
ref="address"
></
property
>
</
bean
>
数组注入:
public
void
setBooks(String[]
books
) {
this
.
books
=
books
;
}
<
bean
id
=
"student"
class
=
"com.liujie.model.Student"
>
<
property
name
=
"name"
value
=
"张三"
></
property
>
<
property
name
=
"address"
ref
=
"address"
></
property
>
<
property
name
=
"books"
>
<array>
<value>语文</value>
<value>英语</value>
<value>数学</value>
</array>
</
property
>
</
bean
>
List注入:
public
void
setHobbies(List<String>
hobbies
) {
this
.
hobbies
=
hobbies
;
}
<
property
name
=
"hobbies"
>
<list>
<value>羽毛球</value>
<value>乒乓球</value>
<value>玻璃球</value>
</list>
</
property
>
Map注入:
public
void
setCards(Map<String, String>
cards
) {
this
.
cards
=
cards
;
}
<
property
name
=
"cards"
>
<map>
<entry key="中国银行" value="15602822222222222"></entry>
<entry>
<key><value>建设银行</value></key>
<value>6789990209833833</value>
</entry>
</map>
</
property
>
Set注入:
public
void
setGames(Set<String>
games
) {
this
.
games
=
games
;
}
<
property
name
=
"games"
>
<set>
<value>lol</value>
<value>dota</value>
<value>cs</value>
</set>
</
property
>
Null注入:
public
void
setWife(String
wife
) {
this
.
wife
=
wife
;
}
<
property
name
=
"wife"
>
<null></null>
</
property
>
Properties注入:
public
void
setInfo(Properties
info
) {
this
.
info
=
info
;
}
<
property
name
=
"info"
>
<props>
<prop key="学号">2015052601</prop>
<prop key="sex">男</prop>
<prop key="name">小明</prop>
</props>
</
property
>
p命名空间注入:
public
class
User {
private
String
name
;
private
int
age
;
public
String getName() {
return
name
;
}
public
void
setName(String
name
) {
this
.
name
=
name
;
}
public
int
getAge() {
return
age
;
}
public
void
setAge(
int age
) {
this
.
age
=
age
;
}
@Override
public
String toString() {
return
"User [name="
+
name
+
", age="
+
age
+
"]"
;
}
}
<
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"
>
<!-- p命名空间注入,属性依然要设置setter方法 -->
<
bean
id
=
"user"
class
=
"com.liujie.model.User"
p:name="李白"
p:age="230"
></
bean
>
c命名空间注入:
public
class
User {
private
String
name
;
private
int
age
;
public
User() {
super
();
}
public
User(String
name, int age
) {
super
();
this
.
name
=
name
;
this
.
age
=
age
;
}
@Override
public
String toString() {
return
"User [name="
+
name
+
", age="
+
age
+
"]"
;
}
}
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
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"
>
<!-- c命名空间注入要求有对应参数的构造方法 -->
<
bean
id
=
"user"
class
=
"com.liujie.model.User"
c:name
=
"杜甫"
c:age
=
"280"
></
bean
>