ioc(inversion if control)-控制反轉(zhuǎn)是spring倆大核心技術(shù)之一,ioc一般分為倆種類型:依賴注入(dependency injection,簡稱di)和依賴查找(dependency lookup)
使用示例:
1、新建工程并導(dǎo)入spring相關(guān)jar包。
2、新建數(shù)據(jù)訪問層及業(yè)務(wù)邏輯層
代碼結(jié)構(gòu):
代碼示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
/** * 實(shí)體bean * @author bc * */ public class user { private integer id; private string username; private string password; //get set方法略 } /** * 數(shù)據(jù)訪問層接口 * @author bc * */ public interface userdaointerface { /**查詢所有用戶信息*/ public list<user> getuserlist(); } /** * 數(shù)據(jù)訪問層實(shí)現(xiàn)類 * @author bc * */ public class userdaoimpl implements userdaointerface { /**模擬數(shù)據(jù)庫數(shù)據(jù)*/ private list<user> userlist; public userdaoimpl() { userlist = new arraylist<user>(); user u = new user( 1 , "張三" , "123" ); userlist.add(u); u = new user( 2 , "李四" , "456" ); userlist.add(u); u = new user( 3 , "王五" , "789" ); userlist.add(u); u = new user( 4 , "趙六" , "233" ); userlist.add(u); } @override public list<user> getuserlist() { return userlist; } } /** * 業(yè)務(wù)邏輯層接口 * @author bc * */ public interface userbizinterface { /**查詢所有用戶信息*/ public list<user> getuserlist(); } /** * 業(yè)務(wù)邏輯層實(shí)現(xiàn)類 * @author bc * */ public class userbizimpl implements userbizinterface { /**使用spring注入*/ private userdaointerface userdao; @override public list<user> getuserlist() { return userdao.getuserlist(); } /**通過set方法注入,因此需要注入的屬性必須設(shè)置set方法*/ public void setuserdao(userdaointerface userdao) { this .userdao = userdao; } public userdaointerface getuserdao() { return userdao; } } |
3、編寫applicationcontext.xml配置文件
表頭信息:
1
2
3
4
5
6
7
|
<?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-4.1.xsd"> |
配置代碼:
1
2
3
4
5
6
7
|
<!-- 數(shù)據(jù)訪問層對象:userdao --> <bean id= "userdao" class = "com.bc.dao.impl.userdaoimpl" ></bean> <!-- 業(yè)務(wù)邏輯層對象:userbiz --> <bean id= "userbiz" class = "com.bc.biz.impl.userbizimpl" > <!-- 通過set方法注入數(shù)據(jù)訪問層屬性 --> <property name= "userdao" ref= "userdao" /> </bean> |
測試代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class userbiztest { private applicationcontext ctx; @before public void load() { //讀取applicationcontext.xml配置文件 ctx = new classpathxmlapplicationcontext( "applicationcontext.xml" ); } @test public void getuserlisttest() { //創(chuàng)建一個業(yè)務(wù)邏輯層對象 userbizinterface userdao = (userbizinterface) ctx.getbean( "userbiz" ); //調(diào)用方法獲取用戶信息 list<user> userlist = userdao.getuserlist(); //遍歷集合 for (user user : userlist) { system.out.println(user.getid() + "|" + user.getusername() + "|" +user.getpassword()); } } } |
上面的實(shí)例代碼中,我們使用的是set方法注入,spring的注入方式有許多種,注入的屬性類型也有很多情況,詳細(xì)請參考:
淺談spring ioc的注入方式及注入不同的數(shù)據(jù)類型
關(guān)于bean的作用域
scope=”singleton” 默認(rèn),表示在spring容器中僅存在一個共享的bean實(shí)例
scope=”prototype” 每次從容器中都獲取一個新的實(shí)例
scope=”request” 每次http請求都會創(chuàng)建一個新的bean實(shí)例
scope=”session” 同一個http請求共享一個bean實(shí)例
scope=”global session” 同一個全局session共享一個bean實(shí)例
總結(jié)
以上就是本文關(guān)于spring ioc的簡單實(shí)例及bean的作用域?qū)傩越馕龅娜績?nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/qq_32588349/article/details/51554080