泛型依赖注入: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. 获取 Django版本号的两种方式

    one k@ubuntu:~$ python Python ( , ::) [GCC ] on linux2 Type "help", "copyright", ...

  2. Spring Cloud Gateway(二):Spring Cloud Gateway整合Eureka应用

    Spring Cloud Gateway 应用概述 下面的示例启动两个服务:gataway-server 和 user-service 都注册到注册中心 Eureka上,客户端请求后端服务[user- ...

  3. vue-导入element-ui

    安装 npm install element-ui -S 项目中导入 修改main.js import ElementUI from 'element-ui'; import 'element-ui/ ...

  4. Flutter安装

    下载右边的安装包以获取最新版本 stable 的 Flutter SDK 将压缩包解压,然后把其中的 flutter 目录整个放在你预想的 Flutter SDK 安装目录中(比如 C:\src\fl ...

  5. js 移除数组中的内容

    使用方法:arr.splice(arr.indexOf(ele),length):表示先获取这个数组中这个元素的下标,然后从这个下标开始计算,删除长度为length的元素 这种删除方式适用于任何js数 ...

  6. python操作MySQL数据库的三个模块

    python使用MySQL主要有两个模块,pymysql(MySQLdb)和SQLAchemy. pymysql(MySQLdb)为原生模块,直接执行sql语句,其中pymysql模块支持python ...

  7. python 中对象is和==是怎么比较的

    Python中的对象包含三要素:id.type.value.其中id用来唯一标识一个对象,type标识对象的类型,value是对象的值.is判断的是a对象是否就是b对象,是通过id来判断的.==判断的 ...

  8. 路由器的路由配置命令汇总(win和linux系统)

    路由器/Linux主机/win下主机的路由配置汇总 2009-07-16 17:43:15 分类: 系统运维 工作时总是要在这三个个体中配来配去,所以为了方便,汇总了.   win下:   使用 Ro ...

  9. [Scikit-learn] 1.1 Generalized Linear Models - Logistic regression & Softmax

    二分类:Logistic regression 多分类:Softmax分类函数 对于损失函数,我们求其最小值, 对于似然函数,我们求其最大值. Logistic是loss function,即: 在逻 ...

  10. [Scikit-learn] 1.4 Support Vector Classification

    Ref: http://sklearn.lzjqsdd.com/modules/svm.html Ref: CS229 Lecture notes - Support Vector Machines ...