最近对系统进行改造,发现在泛型实例初始化的时候,得不到想要的泛型。或者需要强制转换。

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 泛型注入的更多相关文章

  1. Spring.NET依赖注入框架学习-- 泛型对象的创建和使用

    Spring.NET依赖注入框架学习-- 泛型对象的创建和使用 泛型对象的创建方法和普通对象是一样的. 通过构造器创建泛型对象 下面是一个泛型类的代码: namespace GenericsPlay ...

  2. Spring的泛型依赖注入

    Spring 4.x 中可以为子类注入子类对应的泛型类型的成员变量的引用,(这样子类和子类对应的泛型类自动建立关系)具体说明: 泛型注入:就是Bean1和Bean2注入了泛型,并且Bean1和Bean ...

  3. Spring框架入门之Spring4.0新特性——泛型注入

    Spring框架入门之Spring4.0新特性——泛型注入 一.为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入. ...

  4. spring注解方式配置以及spring4的泛型注入 (4)

    目录 一.@Controller 注解控制层(action) 二.@Service 注解服务层 三.@Repository 持久层 四.spring4的泛型注入测试 1 创建两个实体User和Role ...

  5. spring泛型注入

    泛型依赖注入 Spring 4.0版本中更新了很多新功能,其中比较重要的一个就是对带泛型的Bean进行依赖注入的支持. 泛型依赖注入允许我们在使用spring进行依赖注入的同时,利用泛型的优点对代码进 ...

  6. spring bean自动注入

    使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ...

  7. 类spring ioc 泛型保留

    类spring ioc 泛型保留 什么是泛型擦除 Java并不会传递泛型类,举个直观的栗子: @Component public class BaseProvider<T> { publi ...

  8. (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)

    Spring要把xml配置中bean的属性实例化为具体的bean,"依赖注入"是关卡.所谓的"依赖注入",就是把应用程序对bean的属性依赖都注入到spring ...

  9. Spring的依赖注入(DI)三种方式

    Spring依赖注入(DI)的三种方式,分别为: 1.  接口注入 2.  Setter方法注入 3.  构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...

随机推荐

  1. hdu 5313 Bipartite Graph(dfs染色 或者 并查集)

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

  2. spark1.3的部署

    1.下载源码,根据自己的环境编译,我这里下载的是spark1.3版本 本人采用sbt编译, SPARK_HADOOP_VERSION=2.5.2 SPARK_YARN=ture sbt/sbt ass ...

  3. Servlet登陆功能的实现

    (1)创建动态的Login页面 //登陆界面 package com.lc; import javax.servlet.http.*; import java.io.*; public class L ...

  4. oracle 同样数据删除(仅仅留一条)

    DELETE FROM reg_user t1 WHERE user_name='9527008' and rowid > ( SELECT min(rowid) FROM location t ...

  5. Android ActionBar详解(三):ActionBar实现切换Tabs标签

    实现切换Tabs标签; Activity代码: public class ActionBarTabs extends Activity { @Override protected void onCre ...

  6. actionBar兼容2.1及以上版本的做法 .

    正在准备一个项目,需要尊重android design的同时还要做到很好的兼容低版本,于是就先从actionBar开始吧. 1,新建一个android工程startActionBar,minSdkVe ...

  7. Linux的目录结构及其作用

    /bin bin是Binary的缩写.这个目录存放着最经常使用的命令. /boot这里存放的是启动Linux时使用的一些核心文件,包括一些连接文件以及镜像文件. /dev dev是Device(设备) ...

  8. js 闭包和回调

    原文:http://www.cnblogs.com/yuyuj/p/4525530.html 之前的工作都是基于老大搭建的框架,仿照他写的例子写的请求,很多东东也都做好了封装,只需要了解下直接调用就好 ...

  9. MS-SQL数据库备份方法

    一.手动备份 打开企业管理器 --> 右键点击需要备份的数据库 --> 所有任务 --> 备份数据库 或者: 查询分析器: use master  backup database 数 ...

  10. hdu Repositoryti

    算法:字典树 题意:给你一些字符串,然后会有一些询问,输出询问在给定的字符串中出现了多少次(字串也是): 例如 add,子串有:a ,d,d,ad ,dd,add:你会发现子串d出现了两次,那么怎么办 ...