简介:

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(十六):泛型依赖注入的更多相关文章

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

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

  2. Spring第六弹—-依赖注入之使用构造器注入与使用属性setter方法注入

    所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中. 使用构造器注入   1 2 3 4 <constructor-arg index=“0” type=“java.lang. ...

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

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

  4. Spring的泛型依赖注入

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

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

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

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

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

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

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

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

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

  9. (转)Spring读书笔记-----Spring核心机制:依赖注入

    Java应用(从applets的小范围到全套n层服务端企业应用)是一种典型的依赖型应用,它就是由一些互相适当地协作的对象构成的.因此,我们说这些对象间存在依赖关系.加入A组件调用了B组件的方法,我们就 ...

随机推荐

  1. rails 数据迁移 -migration

    1.创建一个fruits 项目: rails new fruits -d mysql --skip-bundle 2.修改Gemfile: source 'https://gems.ruby-chin ...

  2. Cocos2d-x 3.0游戏开发之虚拟机IOS环境:匹配才是好,莫要随便升级软件

    尊重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/34110449 做为一个买不起MAC的Coder,仅 ...

  3. 阿里云esc云服务器IP不能访问的解决办法

    问题:阿里云服务器,专有网络,web设置完毕,在服务器中localhost能够访问,并且关闭防火墙,但是使用公网ip无法访问. 解决:找到本实例安全组,配置规则,按照要求填入80或其他端口.配置完成后 ...

  4. [廖雪峰] Git 分支管理(1):创建与合并分支(HEAD、master、dev、指针)

    每次提交,Git 都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条时间线,在 Git 里,这个分支叫主分支,即 master 分支.HEAD 严格来说不是指向提交,而是指向 mas ...

  5. 飘逸的python - 中文编码长度有趣的现象

    最近在做验证用户姓名的功能时发现这样一个现象.   >>len(u'打怪者') #unicode 3 >>len(u'打怪者'.encode('gbk')) #gbk 6 &g ...

  6. Win10正式版开机慢怎么办 开机黑屏时间长怎么办

    升级Win10正式版后开机速度慢.黑屏时间长怎么解决呢?其实我重要是由Win10正式版所提供的“快速启动”功能与电脑显卡驱动.电源管理驱动不兼容所造成的.下面就与大家分享一下针对Win10正式版开机速 ...

  7. AngularJS自定义Directive不一定返回对象

    AngularJS中,当需要自定义Directive时,通常返回一个对象,就像如下的写法: angular.module('modulename') .directive('myDirective', ...

  8. 使用HttpClient对ASP.NET Web API服务实现增删改查

    本篇体验使用HttpClient对ASP.NET Web API服务实现增删改查. 创建ASP.NET Web API项目 新建项目,选择"ASP.NET MVC 4 Web应用程序&quo ...

  9. Idea2018激活破解

    原文:https://www.imsxm.com/2018/07/idea-2018-1-5-crack-patcher.html 最近更新了Intellij IDEA到2018.1.5之后,使用之前 ...

  10. C#高级编程小结

    小结 这几章主要介绍了如何使用新的dynamic类型,还讨论了编译器在遇到dynamic类型时会做什么.还讨论了DLP,可以把它包含在简单的应用程序中.并通过Pythin使用DLR,执行Python脚 ...