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类:

package com.dxz.demo.generic.model;

import java.io.Serializable;

public class Organization implements Serializable {
private Long id;
private String name;
}
package com.dxz.demo.generic.model; import java.io.Serializable; public class User implements Serializable {
private Long id;
private String name; public User(Long id, String name) {
this.id = id;
this.name = name;
}
}

dao类:

package com.dxz.demo.generic.dao;

import java.io.Serializable;

public abstract class BaseDao<M extends Serializable> {
public void save(M m) {
System.out.println("BaseDao save:" + m);
}
} package com.dxz.demo.generic.dao; import org.springframework.stereotype.Repository; import com.dxz.demo.generic.model.Organization; @Repository
public class OrganizationDao extends BaseDao<Organization> {
}
package com.dxz.demo.generic.dao; import org.springframework.stereotype.Repository; import com.dxz.demo.generic.model.User; @Repository
public class UserDao extends BaseDao<User> {
}

以前Service写法:

package com.dxz.demo.generic.service.old;

import java.io.Serializable;

import com.dxz.demo.generic.dao.BaseDao;

public abstract class BaseService<M extends Serializable> {
private BaseDao<M> dao; public void setDao(BaseDao<M> repository) {
this.dao = repository;
} public void save(M m) {
System.out.println("old-----");
dao.save(m);
}
} package com.dxz.demo.generic.service.old; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.dxz.demo.generic.dao.OrganizationDao;
import com.dxz.demo.generic.model.Organization; @Service
public class OrganizationService extends BaseService<Organization> {
@Autowired
public void setOrganizationRepository(OrganizationDao organizationDao) {
setDao(organizationDao);
}
}
package com.dxz.demo.generic.service.old; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.dxz.demo.generic.dao.UserDao;
import com.dxz.demo.generic.model.User; @Service
public class UserService extends BaseService<User> {
@Autowired
public void setUserDao(UserDao userDao) {
setDao(userDao);
}
}

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

service泛型Service的写法

package com.dxz.demo.generic.service.newf;

import java.io.Serializable;

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

import com.dxz.demo.generic.dao.BaseDao;

public abstract class BaseService<M extends Serializable> {
@Autowired
protected BaseDao<M> dao; public void save(M m) {
System.out.println("newf ----");
dao.save(m);
}
}
package com.dxz.demo.generic.service.newf; import org.springframework.stereotype.Service; import com.dxz.demo.generic.model.Organization; @Service
public class GOrganizationService extends BaseService<Organization> {
}
package com.dxz.demo.generic.service.newf; import org.springframework.stereotype.Service; import com.dxz.demo.generic.model.User; @Service("gUserService")
public class GUserService extends BaseService<User> {
}

spring对bean的装配类:

package com.dxz.demo.generic;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
//添加自动扫描注解,basePackages为待注册bean的包路径
@ComponentScan(basePackages = "com.dxz.demo.generic")
public class GenericDemoConfiguration {
public GenericDemoConfiguration() {
System.out.println("GenericDemoConfiguration容器启动初始化。。。");
}
}
package com.dxz.demo.generic; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; @Configuration
@Import(GenericDemoConfiguration.class)
public class GenericDemoConfig {
}

测试类:

package com.dxz.demo.generic;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.dxz.demo.generic.model.User; public class TestMain { public static void main(String[] args) {
// @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContext
ApplicationContext context = new AnnotationConfigApplicationContext(GenericDemoConfig.class);
com.dxz.demo.generic.service.newf.BaseService service = (com.dxz.demo.generic.service.newf.BaseService)context.getBean("gUserService");
service.save(new User(1L,"duan")); com.dxz.demo.generic.service.old.BaseService service2 = (com.dxz.demo.generic.service.old.BaseService)context.getBean("userService");
service2.save(new User(1L,"duan"));
} }

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

结果:

这个也是我之前非常想要的一个功能,这样对于那些基本的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. PTA——最大公约数和最小公倍数

    PTA 7-26 最大公约数和最小公倍数 #include<stdio.h> int main(){ int num1,num2,temp1,temp2,r; scanf("%d ...

  2. ideal通过svn上传项目和激活方式

    激活方式: http://www.ideadev.club/topic/detail/102?anchor=1526259147353#1526259147353

  3. django-dailyfresh

    Hold on ,learn by myself! redis nosql - 不支持sql语法 - 存储数据都是KV形式 - Mongodb - Redis - Hbase hadoop - Cas ...

  4. 《DSP using MATLAB》Problem 5.17

    1.代码 %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% ...

  5. 通过阻止 touchstart 事件,解决移动端的 BUG

    在 iOS10 下,即使设置了 meta 标签的 user-scalable=no 依然不能阻止用户缩放. 解决此 BUG 的方法是阻止 PC 事件: document.addEventListene ...

  6. Hibernate(二)

    性能分析 抓取策略 研究对象 研究怎么样提取集合的,该策略应该作用与set元素上 研究从一的一方加载多的一方 案例 查询cid为1的班级的所有的学生 明:通过一条sql语句:左外链接,把classes ...

  7. vue cli 3.x 设置4个空格缩进

    vue cli 3.x 设置4个空格缩进: 文件.editorconfig中,indent_size = 2修改为indent_size = 4 文件 .eslintrc.js 添加一行:'inden ...

  8. python的requests快速上手、高级用法和身份认证

    https://blog.csdn.net/qq_25134989/article/details/78800209 快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其 ...

  9. 【转】基于keras 的神经网络股价预测模型

    from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY,YEARLY from matplotli ...

  10. 使用php生成数字、字母组合验证码

    项目中经常会遇到一些登陆验证,支付验证等等一系列安全验证的策略.实现方法多种多样,下面就来讲解下如何用php生成简单的文字+数字组合的验证码: 所用语言php,gd库 原理解释: a>实质上是在 ...