BaseService<T>:有RoleService和UserService两的子类

BaseRepepositry<T>:有UserRepository和RoleRepositry两个子类

由于BaseService<T>和BaseRepepositry<T>有关系所以,得出下面的子类也存在这样的关系

具体代码

1、User.java

1 package com.proc.bean;
2
3 public class User {
4
5 }

2、BaseRepository.java

1 package com.proc.repository;
2
3 public class BaseRepository<T> {
4
5 }

3、BaseService.java

 1 package com.proc.service;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4
5 import com.proc.repository.BaseRepository;
6
7 public class BaseService<T> {
8
9 @Autowired
10 protected BaseRepository<T> baseRepository;
11
12 public void save(){
13 System.out.println("save ………………");
14 System.out.println(baseRepository);
15 }
16 }

4、UserRepository.java

 1 package com.proc.repository;
2
3 import org.springframework.stereotype.Repository;
4
5 import com.proc.bean.User;
6
7 @Repository
8 public class UserRepository extends BaseRepository<User> {
9
10 }

5、UserService.java

 1 package com.proc.service;
2
3 import org.springframework.stereotype.Service;
4
5 import com.proc.bean.User;
6
7 @Service
8 public class UserService extends BaseService<User>{
9
10 }

6、xml配置

<context:component-scan base-package="com.proc" />

7、代码测试

1 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
2 UserService userService=(UserService) ctx.getBean("userService");
3 userService.save();

输出结果:

save ………………
com.proc.repository.UserRepository@15bfd87

这里可以看到对象的类型为UserRepository

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

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

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

  2. Spring泛型依赖注入

    1.定义基础仓库 package com.spring.generic.di; public class BaseRepository<T> { } 2.定义基础服务层   package ...

  3. Spring4.0学习笔记(9) —— Spring泛型依赖注入

    1.定义基础仓库 package com.spring.generic.di; public class BaseRepository<T> { } 2.定义基础服务层 package c ...

  4. Spring 泛型依赖注入

    BaseService<T>:有RoleService和UserService两的子类 BaseRepepositry<T>:有UserRepository和RoleRepos ...

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

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

  6. Spring(十六):泛型依赖注入

    简介: Spring4.X之后开始支持泛型依赖注入. 使用示例: 1.定义实体 package com.dx.spring.bean.componentscan; import java.io.Ser ...

  7. Spring.NET依赖注入框架学习-- 泛型对象的创建和使用

    Spring.NET依赖注入框架学习-- 泛型对象的创建和使用 泛型对象的创建方法和普通对象是一样的. 通过构造器创建泛型对象 下面是一个泛型类的代码: namespace GenericsPlay ...

  8. Spring的泛型依赖注入

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

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

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

随机推荐

  1. 【Bootstrap】 框架 栅格布局系统设计原理

    前提条件(Bootstrap 自带) 首先使用这个布局之前要定义一下代码: 这行代码如果不懂,可以搜索一下,总之大致意思就是,被定义的元素的内边距和边框不再会增加它的宽度,不加入的话排版会有问题. 不 ...

  2. 2019年12月12日英语学习-Will I Or Won't I ?For Since

    没办法,听不懂,记不住.就会一句.艹

  3. NX二次开发-UFUN更改视图比例大小UF_DRAW_set_view_scale

    #include <uf.h> #include <uf_draw.h> #include <uf_drf.h> #include <uf_obj.h> ...

  4. Java--会移动、反弹的球

    package firstpack; import java.awt.*; public class MyStar { public static void main(String[] args) { ...

  5. 阿里云ecs(phpstudy一件包)

            选择语言       保存并连接    Linux硬盘挂载是比较常见的管理操作之一.默认情况下数据盘没有挂载,需要手动挂载到系统中.     具体操作是分三步:     硬盘挂载1)需 ...

  6. hexo next主题深度优化(七),cdn加速。

    文章目录 注: 正题: 免费cdn 收费cdn 个人博客:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io ...

  7. super 关键字的使用及说明

    super 关键字主要用于访问父类的变量和方法. 代码示例: public class Student { String name; public Student(){ System.out.prin ...

  8. Vue.js中的图片引用路径问题

    当我们在Vue.js项目中引用图片时,关于图片路径有以下几种情形: 使用一: 在data里面定义好图片路径: /*错误写法*/ imgUrl:'../assets/logo.png' 在templat ...

  9. kettle 中 java.lang.ClassCastException: [B cannot be cast to java.lang.String报错的解决方法

    问题描述:从数据库中查询出的某字段是json类型数据,然后在json输入步骤报错java.lang.ClassCastException: [B cannot be cast to java.lang ...

  10. 在CentOS6上安装mysql5.7报错

    报错截图: 处理方法: # yum install numactl perl -y