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 ...
随机推荐
- 服务器-华为RH2288H V3-Server 2008R2忘记登录密码操作方法
1.插入PE盘,重启服务器. 下载地址:http://pan.baidu.com/s/1c16cP6C 密码: 18hq 注:这是支持全系列阵列卡的专用服务器PE工具,市面上的绝大多数PE在服务器中都 ...
- (12)模板语言-with
with的用处 当一个变量特别特别长,可以用with给这个变量重命名 views.py from django.shortcuts import render,HttpResponse def ind ...
- 组合数的求法 (n<=1e8 可以过来看)
C(n,m) =n!/(m!* (n-m)! ); o(n) 求 1-m的逆元 o(n) 求 n的阶乘 代码实现 https://www.cnblogs.com/linyujun/p/5194189 ...
- Spring——使用自定义标签
文章内容参考了<Spring源码深度解析>一书.自己照着书中内容做了一遍,不懂的地方以及采坑的地方会在文中记录. 推荐一篇post,关于Spring配置文件的命名空间: https://w ...
- 【shell编程】之基础知识-常用命令
一.Shell echo命令 Shell 的 echo 指令与 PHP 的 echo 指令类似,都是用于字符串的输出.命令格式: echo string 您可以使用echo实现更复杂的输出格式控制. ...
- LG4238 【【模板】多项式求逆】
前言 学习了Great_Influence的递推实现,我给大家说一下多项式求逆严格的边界条件,因为我发现改动一些很小的边界条件都会使程序出错.怎么办,背代码吗?背代码是不可能,这辈子都不会背代码的.理 ...
- SQLite数据库下载
一:SQLite简介 SQLite是一种嵌入式数据库,它的数据库就是一个文件.体积很小,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成. 要操作关系数据库,首先需要连接到 ...
- imrersize函数
imrersize函数: 用法:imresize(图像I,method,倍数) 'nearest'(默认值)最近邻插值'bilinear'双线性插值'bicubic'双三次插值 使用方法: clear ...
- 监控文件事件inotify
#include<sys/inotify.h> int inotify_init(void);//创建一个新的inotify实例,成功会返回一个文件描述符fd int inotifyk_a ...
- 一个License的所带来问题
在维护一个老产品时发现一个License的问题.产品是用Z80 Z8F6423, compiler用的是ZDS II Z8 Encode! 4.9.0. 由于有一个Bug要修复,所以我重新检查了一下它 ...