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");
}
}
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 + "]";
}
}
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...........");
}
}
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............");
}
}
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.........");
}
}
9、BaseRepository.java
package com.demo.sshtest.generics;
public class BaseRepository<T> {
public BaseRepository(){
System.out.println("run BaseRepository.........");
}
}
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>
11、运行效果
12、项目目录
13、demo
https://download.csdn.net/download/qq_22778717/10476271