一、依赖注入-Dependency Injection
依赖:指bean对象的创建依赖于容器;bean对象的依赖资源。
注入:指bean对象依赖的资源由容器来设置和装配。
二、Spring的注入
- 构造器注入
- 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
hobbies
) {
this
.
hobbies
=
hobbies
;
}
<
property
name
=
"hobbies"
>
<list>
<value>羽毛球value>
<value>乒乓球value>
<value>玻璃球value>
list>
property
>
Map注入:
public
void
setCards(Map
cards
) {
this
.
cards
=
cards
;
}
<
property
name
=
"cards"
>
<map>
<entry key="中国银行" value="15602822222222222">entry>
<entry>
<key><value>建设银行value>key>
<value>6789990209833833value>
entry>
map>
property
>
Set注入:
public
void
setGames(Set
games
) {
this
.
games
=
games
;
}
<
property
name
=
"games"
>
<set>
<value>lolvalue>
<value>dotavalue>
<value>csvalue>
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="学号">2015052601prop>
<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"
>
<
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"
>
<
bean
id
=
"user"
class
=
"com.liujie.model.User"
c:name
=
"杜甫"
c:age
=
"280"
>
bean
>