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新特性-泛型依赖注入的更多相关文章

  1. Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC 配置校验器

    Spring4新特性——泛型限定式依赖注入 Spring4新特性——核心容器的其他改进 Spring4新特性——Web开发的增强 Spring4新特性——集成Bean Validation 1.1(J ...

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

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

  3. Spring框架入门之Spring4.0新特性——泛型注入

    Spring框架入门之Spring4.0新特性——泛型注入 一.为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入. ...

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

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

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

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

  6. Spring4新特性简述

    Spring是一个java世界中极其流行 的开源框架.Spring的初衷是降低企业级开发的复杂性,并试图通过POJO对象实现之前EJB这类重型框架才能实现的功能.Spring不仅仅对服务 端开发有用, ...

  7. Spring(十六):泛型依赖注入

    简介: Spring4.X之后开始支持泛型依赖注入. 使用示例: 1.定义实体 package com.dx.spring.bean.componentscan; import java.io.Ser ...

  8. Spring基础—— 泛型依赖注入

    一.为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入. 二.泛型依赖注入:子类之间的依赖关系由其父类泛型以及父类之 ...

  9. Java JDK5新特性-泛型

    2017-10-30 22:47:11 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 泛型的本质 ...

随机推荐

  1. 强大的grep,sed和awk--用案例来讲解

    准备工作: 先简单了解grep,sed和awk功能 1) grep 显示匹配特定模式的内容 grep -v 'boy' test.txt 过滤掉test.txt文件的boy,显示其余内容 grep ' ...

  2. 谦先生的程序员日志之我的hadoop大数据生涯一

    从一个初级程序员到高级程序员的经历 你好!我是谦先生,我是茫茫程序猿中的一猿,平凡又执着. 刚入行的时候说实话,啥都不懂,就懂点皮毛的java,各种被虐狗的感觉.又写js又写css又写后台...慢慢被 ...

  3. Vue.js2.0中的变化(持续更新中)

    最近自己在学习Vue.js,在看一些课程的时候可能Vue更新太块了导致课程所讲知识和现在Vue的版本不符,从而报错,我会在以后的帖子持续更新Vue的变化与更新,大家也可以一起交流,共同监督学习! 1. ...

  4. 【转】深入浅出:Linux设备驱动之字符设备驱动

    深入浅出:Linux设备驱动之字符设备驱动 一.linux系统将设备分为3类:字符设备.块设备.网络设备.使用驱动程序: 字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据 ...

  5. LINUX 笔记-文件属性相关命令

    chgrp:该命令用于改变文件所属用户组 chown:该命令用于改变文件的所有者 chmod:该命令用于改变文件的权限 -R:进行递归的持续更改,即连同子目录下的所有文件都会更改  

  6. 栈(存储结构链表)--Java实现

    /*用链表实现栈--链栈 * */ public class MyLinkedStack { public MyLinkedList linklist; int items; public MyLin ...

  7. java加密解密

    java加密解密 public class MD5Util { /** * @param args */ public static void main(String[] args) { System ...

  8. LeetCode 476. Number Complement (数的补数)

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  9. LeetCode 73. Set Matrix Zeros(矩阵赋零)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...

  10. 对Java原子类AtomicInteger实现原理的一点总结

    java原子类不多,包路径位于:java.util.concurrent.atomic,大致有如下的类: java.util.concurrent.atomic.AtomicBoolean java. ...