Spring 4.0已经发布RELEASE版本,不仅支持Java8,而且向下兼容到JavaSE6/JavaEE6,并移出了相关废弃类,新添加如Java8的支持、Groovy式Bean定义DSL、对核心容器进行增强、对Web框架的增强、Websocket模块的实现、测试的增强等。其中两个我一直想要的增强就是:支持泛型依赖注入、对cglib类代理不再要求必须有空参构造器了。具体更新请参考:

http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/htmlsingle/#new-in-4.0

model类:

  1. package com.dxz.demo.generic.model;
  2.  
  3. import java.io.Serializable;
  4.  
  5. public class Organization implements Serializable {
  6. private Long id;
  7. private String name;
  8. }
  9. package com.dxz.demo.generic.model;
  10.  
  11. import java.io.Serializable;
  12.  
  13. public class User implements Serializable {
  14. private Long id;
  15. private String name;
  16.  
  17. public User(Long id, String name) {
  18. this.id = id;
  19. this.name = name;
  20. }
  21. }

dao类:

  1. package com.dxz.demo.generic.dao;
  2.  
  3. import java.io.Serializable;
  4.  
  5. public abstract class BaseDao<M extends Serializable> {
  6. public void save(M m) {
  7. System.out.println("BaseDao save:" + m);
  8. }
  9. }
  10.  
  11. package com.dxz.demo.generic.dao;
  12.  
  13. import org.springframework.stereotype.Repository;
  14.  
  15. import com.dxz.demo.generic.model.Organization;
  16.  
  17. @Repository
  18. public class OrganizationDao extends BaseDao<Organization> {
  19. }
  20. package com.dxz.demo.generic.dao;
  21.  
  22. import org.springframework.stereotype.Repository;
  23.  
  24. import com.dxz.demo.generic.model.User;
  25.  
  26. @Repository
  27. public class UserDao extends BaseDao<User> {
  28. }

以前Service写法:

  1. package com.dxz.demo.generic.service.old;
  2.  
  3. import java.io.Serializable;
  4.  
  5. import com.dxz.demo.generic.dao.BaseDao;
  6.  
  7. public abstract class BaseService<M extends Serializable> {
  8. private BaseDao<M> dao;
  9.  
  10. public void setDao(BaseDao<M> repository) {
  11. this.dao = repository;
  12. }
  13.  
  14. public void save(M m) {
  15. System.out.println("old-----");
  16. dao.save(m);
  17. }
  18. }
  19.  
  20. package com.dxz.demo.generic.service.old;
  21.  
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Service;
  24.  
  25. import com.dxz.demo.generic.dao.OrganizationDao;
  26. import com.dxz.demo.generic.model.Organization;
  27.  
  28. @Service
  29. public class OrganizationService extends BaseService<Organization> {
  30. @Autowired
  31. public void setOrganizationRepository(OrganizationDao organizationDao) {
  32. setDao(organizationDao);
  33. }
  34. }
  35. package com.dxz.demo.generic.service.old;
  36.  
  37. import org.springframework.beans.factory.annotation.Autowired;
  38. import org.springframework.stereotype.Service;
  39.  
  40. import com.dxz.demo.generic.dao.UserDao;
  41. import com.dxz.demo.generic.model.User;
  42.  
  43. @Service
  44. public class UserService extends BaseService<User> {
  45. @Autowired
  46. public void setUserDao(UserDao userDao) {
  47. setDao(userDao);
  48. }
  49. }

可以看到,以前必须再写一个setter方法,然后指定注入的具体类型,然后进行注入;

service泛型Service的写法

  1. package com.dxz.demo.generic.service.newf;
  2.  
  3. import java.io.Serializable;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6.  
  7. import com.dxz.demo.generic.dao.BaseDao;
  8.  
  9. public abstract class BaseService<M extends Serializable> {
  10. @Autowired
  11. protected BaseDao<M> dao;
  12.  
  13. public void save(M m) {
  14. System.out.println("newf ----");
  15. dao.save(m);
  16. }
  17. }
  18. package com.dxz.demo.generic.service.newf;
  19.  
  20. import org.springframework.stereotype.Service;
  21.  
  22. import com.dxz.demo.generic.model.Organization;
  23.  
  24. @Service
  25. public class GOrganizationService extends BaseService<Organization> {
  26. }
  27. package com.dxz.demo.generic.service.newf;
  28.  
  29. import org.springframework.stereotype.Service;
  30.  
  31. import com.dxz.demo.generic.model.User;
  32.  
  33. @Service("gUserService")
  34. public class GUserService extends BaseService<User> {
  35. }

spring对bean的装配类:

  1. package com.dxz.demo.generic;
  2.  
  3. import org.springframework.context.annotation.ComponentScan;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. @Configuration
  7. //添加自动扫描注解,basePackages为待注册bean的包路径
  8. @ComponentScan(basePackages = "com.dxz.demo.generic")
  9. public class GenericDemoConfiguration {
  10. public GenericDemoConfiguration() {
  11. System.out.println("GenericDemoConfiguration容器启动初始化。。。");
  12. }
  13. }
  14. package com.dxz.demo.generic;
  15.  
  16. import org.springframework.context.annotation.Configuration;
  17. import org.springframework.context.annotation.Import;
  18.  
  19. @Configuration
  20. @Import(GenericDemoConfiguration.class)
  21. public class GenericDemoConfig {
  22. }

测试类:

  1. package com.dxz.demo.generic;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5.  
  6. import com.dxz.demo.generic.model.User;
  7.  
  8. public class TestMain {
  9.  
  10. public static void main(String[] args) {
  11. // @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContext
  12. ApplicationContext context = new AnnotationConfigApplicationContext(GenericDemoConfig.class);
  13. com.dxz.demo.generic.service.newf.BaseService service = (com.dxz.demo.generic.service.newf.BaseService)context.getBean("gUserService");
  14. service.save(new User(1L,"duan"));
  15.  
  16. com.dxz.demo.generic.service.old.BaseService service2 = (com.dxz.demo.generic.service.old.BaseService)context.getBean("userService");
  17. service2.save(new User(1L,"duan"));
  18. }
  19.  
  20. }

大家可以看到,现在的写法非常简洁。支持泛型式依赖注入。

结果:

这个也是我之前非常想要的一个功能,这样对于那些基本的CRUD式代码,可以简化更多的代码。

如果大家用过Spring data jpa的话,以后注入的话也可以使用泛型限定式依赖注入 :

  1. @Autowired
  2. private Repository<User> userRepository;

对于泛型依赖注入,最好使用setter注入,这样万一子类想变,比较容易切换。比如https://github.com/zhangkaitao/es,如果有多个实现时,子类可以使用@Qualifier指定使用哪一个。

spring4.0之六:Generic Qualifier(泛型限定)的更多相关文章

  1. Spring4.0系列9-websocket简单应用

    http://wiselyman.iteye.com/blog/2003336 ******************************************* Spring4.0系列1-新特性 ...

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

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

  3. JAVA之旅(二十一)——泛型的概述以及使用,泛型类,泛型方法,静态泛型方法,泛型接口,泛型限定,通配符

    JAVA之旅(二十一)--泛型的概述以及使用,泛型类,泛型方法,静态泛型方法,泛型接口,泛型限定,通配符 不知不觉JAVA之旅已经写到21篇了,不得不感叹当初自己坚持要重学一遍JAVA的信念,中途也算 ...

  4. struts2.3.16.1+hibernate4.3.4+spring4.0.2

    把之前的老项目用新的改了 发现新的有点很方便啊 Struts2+Hibernate+Spring整合     用的是      struts2.3.16.1      hibernate4.3.4   ...

  5. 牛客网Java刷题知识点之泛型概念的提出、什么是泛型、泛型在集合中的应用、泛型类、泛型方法、泛型接口、泛型限定上限、泛型限定下限、 什么时候使用上限?泛型限定通配符的体现

    不多说,直接上干货! 先来看个泛型概念提出的背景的例子. GenericDemo.java package zhouls.bigdata.DataFeatureSelection; import ja ...

  6. [转]Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合

    原文地址:http://blog.csdn.net/ycb1689/article/details/22928519 最新版Struts2+Hibernate+Spring整合 目前为止三大框架最新版 ...

  7. Spring4.0编程式定时任务配置

    看过很多定时调度的配置,大多使用XML配置,觉得比较麻烦,也比较老套.这里介绍一种基于spring4.0注解编程式配置定时任务,简单清晰,使用方便.. 至于引入spring相关jar这里不多说,直接切 ...

  8. Spring4.0支持Groovy配置

    介绍 前一段时间观注了一下Spring4.0的一些特性,当中就有对Groovy配置的支持.因为临时还没有很深入的研究.所以举个小样例来说明一下怎样支持Groovy配置. package shuai.s ...

  9. Spring4.0.1+Quartz2.2.1实现定时任务调度[亲测可用]

    Spring4.0.1+Quartz2.2.1实现定时任务调度[亲测可用] tip:只需要配置xml文件即可 1.第三方依赖包的引入 <properties> <project.bu ...

随机推荐

  1. 服务器-华为RH2288H V3-Server 2008R2忘记登录密码操作方法

    1.插入PE盘,重启服务器. 下载地址:http://pan.baidu.com/s/1c16cP6C 密码: 18hq 注:这是支持全系列阵列卡的专用服务器PE工具,市面上的绝大多数PE在服务器中都 ...

  2. (12)模板语言-with

    with的用处 当一个变量特别特别长,可以用with给这个变量重命名 views.py from django.shortcuts import render,HttpResponse def ind ...

  3. 组合数的求法 (n<=1e8 可以过来看)

    C(n,m) =n!/(m!* (n-m)!  ); o(n) 求 1-m的逆元 o(n) 求 n的阶乘 代码实现 https://www.cnblogs.com/linyujun/p/5194189 ...

  4. Spring——使用自定义标签

    文章内容参考了<Spring源码深度解析>一书.自己照着书中内容做了一遍,不懂的地方以及采坑的地方会在文中记录. 推荐一篇post,关于Spring配置文件的命名空间: https://w ...

  5. 【shell编程】之基础知识-常用命令

    一.Shell echo命令 Shell 的 echo 指令与 PHP 的 echo 指令类似,都是用于字符串的输出.命令格式: echo string 您可以使用echo实现更复杂的输出格式控制. ...

  6. LG4238 【【模板】多项式求逆】

    前言 学习了Great_Influence的递推实现,我给大家说一下多项式求逆严格的边界条件,因为我发现改动一些很小的边界条件都会使程序出错.怎么办,背代码吗?背代码是不可能,这辈子都不会背代码的.理 ...

  7. SQLite数据库下载

    一:SQLite简介 SQLite是一种嵌入式数据库,它的数据库就是一个文件.体积很小,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成. 要操作关系数据库,首先需要连接到 ...

  8. imrersize函数

    imrersize函数: 用法:imresize(图像I,method,倍数) 'nearest'(默认值)最近邻插值'bilinear'双线性插值'bicubic'双三次插值 使用方法: clear ...

  9. 监控文件事件inotify

    #include<sys/inotify.h> int inotify_init(void);//创建一个新的inotify实例,成功会返回一个文件描述符fd int inotifyk_a ...

  10. 一个License的所带来问题

    在维护一个老产品时发现一个License的问题.产品是用Z80 Z8F6423, compiler用的是ZDS II Z8 Encode! 4.9.0. 由于有一个Bug要修复,所以我重新检查了一下它 ...