項目結構如下:
如何思考?面向抽象編程?
先來看看一個序列圖
從以上看出了什么?初學的,能看得出是什么才怪,那不管它了??纯淳唧w實現吧
首先要建立一個實體類:User,放在model包下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.wwj.model; /** * 實體類 * @author wwj * Spring */ public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } } |
下面就是叫你如何面向抽象編程,簡單來說如何面向接口編程,下面定義一個接口
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.wwj.dao; import com.wwj.model.User; /** * 面向抽象編程 * @author wwj * 優點:增加了靈活性 */ public interface UserDAO { public void save(User user); } |
可以很清楚看到,這個接口聲明了一個方法,save方法,里面有一個參數User對象,我們可以想到它是可以用來保存User對象到數據庫里去的。
把具體實現交給實現類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.wwj.dao.impl; import com.wwj.dao.UserDAO; import com.wwj.model.User; /** * 接口實現類 * @author wwj * */ public class UserDAOImpl implements UserDAO{ @Override public void save(User user) { System.out.println( "save user" ); } } |
這樣做的好處是,如果你要更換數據庫環境,你就能靈活定義不同的數據庫代碼了。
怎么調用以上的方法,為了使業務邏輯和數據庫操作分離開,我們需要定義一個業務邏輯類
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
|
package com.wwj.service; import com.wwj.dao.UserDAO; import com.wwj.dao.impl.UserDAOImpl; import com.wwj.model.User; /** * 服務類,實現業務邏輯 * @author wwj * */ public class UserService { private UserDAO userDAO; public UserDAO getUserDAO() { return userDAO; } public void setUserDAO(UserDAO userDAO) { this .userDAO = userDAO; } public void add(User user) { this .userDAO.save(user); } } |
我們可以看到,上面有一個東西,東西?UserDAO啊,看到它的作用了沒有,看到才怪。這里設計到一個IOC也叫做DI的概念,中文意思叫做依賴注入,也叫控制反轉,這在Spring是一個很重要的概念,要把它弄懂,才能很好的理解Spring的原理。
下面真正模擬Spring的實現,有點像工廠模式,利用Spring,我們可以把不同對象裝配在一起使用。
先看一下配置文件beans.xml
1
2
3
4
5
6
|
< beans > < bean id = "u" class = "com.wwj.dao.impl.UserDAOImpl" /> < bean id = "userService" class = "com.wwj.service.UserService" > < property name = "userDAO" bean = "u" /> </ bean > </ beans > |
一個工廠方法
1
2
3
4
5
|
package com.wwj.spring; public interface BeanFactory { public Object getBean(String name); } |
一個解析xml文件的類,并實現BeanFactory
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
|
package com.wwj.spring; import java.lang.reflect.Method; import java.util.HashMap; import java.util.List; import java.util.Map; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; public class ClassPathXmlApplicationContext implements BeanFactory{ //定義一個容器,用來存放對象 private Map<String,Object> beans = new HashMap<String, Object>(); public ClassPathXmlApplicationContext() throws Exception{ SAXBuilder sb = new SAXBuilder(); Document doc = sb.build( this .getClass().getClassLoader().getResourceAsStream( "beans.xml" )); Element root = doc.getRootElement(); //獲取根結點 List list = root.getChildren( "bean" ); //取名為bean的所有元素 for ( int i = 0 ; i < list.size(); i++) { Element element = (Element) list.get(i); String id = element.getAttributeValue( "id" ); //取id值 String cla = element.getAttributeValue( "class" ); //取class值 Object o = Class.forName(cla).newInstance(); System.out.println(id); System.out.println(cla); beans.put(id,o); for (Element propertyElement : (List<Element>)element.getChildren( "property" )){ String name = propertyElement.getAttributeValue( "name" ); //UserDAO String bean = propertyElement.getAttributeValue( "bean" ); //u Object beanObject = beans.get(bean); //UserDAOImpl instance //拼湊方法名,實現setUserDAO方法 String methodName = "set" + name.substring( 0 , 1 ).toUpperCase() + name.substring( 1 ); System.out.println( "method name = " + methodName); //利用反射機制獲取方法對象 Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[ 0 ]); m.invoke(o, beanObject); //調用方法 } } } @Override public Object getBean(String name) { return beans.get( "id" ); } } |
來一個測試類
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
|
package com.wwj.service; import org.junit.Test; import com.wwj.model.User; import com.wwj.spring.BeanFactory; import com.wwj.spring.ClassPathXmlApplicationContext; /** * 單元測試類 * @author wwj * */ public class UserServiceTest { @Test public void testAdd() throws Exception{ BeanFactory beanFactory = new ClassPathXmlApplicationContext(); UserService service =(UserService)beanFactory.getBean( "userService" ); User u = new User(); service.add(u); } } |
從測試類我們可以看出點端倪了,首先定義一個BeanFactory對象,通過這個對象調換用其getBean的方法,獲取業務邏輯類對象,后面就可以通過調用這個服務類的add方法把user對象添加到數據庫中去。當然這里沒有實現插入數據庫,只是簡單的實現了測試。其實整個過程很明了的,Spring的核心配置文件,將對象控制起來了,當要使用的時候就將對象注入到服務類當中去,服務類就可以利用DAO層的對象,進行數據庫相關的操作。
以上就是整理的網上關于spring配置內容,希望可以對大家有所幫助。