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中是怎么样实现的. 首先我们需要以下几个 ...
随机推荐
- mv,Directory not empty不能目录覆盖
一.mv /test1/* /test2/test1rm -rf /test1 二. You can however use rsync with the --remove-source-files ...
- Android GridView用法介绍
GridView(网格视图)是按照行和列的方式来显示内容的,一般用于显示图片等内容,比如实现九宫格图: 第一个例子: 实现代码为: MainActivity.java package com.xiao ...
- NOI 2013 书法家
http://uoj.ac/problem/125 我真是日狗了...... 果然还是没有耐心读题,搞到读题读错了2个地方,结果调试了半天...... 言归正传. 动态规划. 这种题目很常见. 我们发 ...
- 【剑指offer】面试题38:数字在排序数组中出现的次数
题目: 统计一个数字在排序数组中出现的次数. 思路: 对二分查找进行改进,找到数字在数组中第一次出现和最后一次出现的位置,这样就得到它出现的次数. 以找第一次出现的位置为例:如果mid元素大于k,则在 ...
- spring加载properties配置文件
public static void main(String[] args){ String path="E:/workspace/bocMarketData/src/config/P ...
- 解决方案--java执行cmd命令ProcessBuilder--出错Exception in thread "main" java.io.IOException: Cannot run program "dir d:\": CreateProcess error=2(xjl456852原创)
当我尝试在java中通过ProcessBuilder运行window的cmd命令时出现错误: public static void main(String [] args) throws IOExce ...
- javascript实现的手风琴折叠菜单效果
演示地址:http://codepen.io/anon/pen/pJERMq 实现效果: HTML代码: <!DOCTYPE html> <html lang="en&qu ...
- Android UI开发详解之ActionBar .
在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果. 一.添加A ...
- MDM 证书申请流程(vendor及customer)
整个流程分为两部分:vendor,customer. 一.Vendor 1.成为一个 MDM Vendor 1) 首先你须要拥有一个 Apple Enterprise account($299/年). ...
- (转)wcf client与webservice通信(-)只修改配置文件而改变服务端
http://www.cnblogs.com/yiyisawa/archive/2008/12/16/1356191.html 问题: 假设有一个大型系统新版本使用wcf 作为服务端,生成wcf cl ...