泛型依赖注入: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. php安装扩展的地址

    1 查看扩展 phpinfo  or extention_loads  or php -m 下载扩展地址 http://pecl.php.net     or http://windows.php.n ...

  2. Til the Cows Come Home ( POJ 2387) (简单最短路 Dijkstra)

    problem Bessie is out in the field and wants to get back to the barn to get as much sleep as possibl ...

  3. 如何用 Windows Live Writer 和 Word 2013 分别发表博客到Cnblog 和CSDN

    ps CSDN 老是505错误,放弃了 为什么会写这篇 最近写博客在 Cnblog 上面写博客, 发现图片不能复制了直接粘贴上,这对于把博客当随手笔记的人来说无疑非常痛苦.求助于博客园,他们让我用 W ...

  4. [python]打印异常信息的不同方式

    异常捕获 try: execpt Exception as e: print(str(e)) 打印异常信息的方式 1.str(e) 返回字符串类型,只给出异常信息,不包括异常信息的类型,如1/0的异常 ...

  5. OpenJudge1.5.6:整数序列的元素最大跨度值

    描述 给定一个长度为n的非负整数序列,请计算序列的最大跨度值(最大跨度值 = 最大值减去最小值). 输入一共2行,第一行为序列的个数n(1 <= n <= 1000),第二行为序列的n个不 ...

  6. EGL Driver message (Critical) eglInitialize: No available renderers.

    使用Python的selenium库进行自动化巡检.并将相对应的数据保存 环境: Windows Embedded Standard Python 2.7.16 selenium 3.141.0 [0 ...

  7. FLUENT多孔介质数值模拟设置【转载】

    转载自:http://zhengjun0228.blog.163.com/blog/static/71377014200971895419613/ 多孔介质条件 多孔介质模型可以应用于很多问题,如通过 ...

  8. pwn学习日记Day22 《程序员的自我修养》读书笔记

    知识杂项 软连接 命令: ln -s 原文件 目标文件 特征: 1.相当于windows的快捷方式 2.只是一个符号连接,所以软连接文件大小都很小 3.当运行软连接的时候,会根据连接指向找到真正的文件 ...

  9. PSO算法

    1.简介粒子群优化算法(PSO)是一种进化计算技术(evolutionary computation),1995 年由Eberhart 博士和kennedy 博士提出,源于对鸟群捕食的行为研究 .该算 ...

  10. Qt pro工程文件介绍

    app - 建立一个应用程序的makefile.这是默认值,所以如果模板没有被指定,这个将被使用. lib - 建立一个库的makefile. vcapp - 建立一个应用程序的Visual Stud ...