Spring 泛型依赖注入

BaseService<T>:有RoleService和UserService两的子类
BaseRepepositry<T>:有UserRepository和RoleRepositry两个子类
由于BaseService<T>和BaseRepepositry<T>有关系所以,得出下面的子类也存在这样的关系
具体代码
1、User.java
package com.proc.bean;
public class User {
}
2、BaseRepository.java
package com.proc.repository;
public class BaseRepository<T> {
}
3、BaseService.java
package com.proc.service;
import org.springframework.beans.factory.annotation.Autowired;
import com.proc.repository.BaseRepository;
public class BaseService<T> {
@Autowired
protected BaseRepository<T> baseRepository;
public void save(){
System.out.println("save ………………");
System.out.println(baseRepository);
}
}
4、UserRepository.java
package com.proc.repository; import org.springframework.stereotype.Repository; import com.proc.bean.User; @Repository
public class UserRepository extends BaseRepository<User> { }
5、UserService.java
package com.proc.service; import org.springframework.stereotype.Service; import com.proc.bean.User; @Service
public class UserService extends BaseService<User>{ }
6、xml配置
<context:component-scan base-package="com.proc" />
7、代码测试
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService=(UserService) ctx.getBean("userService");
userService.save();
输出结果:
save ………………
com.proc.repository.UserRepository@15bfd87
这里可以看到对象的类型为UserRepository
Spring 泛型依赖注入的更多相关文章
- 【串线篇】spring泛型依赖注入原理
spring泛型依赖注入原理 不管三七二十一 servlet :加注解@servlet service:加注解@service dao:加注解@Repository 这相当于在容器中注册这些个类
- Spring泛型依赖注入
1.定义基础仓库 package com.spring.generic.di; public class BaseRepository<T> { } 2.定义基础服务层 package ...
- Spring4.0学习笔记(9) —— Spring泛型依赖注入
1.定义基础仓库 package com.spring.generic.di; public class BaseRepository<T> { } 2.定义基础服务层 package c ...
- Spring 泛型依赖注入(3)
BaseService<T>:有RoleService和UserService两的子类 BaseRepepositry<T>:有UserRepository和RoleRepos ...
- Spring基础—— 泛型依赖注入
一.为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入. 二.泛型依赖注入:子类之间的依赖关系由其父类泛型以及父类之 ...
- Spring(十六):泛型依赖注入
简介: Spring4.X之后开始支持泛型依赖注入. 使用示例: 1.定义实体 package com.dx.spring.bean.componentscan; import java.io.Ser ...
- Spring.NET依赖注入框架学习-- 泛型对象的创建和使用
Spring.NET依赖注入框架学习-- 泛型对象的创建和使用 泛型对象的创建方法和普通对象是一样的. 通过构造器创建泛型对象 下面是一个泛型类的代码: namespace GenericsPlay ...
- Spring的泛型依赖注入
Spring 4.x 中可以为子类注入子类对应的泛型类型的成员变量的引用,(这样子类和子类对应的泛型类自动建立关系)具体说明: 泛型注入:就是Bean1和Bean2注入了泛型,并且Bean1和Bean ...
- Spring初学之泛型依赖注入
主要讲泛型依赖注入,所以核心在java文件,配置文件中只需配置扫描包即可,如下: <?xml version="1.0" encoding="UTF-8" ...
随机推荐
- bzoj 1458: 士兵占领 -- 最大流
1458: 士兵占领 Time Limit: 10 Sec Memory Limit: 64 MB Description 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵 ...
- TortoiseSVN 修改密码
在第一次使用TortoiseSVN从服务器CheckOut的时候,会要求输入用户名和密码,这时输入框下面有个选项是保存认证信息,如果选了这个选项,那么以后就不用每次都输入一遍用户名密码了. 不过,如果 ...
- 使用STL中的list容器实现单链表的操作
#include<iostream> #include<list> #include<algorithm> using namespace std; void Pr ...
- Current mirror drives multiple LEDs from a low supply voltage
Driving LEDs at a regulated current from low supply voltages can be difficult because minimal overhe ...
- 插值技术之Bezier插值(1) -- Bezier Curve
作者:i_dovelemon 来源:CSDN 日期:2015 / 7 / 11 主题:Interpolate,Bezier Curve 引言 在游戏开发中.诸如动画系统.路径计算等等操作,都会遇到对数 ...
- MONO,原来你是水中月
什么是MONO? MONO项目是由Ximian发起的,由Miguel de lcaza领导的,一个致力于开创.NET在Linux上使用的开源工程.它包含了一个C#语言的编译器,一个CLR的运行时,和一 ...
- 常见dotNet加密保护工具分析介绍(转)
本文主要介绍一些dotNet加密保护工具的原理以及就其脱壳进行简单探讨.remotesoft protector.maxtocode..Net Reactor.Cliprotector .themi ...
- appium+python自动化52-多点触控MultiAction
前言 MultiAction是针对多点触控操作的,是TouchAction的一个补充模块 TouchAction用法参考前面的一篇:appium+python自动化33-TouchAction 多点触 ...
- booksleeve 使用
By offering pipelined, asynchronous, multiplexed and thread-safe access to redis, BookSleeve enables ...
- Andorid之Annotation框架初使用(四)
代替繁琐的finViewById @EActivity public class MyActivity extends Activity { // Injects R.id.myEditText @V ...