spring4.0之六:Generic Qualifier(泛型限定)
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的话,以后注入的话也可以使用泛型限定式依赖注入 :
- @Autowired
- private Repository<User> userRepository;
对于泛型依赖注入,最好使用setter注入,这样万一子类想变,比较容易切换。比如https://github.com/zhangkaitao/es,如果有多个实现时,子类可以使用@Qualifier指定使用哪一个。
spring4.0之六:Generic Qualifier(泛型限定)的更多相关文章
- Spring4.0系列9-websocket简单应用
http://wiselyman.iteye.com/blog/2003336 ******************************************* Spring4.0系列1-新特性 ...
- Spring框架入门之Spring4.0新特性——泛型注入
Spring框架入门之Spring4.0新特性——泛型注入 一.为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入. ...
- JAVA之旅(二十一)——泛型的概述以及使用,泛型类,泛型方法,静态泛型方法,泛型接口,泛型限定,通配符
JAVA之旅(二十一)--泛型的概述以及使用,泛型类,泛型方法,静态泛型方法,泛型接口,泛型限定,通配符 不知不觉JAVA之旅已经写到21篇了,不得不感叹当初自己坚持要重学一遍JAVA的信念,中途也算 ...
- struts2.3.16.1+hibernate4.3.4+spring4.0.2
把之前的老项目用新的改了 发现新的有点很方便啊 Struts2+Hibernate+Spring整合 用的是 struts2.3.16.1 hibernate4.3.4 ...
- 牛客网Java刷题知识点之泛型概念的提出、什么是泛型、泛型在集合中的应用、泛型类、泛型方法、泛型接口、泛型限定上限、泛型限定下限、 什么时候使用上限?泛型限定通配符的体现
不多说,直接上干货! 先来看个泛型概念提出的背景的例子. GenericDemo.java package zhouls.bigdata.DataFeatureSelection; import ja ...
- [转]Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合
原文地址:http://blog.csdn.net/ycb1689/article/details/22928519 最新版Struts2+Hibernate+Spring整合 目前为止三大框架最新版 ...
- Spring4.0编程式定时任务配置
看过很多定时调度的配置,大多使用XML配置,觉得比较麻烦,也比较老套.这里介绍一种基于spring4.0注解编程式配置定时任务,简单清晰,使用方便.. 至于引入spring相关jar这里不多说,直接切 ...
- Spring4.0支持Groovy配置
介绍 前一段时间观注了一下Spring4.0的一些特性,当中就有对Groovy配置的支持.因为临时还没有很深入的研究.所以举个小样例来说明一下怎样支持Groovy配置. package shuai.s ...
- Spring4.0.1+Quartz2.2.1实现定时任务调度[亲测可用]
Spring4.0.1+Quartz2.2.1实现定时任务调度[亲测可用] tip:只需要配置xml文件即可 1.第三方依赖包的引入 <properties> <project.bu ...
随机推荐
- 《DSP using MATLAB》Problem 6.9
9月9日,我们怀念毛主席! 代码: %% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- CH4907 作诗
题意 4907 作诗 0x49「数据结构进阶」练习 描述 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗之后还要虐OI,于是SHY ...
- 简单 babel plugin 开发-使用lerna 工具
babel在现在的web 应用开发上具有很重要的作用,帮助我们做了好多事情,同时又有 比较多的babel plugin 可以解决我们实际开发上的问题. 以下只是学习下如果编写一个简单的babel pl ...
- Gravitee.io alert 引擎架构
alert 在我们的实际开发中应用的场景很多,我们需要进行系统状态的查看,以及特殊异常请求的处理 参考架构图 从下图可以看出,还是很方便的,同时支持slack email... 的实时消息通知,而且我 ...
- debezium mongodb 集成测试
debezium 是一个方便的cdc connector 可以帮助我们解决好多数据实时变更处理.数据分析.微服务的数据通信 从上次跑简单demo到现在,这个工具是有好多的变更,添加了好多方便的功能,支 ...
- FastAdmin 新年福袋进行中
FastAdmin 新年福袋进行中 2019新年福袋活动正在进行中 https://www.fastadmin.net/act/20190101/springfestival.html
- 持续集成--Jenkins--1
持续集成之Jenkins安装部署 1.安装JDK Jenkins是Java编写的,所以需要先安装JDK,这里采用yum安装,如果对版本有需求,可以直接在Oracle官网下载JDK. [root@l ...
- python去除\ufeff、\xa0、\u3000
今天使用python处理一个txt文件的时候,遇到几个特殊字符:\ufeff.\xa0.\u3000,记录一下处理方法 代码:with open(file_path, mode='r') as f: ...
- .ajax向后台传递数组(转)
js部分代码 //创建一个测试数组 var boxIds = new Array(); boxIds.push(12182); boxIds.push(12183); boxIds.push(1218 ...
- 高级java必会系列一:常用线程池和调度类
众所周知,开启线程2种方法:第一是实现Runable接口,第二继承Thread类.(当然内部类也算...)常用的,这里就不再赘述. 一.线程池 1.newCachedThreadPool (1)缓存型 ...