在父类中建立关系 (spring4.x以上版本)

package com.spring.annotation.generic;

import org.springframework.beans.factory.annotation.Autowired;

public class BaseService<T> {

    @Autowired
private BaseRepository<T> dao; public void add(T entity){
System.out.println("add by " + dao);
dao.save(entity);
} } package com.spring.annotation.generic; public class BaseRepository<T> { public void save(T entity){
System.out.println("save:" + entity);
} }

子类

package com.spring.annotation.generic;

import org.springframework.stereotype.Service;

@Service
public class UserService extends BaseService<User>{ } package com.spring.annotation.generic; import org.springframework.stereotype.Repository; @Repository
public class UserDao extends BaseRepository<User>{ }

测试

package com.spring.annotation.generic;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml"); UserService userService = (UserService) ctx.getBean("userService");
userService.add(new User()); } }

结果

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

  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. avalon子孙元素属性监听

    HTML正文: <body ms-controller="ex"> <div class="ms-hover" ms-click=" ...

  2. DNS的过程

    来自:https://www.zhihu.com/question/23042131 作者:郭无心链接:https://www.zhihu.com/question/23042131/answer/6 ...

  3. 瑞波Ripple-OnlineDeletion配置的理解

    1.概要 如果没有设置online deletion,那么Rippled Server的本地数据库大小将会无限增长.为了释放磁盘空间,那种情况应该被制止并且那些数据库文件应该被手动删除.online ...

  4. winform closing事件注册

    参考链接:http://blog.chinaunix.net/uid-215617-id-2213081.html

  5. istio 服务地图

    1.安装 kubectl apply -f install/kubernetes/addons/servicegraph.yam 2.查看安装是否成功kubectl -n istio-system g ...

  6. 1、javaweb学习之配置文件web.xml

    今天这里主要讲述javaweb中的配置文件web.xml中的内容及其作用,都是基础部分,对于初学者需要好好掌握理解. 简单配置: <servlet>    <servlet-name ...

  7. [线段树]picture

    PICTURE 题目描述 N(N<5000) 张矩形的海报,照片和其他同样形状的图片贴在墙上.它们的边都是垂直的或水平的.每个矩形可以部分或者全部覆盖其他矩形.所有的矩形组成的集合的轮廓称为周长 ...

  8. 第一个spring boot 程序

    安装.运行.预览省略 错误1:8080端口被IIS占用,关闭它 Description: The Tomcat connector configured to listen on port 8080 ...

  9. h5解决移动端上滑卡顿问题

    select{ -webkit-overflow-scrolling: touch;/*解决移动端滑动卡顿问题*/ -webkit-transform: translateZ(0px);/*开启GPU ...

  10. MS SQMServer2008R2 连接不到远程服务的解决办法

    问题: MS SQMServer2008R2 连接不到远程服务的解决办法.程序提示的错误如下: [2017/02/19 17:46:21] 在与 SQL Server 建立连接时出现与网络相关的或特定 ...