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 ...
随机推荐
- django BBS project login登录功能实现
1.models from django.db import models # Create your models here. from django.contrib.auth.models imp ...
- python在图片上画矩形
python在图片上画矩形 image_path = '' image = cv2.imread(image_path) first_point = (100, 100) last_point = ( ...
- 【杭电OJ3938】【离线+并查集】
http://acm.hdu.edu.cn/showproblem.php?pid=3938 Portal Time Limit: 2000/1000 MS (Java/Others) Memo ...
- LeetCode – Most Common Word
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
- day 59 Bootstrap学习
---恢复内容开始--- 简介 栅格系统用于通过一系列的行(row)与列(column)的组合来创建页面布局,你的内容就可以放入这些创建好的布局中.下面就介绍一下 Bootstrap 栅格系统的工作原 ...
- js技巧专题篇: 页面跳转
本篇主要介绍网页上常见的页面跳转技术.页面跳转有几种方式,比较常用的是window.location.href,window.location.replace,window.open,当然还有目前比较 ...
- day 3 大纲笔记
01 昨日内容回顾 while 条件: 循环体 如何终止循环: 1,改变条件. 2,break. 3,exit() quit() 不推荐. 关键字: break continue while else ...
- 深入理解 content 计数器
计数器可以说是content的重点, 因为此功能非常强大, 实用, 并且不具有可替代性, 甚至可以实现连JavaScript都不好实现的效果. 所谓css计数器效果, 就是使用CSS代码实现随元素的数 ...
- express 与 koa 区别
express 与 koa 区别 区别项 express koa 中间件模型 Compress 模型 洋葱圈模型 对象个数 只有2个对象:Request 和 Response 有3个对象:Reques ...
- Jmeter --- 分布式测试
在使用Jmeter进行性能测试时,如果并发数比较大(比如最近项目需要支持1000并发),单台电脑的配置(CPU和内存)可能无法支持,这时可以使用Jmeter提供的分布式测试的功能. 一.Jmeter分 ...