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" ...
随机推荐
- HDU 5643 King's Game 打表
King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...
- Shell基础学习(三) 传递参数
我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 以下实例我们向脚本传递三个参数 ...
- activemq消息重发机制[转]
大家知道,JMS规范中,Message消息头接口中有setJMSRedelivered(boolean redelivered)和getJMSRedelivered()方法,用于设置和获取消息的重发标 ...
- windows安装zookeeper单机版
1.在apache的官方网站提供了好多镜像下载地址,然后找到对应的版本,目前最新的是3.4.6下载地址:http://mirrors.cnnic.cn/apache/zookeeper/zookeep ...
- Jenkins使用jenkins-cli.jar进行远程调用时出现“ERROR: No such job 'test'”或者权限不够等问题解决(Windows)
网上最提倡的解决办法是用SSH的key进行登录,但是我发觉Linux上非常容易实现,但是Windows压根不知道在哪里设置. 原文:https://issues.jenkins-ci.org/brow ...
- Struts2的ActionError&ActionMessage示例
本教程显示使用Struts2的 ActionError 和 ActionMessage 类. 1. ActionError – 是用来发送错误信息反馈给用户 - 通过 <s:actionerro ...
- cadence学习(1)常规封装的建立
1.建立焊盘. (1)首先要获得datasheet(或可用pcb matrix ipc-7531标准的可查询封装软件)中元器件的封装信息. (2)建立.pad文件.打开PCB Editor Utili ...
- systemd-udevd
描述:systemd-udevd是监听内核发出的设备事件,并根据udev规则处理每个事件. 选项: --daemon 脱离控制台,并作为后台守程运行. --debug 在标准错误上打印调试信息 --c ...
- Labeled Faces in the Wild 人脸识别数据集 部分训练数据
development training set Note: images displayed are original (non-aligned/funneled) images. match pa ...
- dedecms织梦 标签远程文件写入漏洞
测试方法: @Sebug.net dis本站提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! 前题条件,必须准备好自己的dede数据库,然后插入数据: insert into d ...