ioc(inversion if control)-控制反轉是spring倆大核心技術之一,ioc一般分為倆種類型:依賴注入(dependency injection,簡稱di)和依賴查找(dependency lookup)
使用示例:
1、新建工程并導入spring相關jar包。
2、新建數據訪問層及業務邏輯層
代碼結構:
代碼示例:
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
|
/** * 實體bean * @author bc * */ public class user { private integer id; private string username; private string password; //get set方法略 } /** * 數據訪問層接口 * @author bc * */ public interface userdaointerface { /**查詢所有用戶信息*/ public list<user> getuserlist(); } /** * 數據訪問層實現類 * @author bc * */ public class userdaoimpl implements userdaointerface { /**模擬數據庫數據*/ 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; } } /** * 業務邏輯層接口 * @author bc * */ public interface userbizinterface { /**查詢所有用戶信息*/ public list<user> getuserlist(); } /** * 業務邏輯層實現類 * @author bc * */ public class userbizimpl implements userbizinterface { /**使用spring注入*/ private userdaointerface userdao; @override public list<user> getuserlist() { return userdao.getuserlist(); } /**通過set方法注入,因此需要注入的屬性必須設置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
|
<!-- 數據訪問層對象:userdao --> <bean id= "userdao" class = "com.bc.dao.impl.userdaoimpl" ></bean> <!-- 業務邏輯層對象:userbiz --> <bean id= "userbiz" class = "com.bc.biz.impl.userbizimpl" > <!-- 通過set方法注入數據訪問層屬性 --> <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() { //創建一個業務邏輯層對象 userbizinterface userdao = (userbizinterface) ctx.getbean( "userbiz" ); //調用方法獲取用戶信息 list<user> userlist = userdao.getuserlist(); //遍歷集合 for (user user : userlist) { system.out.println(user.getid() + "|" + user.getusername() + "|" +user.getpassword()); } } } |
上面的實例代碼中,我們使用的是set方法注入,spring的注入方式有許多種,注入的屬性類型也有很多情況,詳細請參考:
關于bean的作用域
scope=”singleton” 默認,表示在spring容器中僅存在一個共享的bean實例
scope=”prototype” 每次從容器中都獲取一個新的實例
scope=”request” 每次http請求都會創建一個新的bean實例
scope=”session” 同一個http請求共享一個bean實例
scope=”global session” 同一個全局session共享一個bean實例
總結
以上就是本文關于spring ioc的簡單實例及bean的作用域屬性解析的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/qq_32588349/article/details/51554080