spring4新特性-泛型依赖注入
1 文件结构
2 具体类
2.1两个抽象类,在Service里面写公共的方法,在各自的具体实现类里面写各自的方法
package repo; import model.User; /**
* Created by pinker on 2016/10/30.
*/
public abstract class Repository<T> {
public abstract T getAllUser() throws Exception;
}
package service; import model.User;
import org.springframework.beans.factory.annotation.Autowired;
import repo.Repository; /**
* Created by pinker on 2016/10/30.
*/
public abstract class Service<T>{
@Autowired
private Repository<T> repository; public abstract T getAllUser() throws Exception; public void save()throws Exception{
System.out.println("Service----------->"+repository);
}
}
2.2 实现类
package repoimpl; import model.User;
import repo.Repository;
/**
* Created by pinker on 2016/10/30.
*/
@org.springframework.stereotype.Repository
public class UserRepository extends Repository<User> {
@Override
public User getAllUser() throws Exception {
User user = new User(1, "HS", 23);
return user;
}
}
package serviceimpl; import model.User;
import org.springframework.beans.factory.annotation.Autowired;
import repoimpl.UserRepository;
import service.Service; /**
* Created by pinker on 2016/10/30.
*/
@org.springframework.stereotype.Service
public class UserService extends Service<User> {
@Autowired
private UserRepository userRepository; @Override
public User getAllUser() throws Exception {
return userRepository.getAllUser();
}
}
2.3 测试类(偷懒了,因为只用了两个包,不是测试的写法)
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import serviceimpl.UserService; /**
* Created by pinker on 2016/10/30.
*/ public class test {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/Spring.xml");
UserService userService = (UserService) context.getBean("userService");
userService.save();
System.out.println(userService.getAllUser());
}
}
2.4 结果
可以看出,里面既可以调用公共方法也可以调用自己特有的实现类及自己的方法
spring4新特性-泛型依赖注入的更多相关文章
- Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC 配置校验器
Spring4新特性——泛型限定式依赖注入 Spring4新特性——核心容器的其他改进 Spring4新特性——Web开发的增强 Spring4新特性——集成Bean Validation 1.1(J ...
- Spring4学习回顾之路10-Spring4.x新特性:泛型依赖注入
泛型依赖注入:Spring 4.x中可以为子类注入子类对应的泛型类型的成员变量的引用. 话语太过抽象,直接看代码案例,依次建立如下代码: User.java package com.lql.sprin ...
- Spring框架入门之Spring4.0新特性——泛型注入
Spring框架入门之Spring4.0新特性——泛型注入 一.为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入. ...
- Spring新特性_泛型依赖注入
泛型依赖注入 package com.tanlei.spring.generic; import org.springframework.beans.factory.annotation.Autowi ...
- 转载--浅谈spring4泛型依赖注入
转载自某SDN-4O4NotFound Spring 4.0版本中更新了很多新功能,其中比较重要的一个就是对带泛型的Bean进行依赖注入的支持.Spring4的这个改动使得代码可以利用泛型进行进一步的 ...
- Spring4新特性简述
Spring是一个java世界中极其流行 的开源框架.Spring的初衷是降低企业级开发的复杂性,并试图通过POJO对象实现之前EJB这类重型框架才能实现的功能.Spring不仅仅对服务 端开发有用, ...
- Spring(十六):泛型依赖注入
简介: Spring4.X之后开始支持泛型依赖注入. 使用示例: 1.定义实体 package com.dx.spring.bean.componentscan; import java.io.Ser ...
- Spring基础—— 泛型依赖注入
一.为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入. 二.泛型依赖注入:子类之间的依赖关系由其父类泛型以及父类之 ...
- Java JDK5新特性-泛型
2017-10-30 22:47:11 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 泛型的本质 ...
随机推荐
- Linux文件类型介绍
文件类型介绍: Linux系统不同于Windows系统,两者文件类型和文件扩展名也有很大的差异.Linux中的文件类型和Linux文件的文件扩展名所代表的意义和Windows系统完全不同.用户一般通过 ...
- scala PartialFunction
1.orElse和andThen的区别 源码如下,区别很明显,orElse是并列的关系,而andThen是调用者的结果作为k的输入. trait PartialFunction[-A, +B] ext ...
- Django中添加富文本编辑器
使用的是CKeditor这个模块 1.安装: pip install django-ckeditor 2.将ckeditor注册到settings.py文件中, 并添加ckeditor的url到你项目 ...
- HTML之头部标签
<header> head 元素元素包含了所有的头部标签元素 可以添加在头部区域的元素标签为: title,style,meta,link,script,noscript,base < ...
- win10 uwp Window.Current.Dispatcher中Current为null
本文说的是进行网络中异步界面出现的错误,可能带有一定的主观性和局限性,说的东西可能不对或者不符合每个人的预期.如果觉得我有讲的不对的,就多多包含,或者直接关掉这篇文章,但是请勿生气或者发怒吐槽,可以在 ...
- Struts2 xml 详解
<!-- include节点是struts2中组件化的方式 可以将每个功能模块独立到一个xml配置文件中 然后用include节点引用 --> <include file=" ...
- 安装debian 9.1后,中文环境下将home目录下文件夹改为对应的英文
安装了debian 9.1后,中文环境下home目录下文件夹显示的是中文,相当不方便cd命令,改为对应的英文吧,需要用到的软件xdg-user-dirs-gtk #安装需要的软件 sudo apt i ...
- 备忘:有MTU值设置不当导致的部分网站无法访问问题
如题,有时候突然weibo.com,webQQ等网站网络连接超时,怎么找也没得原因,今天管理电信的光猫,发现设置的MTU的1400,突然想起之前电脑和路由器上设置的MTU是1500,感觉可能是这个问题 ...
- java swing中Timer类的学习
最近在完成学校课程的java平时作业,要实现一个计时器,包含开始.暂停以及重置三个功能.由于老师规定要用这个timer类,也就去学习了一下,顺便记录一下. 首先呢去查了一下java手册上的东西,发现t ...
- win8在安装office visio2003出现“请求的操作需要提升”,解决方法
单击右键,然后以管理员身份运行即可