Spring 4.x中可以为子类注入子类对应的泛型类型的成员变量的引用

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

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

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

package generic.di; 

public class BaseRepository<T> { } 

————————————————————————————————————————————————————————————————————————————————————————————————————————————————
package generic.di; import org.springframework.beans.factory.annotation.Autowired; public class BaseService<T> { @Autowired
protected BaseRepository<T> repository; public void add() {
System.out.println("add");
System.out.println(repository);
}
}
package generic.di;

import org.springframework.stereotype.Repository;

@Repository
public class RoleRepository extends BaseRepository<Organization> {
}

—————————————————————————————————————————————————————————————————————————————————————————————————————————————— package generic.di;
import org.springframework.stereotype.Service; @Service
public class RoleService extends BaseService<Organization> {
}

——————————————————————————————————————————————————————————————————————————————————————————————————————————————
package generic.di; public class Organization {
}
package generic.di;

public class User {
}
——————————————————————————————————————————————————————————————————————————————————————————————————————————————
package generic.di; import org.springframework.stereotype.Repository; @Repository
public class UserRepository extends BaseRepository<User> { } ——————————————————————————————————————————————————————————————————————————————————————————————————————————————
package generic.di; import org.springframework.stereotype.Service; @Service
public class UserService extends BaseService<User> {
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="generic.di">
</context:component-scan>
</beans>
package generic.di;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("generic/di/15-1.xml");
UserService userService = (UserService) ctx.getBean("userService");
RoleService roleService = (RoleService) ctx.getBean("roleService");
userService.add();
roleService.add();
}
}

aaarticlea/png;base64,iV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ybVBi4vGRusV80V/Pj/us+c3+Zqz01br4midr/KcStvL8SnOF69SdsGPEACUgjgBlII4AZSCOAGU8i/DEsrHFnJZNgAAAABJRU5ErkJggg==" alt="" />

15Spring泛型依赖注入的更多相关文章

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

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

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

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

  3. Spring的泛型依赖注入

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

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

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

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

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

  6. Autofac 泛型依赖注入

    using Autofac;using Autofac.Extensions.DependencyInjection;using Hangfire;using Microsoft.AspNetCore ...

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

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

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

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

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

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

随机推荐

  1. node.js在读取文件时中文乱码问题

    断更很久了........从今天开始会努力的持续更博,积极学习. 言归正传.今天在写node.js的demo时发现一个bug.我在node中读取本地的text文件时,发现英文的内容可以被读取,但是中文 ...

  2. notepad++无法设置成默认打开方式

    安装软件自动保存到默认的目录下(c盘下)

  3. bind: Invalid argument

    出现此问题在于,listen函数在socket函数和bind函数之间. 例: /*客户端程序开始建立sockfd描述符*/ listenfd = socket(AF_INET,SOCK_STREAM, ...

  4. bzoj2333[SCOI2011]棘手的操作 洛谷P3273 [SCOI2011]棘手的操作

    2333? 先记一下吧,这题现在全部都是照着题解做的,因为怎么改都改不出来,只好对着题解改,以后还要再做过 以后再也不用指针了!太恶心了!空指针可不止直接特判那么简单啊,竟然还要因为空指针写奇怪的分类 ...

  5. 使用Apache Commons IO组件读取大文件

    Apache Commons IO读取文件代码如下: Files.readLines(new File(path), Charsets.UTF_8); FileUtils.readLines(new ...

  6. Android 线程池系列教程(5)与UI线程通信要用Handler

    Communicating with the UI Thread 上一课 下一课 1.This lesson teaches you to Define a Handler on the UI Thr ...

  7. Android内存堆上限Android的缺省值是16M(某些机型是24M)

    转自: http://www.cnblogs.com/jacktu/archive/2010/12/30/1921475.html 大家都知道Android的上层应用是基于 Dalvik Virtua ...

  8. Access2010 - 数据类型[转]

    原文链接 Access允许十种数据类型:文本.备注.数值.日期/时间.货币.自动编号.是/否.OLE对象.超级链接.附件.查询向导 . 文本(Text):这种类型允许最大255个字符或数字,Acces ...

  9. 安装CentOS--设置网络_1

    (1)在登录黑框中输入如下命令,让CentOS 7自动获取一个IP地址: # dhclient (2)正常情况下不会有任何输出内容.用如下命令查看获取到的IP地址: # ip addr 它将返回如图所 ...

  10. Android应用开发细节点

    1.如果handler是在主线程声明,就属于主线程,handleMessage属于引用handler的那个线程:2.ByteArrayOutputStream/ByteArrayInputStream ...