Spring的泛型依赖注入
Spring 4.x 中可以为子类注入子类对应的泛型类型的成员变量的引用,(这样子类和子类对应的泛型类自动建立关系)
具体说明:
泛型注入:就是Bean1和Bean2注入了泛型,并且Bean1和Bean2建立依赖关系,这样子类Bean3(继承bean1)和bean4(继承bean2)就会自动建立关系
不是泛型注入:就是说Bean1和Bean2都没有注入泛型,只是建立了关系,子类Bean3(继承bean1)和bean4(继承bean2)也会自动建立关系
泛型依赖注入的实现步骤:
1新建两个泛型类,并建立依赖关系:
具体代码:
BaseRepository:
package com.jeremy.spring.genericityDI;
public class BaseRepository{
}
BaseService:
package com.jeremy.spring.genericityDI;
import org.springframework.beans.factory.annotation.Autowired;
public class BaseService<T> {
@Autowired------//这里告诉IOC容器自动装配有依赖关系的Bean
protected BaseRepository baseRepository;--------//这里是子类继承依赖关系
public void add(){
System.out.println("add..............");
System.out.println(baseRepository);
}
}
2新建一个泛型类:
User:
package com.jeremy.spring.genericityDI;
public class User {
}
3新建BaseRepository和BaseService的子类
UserRepository:
package com.jeremy.spring.genericityDI;
import org.springframework.stereotype.Component; @Component
public class UserRepository extends BaseRepository{ }
UserService:
package com.jeremy.spring.genericityDI; import org.springframework.stereotype.Service; @Service
public class UserService extends BaseService{ }
4:在Spring的配置文件中配置自动装配带有注解的Bean:
<?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-4.0.xsd">
<context:component-scan base-package="com.jeremy.spring.genericityDI"></context:component-scan> </beans>
5测试代码和结果
测试代码:
@Test
public void test() {
ApplicationContext actx=new ClassPathXmlApplicationContext("Bean-genericity-di.xml");
UserService userService=(UserService) actx.getBean("userService");
userService.add();
}
测试结果:
add..............
com.jeremy.spring.genericityDI.UserRepository@16546ef
从结果看,虽然子类没有建立依赖关系,但userRepository实例还是被实例化了,就证明了父类的依赖关系,子类是可以继承的
其实这里也可以说明,就算父类不是被IOC容器管理,但是建立关系时添加了@Autowired注解,父类的关系会被继承下来
题外话:
那泛型注入和不是泛型注入有什么优缺点???二者真正的作用是什么呢??
至于两种不同的方式注入到底有什么优缺点,以后自己在开发中慢慢领悟吧,现在就是把基础弄好
Spring的泛型依赖注入的更多相关文章
- Spring基础—— 泛型依赖注入
一.为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入. 二.泛型依赖注入:子类之间的依赖关系由其父类泛型以及父类之 ...
- Spring学习--泛型依赖注入
暂时没有搞懂.
- 转载--浅谈spring4泛型依赖注入
转载自某SDN-4O4NotFound Spring 4.0版本中更新了很多新功能,其中比较重要的一个就是对带泛型的Bean进行依赖注入的支持.Spring4的这个改动使得代码可以利用泛型进行进一步的 ...
- Spring(十六):泛型依赖注入
简介: Spring4.X之后开始支持泛型依赖注入. 使用示例: 1.定义实体 package com.dx.spring.bean.componentscan; import java.io.Ser ...
- Spring初学之泛型依赖注入
主要讲泛型依赖注入,所以核心在java文件,配置文件中只需配置扫描包即可,如下: <?xml version="1.0" encoding="UTF-8" ...
- 【串线篇】spring泛型依赖注入原理
spring泛型依赖注入原理 不管三七二十一 servlet :加注解@servlet service:加注解@service dao:加注解@Repository 这相当于在容器中注册这些个类
- Spring新特性_泛型依赖注入
泛型依赖注入 package com.tanlei.spring.generic; import org.springframework.beans.factory.annotation.Autowi ...
- 谈谈自己了解的spring.NET的依赖注入
spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection),以达到解耦的目的,实现模块的组件化.程序 ...
- spring.NET的依赖注入
谈谈自己了解的spring.NET的依赖注入 spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection), ...
随机推荐
- 从 "org.apache.hadoop.security.AccessControlException:Permission denied: user=..." 看Hadoop 的用户登陆认证
假设远程提交任务给Hadoop 可能会遇到 "org.apache.hadoop.security.AccessControlException:Permission denied: use ...
- C语言复杂声明解读简明方法
//char (*(*x[3])())[5];//x是什么类型的变量? // //分析C语言声明,关键是搞清楚这个变量是个什么东西(函数.指针.数组), //是函数那么剩下的就是他的参数和返回值, / ...
- plink参数说明
Plink: command-line connection utilityRelease 0.67Usage: plink [options] [user@]host [command] ...
- p4n 今天与朋友沟通支付云服务普及以及跨境电子商务的光辉前景
p4n 今天与朋友沟通支付云服务普及以及跨境电子商务的光辉前景 跨境电子商务也是个光忙四色和的跨境电子商务啊..支付项目也是个强大的项目.. 过几天我们就要宣布正式发布atipay ,并宣称将致力于推 ...
- Atitit. 解决unterminated string literal 缺失引号
Atitit. 解决unterminated string literal 缺失引号 原因:::或许string没使用引号括号起来...missingMessage缺失了一个单个的引号 Error: ...
- mysql 主主复制(双主复制)binlog-do-db
[root@DB ~]# grep "binlog-do-db" /etc/my.cnf binlog-do-db = test [root@DB-S ~]# grep " ...
- web中文字体Font-family应该写什么?
最佳实践是: font-family: Helvetica, Tahoma, Arial, "Microsoft YaHei", "微软雅黑",STXihei, ...
- html5 indexDB的使用
angular.module('indexdb', []) .factory('indexDbJs', [function() { const CurDBVersion = 10000; window ...
- python文件目录操作大全
python只获取当前目录下的文件夹及文件名 list = os.listdir(rootdir)#列出目录下的所有文件和目录 for line in list: filepath = os.path ...
- dp之完全背包poj3181(高精度背包)
这个题目要用到大数的加法,其他的,我没有感觉到有什么难想的......比较水的背包题,掠过..... #include<iostream> #include<stdio.h> ...