Spring初学之泛型依赖注入
主要讲泛型依赖注入,所以核心在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初学之泛型依赖注入的更多相关文章
- Spring基础—— 泛型依赖注入
		
一.为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入. 二.泛型依赖注入:子类之间的依赖关系由其父类泛型以及父类之 ...
 - Spring(十六):泛型依赖注入
		
简介: Spring4.X之后开始支持泛型依赖注入. 使用示例: 1.定义实体 package com.dx.spring.bean.componentscan; import java.io.Ser ...
 - 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组件的方法,我们就 ...
 - 回客科技 面试的 实现ioc 容器用到的技术,简述BeanFactory的实现原理,大搜车面试的 spring 怎么实现的依赖注入(DI)
		
前言:这几天的面试,感觉自己对spring 的整个掌握还是很薄弱.所以需要继续加强. 这里说明一下spring的这几个面试题,但是实际的感觉还是不对的,这种问题我认为需要真正读了spring的源码后说 ...
 
随机推荐
- M²的经典语录
			
1. If you failed, stop and think! You should work in the correct way. 2. If I can do all of it, why ...
 - EasyNVR智能云终端硬件使用说明(EasyNVR无插件直播服务硬件的具体使用方法)
			
问题背景 随着EasyNVR硬件版本(EasyNVR硬件云终端)的发布不少客户选择了EasyNVR云终端作为产品选择,在客户收到EasyNVR云终端的时候肯定都有一个疑问,那就是如何使用手头上的这个小 ...
 - Quality of service
			
w https://en.wikipedia.org/wiki/Quality_of_service Quality of service (QoS) is the overall performan ...
 - nosql_action
			
ps -aux 查当前端口占用 connecting to: test > show dbs local .078125GB testphp .203125GB > use testph ...
 - 内置函数: zip 用法
			
描述 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表. 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符 ...
 - pycharm中导入自写模块时,模块下出现红线
			
问题描述: 在pycharm中导入自己写的模块时,得不到智能提示,并在模块名下出现下红线,但是代码可以执行,错误提示为下图所示: 原因:出现 以上情况,是因为文件目录设置的问题,pycharm中的最上 ...
 - 转载:http://blog.csdn.net/foruok/article/details/53500801
			
凭兴趣求职80%会失败,为什么 标签: 求职跳槽找工作兴趣技术 2016-12-07 06:51 43316人阅读 评论(69) 收藏 举报 本文章已收录于: 分类: 随笔(144) 作者同类文章 ...
 - [转载]威力导演14旗舰破解版(中文简体)|取消30天限制CyberLink&nb
			
2015月9月15日(当地时间),CyberLink讯连科技发布新一代视频编辑软件 — PowerDirector威力导演14,融合了上个版本发布以来的多次更新升级,威力导演依旧 ...
 - java中byte数组与int,long,short间的转换
			
http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * <l ...
 - HAProxy详解
			
HAProxy概述与配置 一.HAProxy概述 HAProxy是由 WillyTarreau开发的一款具备高可用性.负载均及基于 TCP和 HTTP的应用代理开源软件,基于HAProxy的负载均衡架 ...