spring 4 泛型注入
最近对系统进行改造,发现在泛型实例初始化的时候,得不到想要的泛型。或者需要强制转换。
spring 4 开始支持泛型对象初始化,初始化方法如下:
注:使用配置文件的方法暂时还没有发现,下面是使用java annotation的方法:
package com.mitchz..toolkit.chain; import java.util.List;
import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.asiainfo.iposs.toolkit.chain.spring.TestMessageContext;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps; /**
* @author zhangya
* @version 1.0
* @since 2014年5月25日
* @category com.mitchz.toolkit.chain
*/
@Configuration
public class TestConfiguration
{ @Autowired
@Qualifier("getCustomerInfo")
HandlerCommand<String, Object, TestMessageContext> getCustomerInfo;
@Autowired
@Qualifier("testDriveVehicle")
HandlerCommand<String, Object, TestMessageContext> testDriveVehicle;
@Autowired
@Qualifier("negotiateSale")
HandlerCommand<String, Object, TestMessageContext> negotiateSale;
@Autowired
@Qualifier("arrangeFinancing")
HandlerCommand<String, Object, TestMessageContext> arrangeFinancing;
@Autowired
@Qualifier("closeSale")
HandlerCommand<String, Object, TestMessageContext> closeSale;
@Autowired
@Qualifier("chain1")
HandlerChain<String, Object, TestMessageContext> chain1;
@Autowired
@Qualifier("chain2")
HandlerChain<String, Object, TestMessageContext> chain2; @Bean(name = "chain1")
public HandlerChain<String, Object, TestMessageContext> createChain1()
{
List<HandlerCommand<String, Object, TestMessageContext>> commands = Lists
.newArrayList();
commands.add(getCustomerInfo);
commands.add(testDriveVehicle);
commands.add(negotiateSale);
commands.add(arrangeFinancing);
commands.add(closeSale);
return new HandlerChain<String, Object, TestMessageContext>(commands);
} @Bean(name = "chain2")
public HandlerChain<String, Object, TestMessageContext> createChain2()
{
List<HandlerCommand<String, Object, TestMessageContext>> commands = Lists
.newArrayList();
commands.add(getCustomerInfo);
commands.add(arrangeFinancing);
commands.add(closeSale);
return new HandlerChain<String, Object, TestMessageContext>(commands);
} @Bean(name = "catalog")
public HandlerCatalog<String, Object, TestMessageContext> createCatalog()
{
Map<String, HandlerChain<String, Object, TestMessageContext>> commands = Maps
.newHashMap();
commands.put("chain1", chain1);
commands.put("chain2", chain2);
return new HandlerCatalog<String, Object, TestMessageContext>(commands);
}
}
完整的例子参考:
http://spring.io/blog/2013/12/03/spring-framework-4-0-and-java-generics
spring 4 泛型注入的更多相关文章
- Spring.NET依赖注入框架学习-- 泛型对象的创建和使用
Spring.NET依赖注入框架学习-- 泛型对象的创建和使用 泛型对象的创建方法和普通对象是一样的. 通过构造器创建泛型对象 下面是一个泛型类的代码: namespace GenericsPlay ...
- Spring的泛型依赖注入
Spring 4.x 中可以为子类注入子类对应的泛型类型的成员变量的引用,(这样子类和子类对应的泛型类自动建立关系)具体说明: 泛型注入:就是Bean1和Bean2注入了泛型,并且Bean1和Bean ...
- Spring框架入门之Spring4.0新特性——泛型注入
Spring框架入门之Spring4.0新特性——泛型注入 一.为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入. ...
- spring注解方式配置以及spring4的泛型注入 (4)
目录 一.@Controller 注解控制层(action) 二.@Service 注解服务层 三.@Repository 持久层 四.spring4的泛型注入测试 1 创建两个实体User和Role ...
- spring泛型注入
泛型依赖注入 Spring 4.0版本中更新了很多新功能,其中比较重要的一个就是对带泛型的Bean进行依赖注入的支持. 泛型依赖注入允许我们在使用spring进行依赖注入的同时,利用泛型的优点对代码进 ...
- spring bean自动注入
使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ...
- 类spring ioc 泛型保留
类spring ioc 泛型保留 什么是泛型擦除 Java并不会传递泛型类,举个直观的栗子: @Component public class BaseProvider<T> { publi ...
- (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)
Spring要把xml配置中bean的属性实例化为具体的bean,"依赖注入"是关卡.所谓的"依赖注入",就是把应用程序对bean的属性依赖都注入到spring ...
- Spring的依赖注入(DI)三种方式
Spring依赖注入(DI)的三种方式,分别为: 1. 接口注入 2. Setter方法注入 3. 构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...
随机推荐
- 查看syslog-ng内存,兼容容器情况
syslog_pid=`ps -ef|grep syslog-ng|grep -v grep |awk '{print $2}'` pid_count=`ps -ef|grep syslog-ng|g ...
- Beyond Compare 忽略两个文件内容的顺序比较文件内容(xjl456852原创)
有时两个文件内容的顺序是不固定的,对比时需要忽略文件顺序进行对比. 可以这样设置: 点击菜单下面工具栏按钮: 点击Format旁的三角,选择Sorted,就会按文件的顺序排序比较.忽略了文件内容顺序的 ...
- IOS 缩放图片常用方法
/** * 指定Size压缩图片 (图片会压缩变形) * * @param image 原图 * @param size 压缩size * * @return 压缩后的图片 */ -(UIImage* ...
- git 拆库 切库 切分 子目录建库
如果git库目录是这样的: git根目录 project_a/ project_b/ ... 并且想为project_a单独创建一个代码库 # 拉一个新分支 git co -b project_a_r ...
- NetAnalyzer笔记 之 四. C#版的抓包软件
[创建时间:2015-09-10 22:37:04] NetAnalyzer下载地址 不好意思啊,NetAnalyzer停更有点长了,今天继续填坑^&^ NetAnalyzer实现结构 在上一 ...
- js为鼠标添加右击事件
<script language="javascript"> /*document.oncontextmenu=Youji;*/ //为当前文档添加鼠标右击事件,防 ...
- [IOI1999]花店橱窗布置(DP路径记录)
题目:[IOI1999]花店橱窗布置 问题编号:496 题目描述 某花店现有F束花,每一束花的品种都不一样,同时至少有同样数量的花瓶,被按顺序摆成一行,花瓶的位置是固定的,从左到右按1到V顺序编号,V ...
- [RxJS] Error Handling in RxJS
Get your code back on the happy path! This lesson covers a variety of ways to handle exceptions thro ...
- linux高级技巧:heartbeat+lvs(一)
1.heartbeat一个简短的引论: Heartbeat 项目是 Linux-HA project的一个组成部分,它实现了一个高可用集群系统.心跳服务和集群通信是高可用集群的两个关键组 ...
- 寒哥细谈之AutoLayout全解
文/南栀倾寒(简书作者)原文链接:http://www.jianshu.com/p/683fbcbfb705著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 看到群中好多朋友还停留在Fr ...