泛型依赖注入:Spring 4.x中可以为子类注入子类对应的泛型类型的成员变量的引用。

  话语太过抽象,直接看代码案例,依次建立如下代码:

User.java

package com.lql.spring05;

/**
* @author: lql
* @date: 2019.10.28
* Description:
*/
public class User { }

BaseService.java

package com.lql.spring05;

import org.springframework.beans.factory.annotation.Autowired;

/**
* @author: lql
* @date: 2019.10.28
* Description:
*/
public class BaseService<T> { @Autowired
protected BaseRepository<T> repository; public void add() {
System.out.println("进入add()方法");
System.out.println("repository = " + repository);
}
}

BaseRepository.java

package com.lql.spring05;

/**
* @author: lql
* @date: 2019.10.28
* Description:
* Created with IntelliJ IDEA
*/
public class BaseRepository<T> { }

UserService.java

package com.lql.spring05;

import org.springframework.stereotype.Service;

/**
* @author: lql
* @date: 2019.10.28
* Description:
*/
@Service
public class UserService extends BaseService<User> { }

UserReposltory.java

package com.lql.spring05;

import org.springframework.stereotype.Repository;

/**
* @author: lql
* @date: 2019.10.28
* Description:
* Created with IntelliJ IDEA
*/
@Repository
public class UserRepository extends BaseRepository<User> { }

编写配置文件

<?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.xsd">
<context:component-scan base-package="com.lql.spring05"/>
</beans>

编写测试类

package com.lql.spring05;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author: lql
* @date: 2019.10.28
* Description:
* Created with IntelliJ IDEA
*/
public class Test {
public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("spring05.xml"); UserService userService = app.getBean("userService", UserService.class); userService.add();
}
}

测试结果:

进入add()方法
repository = com.lql.spring05.UserRepository@79da8dc5

和我们预计的一样,被注入进来了,刚才写的那么多类其实可以用如下图来概括:

Spring4学习回顾之路10-Spring4.x新特性:泛型依赖注入的更多相关文章

  1. Spring4学习回顾之路04—引用其他Bean,集合数据注入,内部Bean

    引用其他Bean 组件应用程序的Bean经常需要相互协作以完成应用程序的功能,所以要求Bean能够相互访问,就必须在Bean配置文件中指定Bean的引用.在Bean的配置文件中可以用过<ref& ...

  2. spring4新特性-泛型依赖注入

    1 文件结构  2 具体类  2.1两个抽象类,在Service里面写公共的方法,在各自的具体实现类里面写各自的方法 package repo;import model.User;/** * Crea ...

  3. Spring4学习回顾之路01—HelloWorld

    以前公司一直使用的是spring3.0,最近一段时间开始用了4.0,官网上都已经有了5.0,但是很多知识点已经忘了差不多了,趁现在项目不忙写写随笔,一来回顾自己的知识点,二来如果能帮助比我还小白的小白 ...

  4. Spring4学习回顾之路11-AOP

    Srping的核心除了之前讲到的IOC/DI之外,还有一个AOP(Aspect Oriented Programming:面向切面编程):通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 ...

  5. Spring4学习回顾之路09-基于注解的方式配置bean

    一:基于注解配置Bean 首先介绍下组件扫描(component scanning): Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 包括: -@Component ...

  6. Spring4学习回顾之路08- FactoryBean配置Bean

    建立Student.java package com.lql.srping04; /** * @author: lql * @date: 2019.10.28 * Description: */ pu ...

  7. Spring4学习回顾之路07- 通过工厂方法配置Bean

    一:通过静态工厂配置Bean 建立Student.java package com.lql.srping04; /** * @author: lql * @date: 2019.10.28 * Des ...

  8. Spring4学习回顾之路06- IOC容器中Bean的生命周期方法

    SpringIOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行特定的任务! Spring IOC容器对Bean的生命周期进行管理的过程: -通过构造器或者工厂方法创建 ...

  9. Spring4学习回顾之路05—自动装配,Bean的继承,依赖和作用域

    自动装配 xml配置里的Bean的自动装配,Spring IOC容器可以自动装配Bean,仅仅需要做的是在<bean>标签里的autowire属性里指定自动装配的模式. ①byType(根 ...

随机推荐

  1. unix/linux 进程间文件锁

    转自 http://www.cnblogs.com/hjslovewcl/archive/2011/03/14/2314333.html 有三种不同的文件锁,这三种都是“咨询性”的,也就是说它们依靠程 ...

  2. RabbitMQ 和 Kafka 的消息可靠性对比

    RabbitMQ和Kafka都提供持久的消息保证.两者都提供至少一次和至多一次的保证,另外,Kafka在某些限定情况下可以提供精确的一次(exactly-once)保证. 让我们首先理解一下上述术语的 ...

  3. Robot Framework(十七) 扩展RobotFramework框架——扩展Robot Framework Jar

    4.4扩展Robot Framework Jar 使用标准JDK安装中包含的jar命令,可以非常简单地向Robot Framework jar添加其他测试库或支持代码.Python代码必须放在jar里 ...

  4. Gi命令行操作

    一.本地库初始化 命令:git init 效果: 注意:.git 目录中存放的是本地库相关的子目录和文件,不要删除,也不要胡乱修改 二.设置签名 形式 用户名:user Email 地址:user@1 ...

  5. 安装使用VUE

    安装使用VUE 如果是简单实用vue的话,可以直接引用js文件. https://vuejs.org/js/vue.js 但是在构建大型项目的时候推荐使用NPM安装,NPM能够很好的和诸如webpac ...

  6. AngularJS开发中常用的写法,如:获取URL参数、路由跳转、$http、获取元素等

    控制器,带状态 app.controller('editCtrl', ['$http', '$location', '$rootScope', '$scope', '$state', '$stateP ...

  7. Flutter实现TabBarView切换页面时每个页面只initState一次

    在  TabBarView  组件中切换页面时,子页面每次均会重新  initState  一次,导致每次都切换页面均会重绘,如下图 如果需要只在第一次进页面  initState  一次,后面再进入 ...

  8. ArcGIS超级工具SPTOOLS-MXD操作篇

    1.1  MXD批量裁剪 操作视频:https://weibo.com/tv/v/Hy7P6bF7d?fid=1034:4381332084881258 把当前窗口的MXD,按某个图层的某个字段批量裁 ...

  9. SpringBoot RestTemplate接收文件,并将文件发送到另外一个程序进行存储

    最近有个需求,接收用户上报的证书,并且保存起来,证书大小不到1M,但该证书的保存必须在另外一个程序进行,所以想到使用springboot接收上传文件后,再通过RestTemplate将文件发送给另外一 ...

  10. layer快速点击会触发多次回调

    场景还原 测试同学反馈点击了一次操作,为什么会有两条操作记录? 我:???? 排查思路 查看日志,看一下是不是发了两次请求,果不其然啊: 并发了,同一时间发送了两次请求,出现了脏写. 原因 系统的co ...