spring ioc:控制反轉,或叫依賴注入
簡單的bean裝配:
importimport
import
test.helloworld;
public
class
test
}
publicclass
helloworld
}
beans.xml
<?xml version="1.0" encoding="utf-8"
?>
<
beans
xmlns
=""xmlns:xsi
=""xsi:schemalocation
="/spring-beans.xsd"
>
<
bean
id="helloworld"
class
="test.helloworld"
>
bean
>
beans
>
依賴注入:
1,屬性注入;
2,建構函式注入;(通過型別;通過索引;聯合使用)
3,工廠方法注入;(非靜態工廠,靜態工廠)
4,泛型依賴注入;(spring4 整合hibernate4 的時候順帶講)
packageentity;
public
class
people
public people(int id, string name, int
age)
public
intgetid()
public
void setid(int
id)
public
string getname()
public
void
setname(string name)
public
intgetage()
public
void setage(int
age)
@override
public
string tostring()
}
packagefactory;
import
entity.people;
public
class
peoplefactory
}
packagefactory;
import
entity.people;
public
class
peoplefactory2
}
importimport
import
entity.people;
public
class
test2
}
<?xml version="1.0" encoding="utf-8"
?>
<
beans
xmlns
=""xmlns:xsi
=""xsi:schemalocation
="/spring-beans.xsd"
>
<
bean
id="people"
class
="entity.people"
>
bean
>
<
bean
id="people2"
class
="entity.people"
>
<
property
name
="id"
value
="1"
>
property
>
<
property
name
="name"
value
="張三"
>
property
>
<
property
name
="age"
value
="11"
>
property
>
bean
>
<
bean
id="people3"
class
="entity.people"
>
<
constructor-arg
type
="int"
value
="2"
>
constructor-arg
>
<
constructor-arg
type
="string"
value
="李四"
>
constructor-arg
>
<
constructor-arg
type
="int"
value
="22"
>
constructor-arg
>
bean
>
<
bean
id="people4"
class
="entity.people"
>
<
constructor-arg
index
="0"
value
="3"
>
constructor-arg
>
<
constructor-arg
index
="1"
value
="王五"
>
constructor-arg
>
<
constructor-arg
index
="2"
value
="55"
>
constructor-arg
>
bean
>
<
bean
id="people5"
class
="entity.people"
>
<
constructor-arg
index
="0"
type
="int"
value
="4"
>
constructor-arg
>
<
constructor-arg
index
="1"
type
="string"
value
="招六"
>
constructor-arg
>
<
constructor-arg
index
="2"
type
="int"
value
="66"
>
constructor-arg
>
bean
>
<
bean
id="peoplefactory"
class
="factory.peoplefactory"
>
bean
>
<
bean
id="people8"
class
="factory.peoplefactory2"
factory-method
="createpeople"
>
bean
>
beans
>
Spring Ioc 依賴注入的幾種方式
一 setter方法注入 配置檔案如下 action實現類中 private ihelloservice helloservice private string name public void sayhello public void sethelloservice ihelloservice h...
Spring IOC原理和應用 依賴注入
spring提供ioc容器,對 bean進行例項化。使用bean時候從容器中取。ioc控制反轉,將物件的建立權反轉到了spring容器中。1 把物件的建立交給spring進行管理 2 ioc操作兩部分 1 ioc配置檔案方法 2 ioc的註解方式 配置bean 1 ioc底層原理使用技術 1 xml...
Spring IOC容器和DI依賴注入
inversion of control 控制反轉容器。作用 解決物件建立以及管理問題。解析 傳統關於物件建立 user user new user 自己控制物件的建立,自己宣告變數管理物件引用。ioc 需要物件,自己不建立,交給ioc容器建立並管理,需要的時候從ioc容器中獲取即可,這種情況就叫控...