在父类中建立关系 (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. 使用Python3.x抓取58同城(南京站)的演出票的信息

    #!/usr/bin/env python #-*-coding: utf-8 -*- import re import urllib.request as request from bs4 impo ...

  2. faiss CPU版本+GPU版本安装

    faiss安装 faiss是facebook开发的有CPU版本和GPU版本的求密集向量相似性和进行密集向量聚类的库. faiss用c++编写,安装faiss需要在github上下载其c++源码并用ma ...

  3. JFinal Web开发学习(六)验证码验证和注册细节

    效果: 实现了注册界面的验证码验证.确认密码.密码md5加盐加密.C3P0插件数据库操作.读取外部配置文件. 1.在注册页面添加了确认密码输入框,修改了字段名称 <!DOCTYPE html&g ...

  4. 【转】我为什么把think in java 读了10遍

    我在想写这篇博文之前,就曾经对我媳妇(她是做web前端的)讲,我把think in java看了几次几次,媳妇那时就用很羡慕和莫名的眼神看着我说,你真有毅力,我当时就蒙了,我以为她会说,你现在基础一定 ...

  5. PAT 1024 科学计数法 (20)(精简版代码+思路+推荐测试样例)

    1024 科学计数法 (20)(20 分) 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+, ...

  6. hdu 1175(BFS&DFS) 连连看

    题目在这里:http://acm.hdu.edu.cn/showproblem.php?pid=1175 大家都很熟悉的连连看,原理基本就是这个,典型的搜索.这里用的是广搜.深搜的在下面 与普通的搜索 ...

  7. gdal source code c++ make windows

    下载源码 GDAL源代码下载地址:http://trac.osgeo.org/gdal/wiki/DownloadSource,或者安装svn从源代码服务器下载,svn地址是:http://svn.o ...

  8. appache压力测试

    apache自带压力工具测试说明: Usage: ab [options] [http[s]://]hostname[:port]/pathOptions are: -n requests Numbe ...

  9. invalid END header解决方法

    我在Windows上的eclipse开发了一个java web项目,然后压缩成war包,通过ftp发送到Linux服务器上,Tomcat先shutdown,再startup.按理说,会在webapps ...

  10. Selenium+PhantomJS

    Selenium Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动操作,不同是Selenium 可以直接运行在浏览器上, ...