主要讲泛型依赖注入,所以核心在java文件,配置文件中只需配置扫描包即可,如下:

<?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.3.xsd"> <context:component-scan base-package="spring.beans.generic.di"></context:component-scan> </beans>
spring.beans.generic.di包中有两个配置的泛型的父类,有两个继承这两个父类的子类,还有一个实体bean,先看实体bean:
由于只是模拟一下,没必要写属性,创建一个类即可。
package spring.beans.generic.di;

public class User {

}

再看两个泛型的父类:

package spring.beans.generic.di;

public class BaseRepository<T> {

}
package spring.beans.generic.di;

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

public class BaseService<T> {
@Autowired
protected BaseRepository<T> repository;
public void add(){
System.out.println("add...");
System.out.println(repository);
}
}

子类:

package spring.beans.generic.di;

import org.springframework.stereotype.Repository;

@Repository
public class UserRepository extends BaseRepository<User>{ }
package spring.beans.generic.di;

import org.springframework.stereotype.Service;

@Service
public class UserService extends BaseService<User> { }

测试:

package spring.beans.generic.di.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.beans.generic.di.UserService; public class Main { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-generic-di.xml");
UserService userService=(UserService) ctx.getBean("userService");
userService.add();//输出的是子类的对象
} }

输出:

add...
spring.beans.generic.di.UserRepository@8646db9

Spring初学之泛型依赖注入的更多相关文章

  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. 【串线篇】spring泛型依赖注入原理

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

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

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

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

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

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

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

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

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

  9. 回客科技 面试的 实现ioc 容器用到的技术,简述BeanFactory的实现原理,大搜车面试的 spring 怎么实现的依赖注入(DI)

    前言:这几天的面试,感觉自己对spring 的整个掌握还是很薄弱.所以需要继续加强. 这里说明一下spring的这几个面试题,但是实际的感觉还是不对的,这种问题我认为需要真正读了spring的源码后说 ...

随机推荐

  1. [HAOI2012] 容易题[母函数]

    794. [HAOI2012] 容易题 ★★☆   输入文件:easy.in   输出文件:easy.out   简单对比时间限制:1 s   内存限制:128 MB 秒 输入:easy.in 输出: ...

  2. mongodb安全配置

    1. 为数据库增加管理员 use admin db.createUser({ >user:'userName', pwd:'password', roles:[{role:'userAdminA ...

  3. Python迭代器包itertools(转)

    原文:http://www.cnblogs.com/vamei/p/3174796.html 作者:Vamei 在循环对象和函数对象中,我们了解了循环器(iterator)的功能.循环器是对象的容器, ...

  4. Json模块的详细介绍(序列化)

    什么叫序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 比如,我们在python代码中计算的一个数据需要给另外一段程序使用,那我们怎么给? 现在我们能想到的方法就是存在文件里,然 ...

  5. 创建spring boot 项目所遇到的问题

    1.添加完MySQL和jdbc约束后,在配置文件内spring.datasource.driver-class-name=com.mysql.jdbc.Driver 报错,显示找不到驱动包,原因是: ...

  6. 备注字段长度控制JS

    //变更原因备注字符长度控制 function checkChangeLength() { var field = $("#changeReasonDesc").val(); ma ...

  7. id函数

    描述 id() 函数用于获取对象的内存地址. 语法 id 语法: id([object]) 参数说明: object -- 对象. 返回值 返回对象的内存地址. 实例 以下实例展示了 id 的使用方法 ...

  8. VC6.0中添加库文件和头文件

    附加头文件包含 VC6.0中: VC6.0默认include包含路径:Tools>Options>Directories>Include files. 对于特定项目的头文件包含,在“ ...

  9. 学好 Python 的 11 个优秀资源

    Python是目前最流行.最易学最强大的编程语言之一,无论你是新手还是老鸟,无论是用于机器学习还是web开发(Pinterest就是案例),Python都是一件利器.此外,Python不但人气日益高涨 ...

  10. BCM 交换机开发

    转:http://blog.chinaunix.net/uid-23782786-id-3839602.html 前言:        最近搞这玩样,真是折腾,网上的资料都是片段,而且很少.折腾了4. ...