Spring(十六):泛型依赖注入
简介:
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(十六):泛型依赖注入的更多相关文章
- Spring初学之泛型依赖注入
主要讲泛型依赖注入,所以核心在java文件,配置文件中只需配置扫描包即可,如下: <?xml version="1.0" encoding="UTF-8" ...
- Spring第六弹—-依赖注入之使用构造器注入与使用属性setter方法注入
所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中. 使用构造器注入 1 2 3 4 <constructor-arg index=“0” type=“java.lang. ...
- Spring基础—— 泛型依赖注入
一.为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入. 二.泛型依赖注入:子类之间的依赖关系由其父类泛型以及父类之 ...
- Spring的泛型依赖注入
Spring 4.x 中可以为子类注入子类对应的泛型类型的成员变量的引用,(这样子类和子类对应的泛型类自动建立关系)具体说明: 泛型注入:就是Bean1和Bean2注入了泛型,并且Bean1和Bean ...
- 【串线篇】spring泛型依赖注入原理
spring泛型依赖注入原理 不管三七二十一 servlet :加注解@servlet service:加注解@service dao:加注解@Repository 这相当于在容器中注册这些个类
- Spring新特性_泛型依赖注入
泛型依赖注入 package com.tanlei.spring.generic; import org.springframework.beans.factory.annotation.Autowi ...
- 转载--浅谈spring4泛型依赖注入
转载自某SDN-4O4NotFound Spring 4.0版本中更新了很多新功能,其中比较重要的一个就是对带泛型的Bean进行依赖注入的支持.Spring4的这个改动使得代码可以利用泛型进行进一步的 ...
- Spring4学习回顾之路10-Spring4.x新特性:泛型依赖注入
泛型依赖注入:Spring 4.x中可以为子类注入子类对应的泛型类型的成员变量的引用. 话语太过抽象,直接看代码案例,依次建立如下代码: User.java package com.lql.sprin ...
- (转)Spring读书笔记-----Spring核心机制:依赖注入
Java应用(从applets的小范围到全套n层服务端企业应用)是一种典型的依赖型应用,它就是由一些互相适当地协作的对象构成的.因此,我们说这些对象间存在依赖关系.加入A组件调用了B组件的方法,我们就 ...
随机推荐
- C++ Curiously Recurring Template Prattern(CRTP)例程
简单介绍和例子请参考:C++ 惯用法 CRTP 简介 下面例子为兼顾CRTP和多态的例子. #include <iostream> #include <vector> usin ...
- STM32F2x Is it possible to request multiple DMA streams with single request
I want to setup an application, where a single trigger-factor (compare-match of a timer) shall reque ...
- 【Go命令教程】6. go doc 与 godoc
go doc 命令可以打印附于Go语言程序 实体 上的文档.我们可以通过把程序实体的标识符作为该命令的参数来达到查看其文档的目的. 插播:所谓 Go语言的 程序实体,是指变量.常量.函数.结构体以及接 ...
- select,poll,epoll之间的区别
(1)select,poll实现需要自己不断轮询所有fd集合,直到设备就绪,期间可能要睡眠和唤醒多次交替.而epoll其实也需要调用epoll_wait不断轮询就绪链表,期间也可能多次睡眠和唤醒交替, ...
- Revit手工创建族
手工创建族 1.画两个参考平面. 图3001 2.点击族类型,添加参数. 图3002,3003 3.添加类型,为类型赋值. 3004 4.创建拉伸截面,完成后,可以三维查看. 3005 5.创建对齐, ...
- 再谈vc发送键盘、组合键消息
关于向Windows窗口发送Alt组合键的问题,这个真是经典问题啊,在网上找了一下,问的人N多,方法差不多, 但就是没有很好解决问题. 之前找到一个能正确发送的code:(Alt+A) PostMes ...
- UICollectionView 相关
当数据不多,collectionView.contentSize小于collectionView.frame.size的时候,UICollectionView是不会滚动的 self.Cov.alway ...
- 禁止浏览器backspace键(退格键)时跳转页面(extjs,javascript)
Ext实现方式: //方法一 var key = new Ext.KeyMap(document,{ key: 8, fn: function(obj,e){ var type = e ...
- 无法上外网, ping网关ping不通的解决——arp命令
转自:http://jingyan.baidu.com/article/3c48dd34873909e10be35894.html 转自:http://man.linuxde.net/arp 用来管理 ...
- Java集合类: Set、List、Map、Queue使用场景
目录 1. Java集合类基本概念 2. Java集合类架构层次关系 3. Java集合类的应用场景代码 1. Java集合类基本概念 在编程中,常常需要集中存放多个数据.从传统意义上讲,数组是我们的 ...