简介:

Spring4.X之后开始支持泛型依赖注入。

使用示例:

1、定义实体

package com.dx.spring.bean.componentscan;

import java.io.Serializable;

public class Member implements Serializable {
private static final long serialVersionUID = -7106886149424419957L; }
package com.dx.spring.bean.componentscan;

import java.io.Serializable;

public class Role implements Serializable{
private static final long serialVersionUID = 514142692576163383L; }

2、定义基础仓库

package com.dx.spring.bean.componentscan;

import java.io.Serializable;

public class BaseRepository<T extends Serializable> {
public void add() {
System.out.println("BaseRepository add");
}
}

3、定义基础服务层

package com.dx.spring.bean.componentscan;

import java.io.Serializable;

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

public class BaseService<T extends Serializable> {
@Autowired
protected BaseRepository<T> baseRepository; public void add() {
System.out.println("BaseService add ");
}
}

4、定义仓库服务层

package com.dx.spring.bean.componentscan;

import org.springframework.stereotype.Repository;

@Repository
public class MemberRepositoryImpl extends BaseRepository<Member> {
@Override
public void add() {
System.out.println("Add");
}
}
package com.dx.spring.bean.componentscan;

import org.springframework.stereotype.Repository;

@Repository
public class RoleRepositoryImpl extends BaseRepository<Role> { }

5、定义Member/Role服务层

package com.dx.spring.bean.componentscan;

import org.springframework.stereotype.Service;

@Service
public class MemberServiceImpl extends BaseService<Member> {
@Override
public void add() {
System.out.println(baseRepository);
super.add();
}
}
package com.dx.spring.bean.componentscan;

import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl extends BaseService<Role> {
@Override
public void add() {
System.out.println(baseRepository);
super.add();
}
}

6、测试类

package com.dx.spring.bean.componentscan;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-component-scan2.xml");
MemberServiceImpl memberService = (MemberServiceImpl) ctx.getBean("memberServiceImpl");
memberService.add(); RoleServiceImpl roleServiceImpl = (RoleServiceImpl) ctx.getBean("roleServiceImpl");
roleServiceImpl.add();
}
}

打印日志:

com.dx.spring.bean.componentscan.MemberRepositoryImpl@530612ba
BaseService add
com.dx.spring.bean.componentscan.RoleRepositoryImpl@2a40cd94
BaseService add

Spring(十六):泛型依赖注入的更多相关文章

  1. Spring初学之泛型依赖注入

    主要讲泛型依赖注入,所以核心在java文件,配置文件中只需配置扫描包即可,如下: <?xml version="1.0" encoding="UTF-8" ...

  2. Spring第六弹—-依赖注入之使用构造器注入与使用属性setter方法注入

    所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中. 使用构造器注入   1 2 3 4 <constructor-arg index=“0” type=“java.lang. ...

  3. Spring基础—— 泛型依赖注入

    一.为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入. 二.泛型依赖注入:子类之间的依赖关系由其父类泛型以及父类之 ...

  4. Spring的泛型依赖注入

    Spring 4.x 中可以为子类注入子类对应的泛型类型的成员变量的引用,(这样子类和子类对应的泛型类自动建立关系)具体说明: 泛型注入:就是Bean1和Bean2注入了泛型,并且Bean1和Bean ...

  5. 【串线篇】spring泛型依赖注入原理

    spring泛型依赖注入原理 不管三七二十一 servlet :加注解@servlet service:加注解@service dao:加注解@Repository 这相当于在容器中注册这些个类

  6. Spring新特性_泛型依赖注入

    泛型依赖注入 package com.tanlei.spring.generic; import org.springframework.beans.factory.annotation.Autowi ...

  7. 转载--浅谈spring4泛型依赖注入

    转载自某SDN-4O4NotFound Spring 4.0版本中更新了很多新功能,其中比较重要的一个就是对带泛型的Bean进行依赖注入的支持.Spring4的这个改动使得代码可以利用泛型进行进一步的 ...

  8. Spring4学习回顾之路10-Spring4.x新特性:泛型依赖注入

    泛型依赖注入:Spring 4.x中可以为子类注入子类对应的泛型类型的成员变量的引用. 话语太过抽象,直接看代码案例,依次建立如下代码: User.java package com.lql.sprin ...

  9. (转)Spring读书笔记-----Spring核心机制:依赖注入

    Java应用(从applets的小范围到全套n层服务端企业应用)是一种典型的依赖型应用,它就是由一些互相适当地协作的对象构成的.因此,我们说这些对象间存在依赖关系.加入A组件调用了B组件的方法,我们就 ...

随机推荐

  1. j.u.c系列(07)---之读写锁:ReentrantReadWriteLock

    写在前面 重入锁ReentrantLock是排他锁,排他锁在同一时刻仅有一个线程可以进行访问,但是在大多数场景下,大部分时间都是提供读服务,而写服务占有的时间较少.然而读服务不存在数据竞争问题,如果一 ...

  2. 使用UltraISO制作Windows 10启动U盘

    1.从官方网站下载制作工具UltraISO:http://cn.ultraiso.net/uiso9_cn.exe 这是个试用版,但也足够用一次了. 2.在电脑上插入一块U盘,容量最好不少于8GB,接 ...

  3. PHP上传文件大小限制的问题(转)

      在用PHP进行文件上传的操作中,需要知道怎么控制上传文件大小的设置,而文件可传大小是受到多种因素制约的,现总结如下:1.php.ini:upload_max_filesize 所上传的文件的最大大 ...

  4. 异步接收MSMQ消息

    在这部分,我们将使用ThreadPool 和MSMQ 进行消息收发.MSMQ 是一个分布式队列,通过MSMQ 一个应用程序可以异步地与另外一个应用程序通信. 在一个典型的场景中,我们要向维护一个队列的 ...

  5. Fiddler 实现手机的抓包

    Fiddler是我最喜爱的工具,几乎每天都用, 我已经用了8年了. 至今我也只学会其中大概50%的功能. Fiddler绝对称得上是"神器", 任何一个搞IT的人都得着的. 小弟我 ...

  6. 【shell学习笔记】curl命令总结

    2014-12-16 20:34 文思海辉 =========== CURL命令总结 1. 下载 curl -o [文件名称] www.baidu.com 2. 显示 HTTP request头信息 ...

  7. 这里包含几乎所有的xcode正式版本

    https://developer.apple.com/downloads/

  8. AngularJS自定义Directive初体验

    通常我们这样定义个module并随之定义一个controller. var app = angular.module('myApp', []); app.controller('CustomersCo ...

  9. (Delphi) Using the Disk Cache 使用磁盘缓存

    The Chilkat Spider component has disk caching capabilities. To setup a disk cache, create a new dire ...

  10. VS2017下Git的使用

    一.使用GIT上的项目 (1)找到项目的git地址 (2)打开 vs2017的团队资源管理器面板,直接克隆(复制)远程Github上的项目 (3)追加新项目,到以上工程中. 新建项目时,把项目创建在步 ...