SSH筆記-泛型依賴註入
1、作用:為子類註入子類對應的泛型類型的成員變量的引用
2、文件
①TestMain.java:測試類
②Info.java:數據模型
③InfoService.java:測試繼承BaseService
④InfoRepository.java:測試繼承BaseRepository
⑤BaseService.java:被繼承的類
⑥BaseRepository.java:被繼承的類
⑦genericsContext.xml:配置文件
3、用法:
(1)運行邏輯:
①基類為BaseService和BaseRepository,BaseService引用了BaseRepository,則可以通過BaseService調用BaseRepository屬性方法
②當InfoService和InfoRepository分別繼承BaseService和BaseRepository時,需要為BaseService和BaseRepository的泛型定義
③當InfoService和InfoRepository分別繼承完畢之後,根據泛型依賴註入的特性,InfoService和InfoRepository會參照BaseService引用了BaseRepository的引用關系自動建立對應引用關系,這就是泛型依賴註入
(2)調用順序:
①Main調用InfoService
②因為InfoService繼承了BaseService,而且BaseService引用了BaseRepository,所以先運行BaseRepository的對象方法及獲取其屬性
③運行完BaseRepository之後,運行BaseService對象方法及獲取其屬性
④最後執行InfoService的方法及獲取其屬性
(3)註意事項:
①執行InfoService和InfoRepository,程序調用順序是一樣的
②同一個線程中,執行InfoService或InfoRepository其中一個之後,就不會執行另一個
(4)泛型依賴註入作用:
①提前封裝好一些操作的邏輯到BaseService和BaseRepository中,當調用繼承了這兩個泛型類的實現類時,就能根據實現類的定義對指定Bean類的數據進行操作,多用於針對不同數據表操作或一些網絡請求的邏輯時,編寫的公用操作方法
4、TestMain.java
package com.demo.sshtest.generics;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
//基類為BaseService和BaseRepository,BaseService引用了BaseRepository,則可以通過BaseService調用BaseRepository屬性方法
//當InfoService和InfoRepository分別繼承BaseService和BaseRepository時,需要為BaseService和BaseRepository的泛型定義
//當InfoService和InfoRepository分別繼承完畢之後,根據泛型依賴註入的特性,InfoService和InfoRepository會參照BaseService引用了BaseRepository的引用關系自動建立對應引用關系,這就是泛型依賴註入
//調用順序:
//(1)Main調用InfoService
//(2)因為InfoService繼承了BaseService,而且BaseService引用了BaseRepository,所以先運行BaseRepository的對象方法及獲取其屬性
//(3)運行完BaseRepository之後,運行BaseService對象方法及獲取其屬性
//(4)最後執行InfoService的方法及獲取其屬性
//註意事項:
//(1)執行InfoService和InfoRepository,程序調用順序是一樣的
//(2))同一個線程中,執行InfoService或InfoRepository其中一個之後,就不會執行另一個
//泛型依賴註入作用:
//提前封裝好一些操作的邏輯到BaseService和BaseRepository中,當調用繼承了這兩個泛型類的實現類時,就能根據實現類的定義對指定Bean類的數據進行操作,多用於針對不同數據表操作或一些網絡請求的邏輯時,編寫的公用操作方法
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("genericsContext.xml");
InfoRepository infoRepository = (InfoRepository)applicationContext.getBean("infoRepository");
System.out.println("----------------------------");
InfoService infoService = (InfoService)applicationContext.getBean("infoService");
}
}
- 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
5、Info.java
package com.demo.sshtest.generics;
public class Info {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Info [id=" + id + ", name=" + name + "]";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
http://www.bokee.net/bloggermodule/blog_viewblog.do?id=31933291
http://www.bokee.net/bloggermodule/blog_viewblog.do?id=31933288
http://www.bokee.net/bloggermodule/blog_viewblog.do?id=31933283
http://www.bokee.net/bloggermodule/blog_viewblog.do?id=31931284
http://www.bokee.net/bloggermodule/blog_viewblog.do?id=31931268
6、InfoService.java
package com.demo.sshtest.generics;
import org.springframework.stereotype.Component;
@Component
public class InfoService extends BaseService<Info>{
public InfoService() {
System.out.println("InfoService...........");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
7、InfoRepository.java
package com.demo.sshtest.generics;
import org.springframework.stereotype.Repository;
@Repository
public class InfoRepository extends BaseRepository<Info>{
public InfoRepository() {
System.out.println("InfoRepository............");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
8、BaseService.java
package com.demo.sshtest.generics;
import org.springframework.beans.factory.annotation.Autowired;
public class BaseService<T> {
@Autowired
private BaseRepository<T> baseRepository;
public BaseService(){
System.out.println("run baseService.........");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
9、BaseRepository.java
package com.demo.sshtest.generics;
public class BaseRepository<T> {
public BaseRepository(){
System.out.println("run BaseRepository.........");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
10、genericsContext.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.demo.sshtest.generics"></context:component-scan>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
11、運行效果
12、項目目錄
13、demo
SSH筆記-泛型依賴註入