泛型依赖注入: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. 【luogu4145】上帝造题的七分钟2 / 花神游历各国--区间开根-线段树

    题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...

  2. 又见回文 (SDUT 2560)

    Problem Description "回文串"是一个正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.现在呢, ...

  3. docker 容器内部访问宿主机

    在宿主机执行: ifconfig 然后查看 docker0 的那个网卡的 ip 地址,比如我的是 172.18.0.1

  4. nginx 配置简单反向代理

    假设端口号是 3000 server { listen ; server_name your.domain; location / { proxy_pass http://127.0.0.1:3000 ...

  5. arcgis python 删除一个数据库所有数据

    # -*- coding: cp936 -*- import xlrd # must init xlrd import arcpy import os def main(): arcpy.env.wo ...

  6. error:Cannot pull with rebase

    原文文链接:https://blog.csdn.net/u012385190/article/details/70670213git 执行git pull –rebase报错误如下: error: C ...

  7. centos中screen的使用

    只是讲解几个简单的操作,例如,创建新窗口,切换到新窗口,删除窗口.(因为我要启动elk,所以需要使用到screen) 一.创建一个新窗口: 安装完成后,直接敲命令screen就可以启动它.但是这样启动 ...

  8. 007-springboot 控制台中文乱码

    处理方式 1.FIle encoding 设置为UTF-8 2.-Dfile.encoding=UTF-8 配置tomcat 的的VM启动参数:-Dfile.encoding=UTF-8 3.idea ...

  9. Qt编写自定义控件35-GIF录屏控件

    一.前言 在平时的写作过程中,经常需要将一些操作动作和效果图截图成gif格式,使得涵盖的信息更全面更生动,有时候可以将整个操作过程和运行效果录制成MP4,但是文件体积比较大,而且很多网站不便于上传,基 ...

  10. ios 本地模糊搜索

    /* 手机 选择区域 模型 */ #import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface MYSelect ...