Spring(十六):泛型依赖注入
简介:
Spring4.X之后开始支持泛型依赖注入。
使用示例:
1、定义实体
package com.dx.spring.bean.componentscan;
import java.io.Serializable;
public class Member implements Serializable {
private static final long serialVersionUID = -7106886149424419957L;
}
package com.dx.spring.bean.componentscan;
import java.io.Serializable;
public class Role implements Serializable{
private static final long serialVersionUID = 514142692576163383L;
}
2、定义基础仓库
package com.dx.spring.bean.componentscan;
import java.io.Serializable;
public class BaseRepository<T extends Serializable> {
public void add() {
System.out.println("BaseRepository add");
}
}
3、定义基础服务层
package com.dx.spring.bean.componentscan;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
public class BaseService<T extends Serializable> {
@Autowired
protected BaseRepository<T> baseRepository;
public void add() {
System.out.println("BaseService add ");
}
}
4、定义仓库服务层
package com.dx.spring.bean.componentscan; import org.springframework.stereotype.Repository; @Repository
public class MemberRepositoryImpl extends BaseRepository<Member> {
@Override
public void add() {
System.out.println("Add");
}
}
package com.dx.spring.bean.componentscan; import org.springframework.stereotype.Repository; @Repository
public class RoleRepositoryImpl extends BaseRepository<Role> { }
5、定义Member/Role服务层
package com.dx.spring.bean.componentscan; import org.springframework.stereotype.Service; @Service
public class MemberServiceImpl extends BaseService<Member> {
@Override
public void add() {
System.out.println(baseRepository);
super.add();
}
}
package com.dx.spring.bean.componentscan; import org.springframework.stereotype.Service; @Service
public class RoleServiceImpl extends BaseService<Role> {
@Override
public void add() {
System.out.println(baseRepository);
super.add();
}
}
6、测试类
package com.dx.spring.bean.componentscan; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-component-scan2.xml");
MemberServiceImpl memberService = (MemberServiceImpl) ctx.getBean("memberServiceImpl");
memberService.add(); RoleServiceImpl roleServiceImpl = (RoleServiceImpl) ctx.getBean("roleServiceImpl");
roleServiceImpl.add();
}
}
打印日志:
com.dx.spring.bean.componentscan.MemberRepositoryImpl@530612ba
BaseService add
com.dx.spring.bean.componentscan.RoleRepositoryImpl@2a40cd94
BaseService add
Spring(十六):泛型依赖注入的更多相关文章
- Spring初学之泛型依赖注入
主要讲泛型依赖注入,所以核心在java文件,配置文件中只需配置扫描包即可,如下: <?xml version="1.0" encoding="UTF-8" ...
- Spring第六弹—-依赖注入之使用构造器注入与使用属性setter方法注入
所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中. 使用构造器注入 1 2 3 4 <constructor-arg index=“0” type=“java.lang. ...
- Spring基础—— 泛型依赖注入
一.为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入. 二.泛型依赖注入:子类之间的依赖关系由其父类泛型以及父类之 ...
- Spring的泛型依赖注入
Spring 4.x 中可以为子类注入子类对应的泛型类型的成员变量的引用,(这样子类和子类对应的泛型类自动建立关系)具体说明: 泛型注入:就是Bean1和Bean2注入了泛型,并且Bean1和Bean ...
- 【串线篇】spring泛型依赖注入原理
spring泛型依赖注入原理 不管三七二十一 servlet :加注解@servlet service:加注解@service dao:加注解@Repository 这相当于在容器中注册这些个类
- Spring新特性_泛型依赖注入
泛型依赖注入 package com.tanlei.spring.generic; import org.springframework.beans.factory.annotation.Autowi ...
- 转载--浅谈spring4泛型依赖注入
转载自某SDN-4O4NotFound Spring 4.0版本中更新了很多新功能,其中比较重要的一个就是对带泛型的Bean进行依赖注入的支持.Spring4的这个改动使得代码可以利用泛型进行进一步的 ...
- Spring4学习回顾之路10-Spring4.x新特性:泛型依赖注入
泛型依赖注入:Spring 4.x中可以为子类注入子类对应的泛型类型的成员变量的引用. 话语太过抽象,直接看代码案例,依次建立如下代码: User.java package com.lql.sprin ...
- (转)Spring读书笔记-----Spring核心机制:依赖注入
Java应用(从applets的小范围到全套n层服务端企业应用)是一种典型的依赖型应用,它就是由一些互相适当地协作的对象构成的.因此,我们说这些对象间存在依赖关系.加入A组件调用了B组件的方法,我们就 ...
随机推荐
- HTML5中的跨文档消息传递
跨文档消息传送(cross-document messaging),有时候也简称为XDM,指的是来自不同域的页面间传递消息.例如,www.w3cmm.com域中的一个页面与一个位于内嵌框架中的p2p. ...
- .net下的span和memory
.net core 2.1的重头戏就是性能,其中最重要的两个类就是span和memory,本文这里简单的介绍一下这两个类的使用. 什么是 Span<T> Span<T> 是新一 ...
- flashsim配置2015最新版本
http://blog.csdn.net/Eidosper/article/details/46458469 http://www.doc88.com/p-7314277317013.html htt ...
- jquery智能弹出层,自己主动推断位置
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ASP.NET Identity系列02,在ASP.NET MVC中增删改查用户
本篇体验在ASP.NET MVC中使用ASP.NET Identity增删改查用户. 源码在这里:https://github.com/darrenji/UseIdentityCRUDUserInMV ...
- cocos2d-x系列笔记技巧篇(2)---关于CREATE_FUNC宏的用法
FROM://http://blog.csdn.net/jinciyulang/article/details/8631889 阅读cocos2d-x demo的代码,我们会看到有些头文件中使用CRE ...
- dotnetty 心跳
IdleStateHandler 需要提供三个参数: readerIdleTimeSeconds, 读超时. 即当在指定的事件间隔内没有从 Channel 读取到数据时, 会触发一个 READER_I ...
- Git:基础要点
直接快照,而非比较差异. 近乎所有操作都可本地执行. 在Git 中的绝大多数操作都只需要访问本地文件和资源,不用连网.但如果用CVCS 的话,差不多所有操作都需要连接网络.因为Git 在本地磁盘上就保 ...
- java.lang.IllegalStateException: The remote endpoint was in state [TEXT_FULL_WRITING] which is an invalid state for called method 解决办法
java.lang.IllegalStateException: The remote endpoint was in state [TEXT_FULL_WRITING] which is an in ...
- 每天一个linux命令-curl命令
下载文件 如果我们想要下载文件,而不是查看,那么可以使用如下命令: curl -O http://mif.polimercolor.ru/mifsoft/MDict.zip 以上命令会下载文件并以原名 ...