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中是怎么样实现的. 首先我们需要以下几个 ...
随机推荐
- bzoj1003[ZJOI2006]物流运输trans
1003: [ZJOI2006]物流运输trans Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常 ...
- perl 学习笔记
一:基础 1:安装perl centos: yum -y install perl 官网:https://www.perl.org/ 升级到5.22:先下载,执行./i ...
- inconvertible types; cannot cast 'android.supoort.v4.app.Fragment' to 'com.example.sevenun.littledemo.fragment.NewsTitleFragment'
inconvertible types; cannot cast 'android.supoort.v4.app.Fragment' to 'com.example.sevenun.littledem ...
- Http(get,post)及HttpClient(get,post)的简单使用
1. 使用 Http 的 Get 方式读取网络数据 import java.io.BufferedReader; import java.io.IOException; import java.io. ...
- html a标签打开邮件
<a href="mailto:frotech@foxmail.com" target="_blank">frotech@foxmail.com&l ...
- Django之上传文件
使用Form表单上传文件 upload.html <!DOCTYPE html> <html lang="en"> <head> <met ...
- Mongo服务器集群配置【转】
http://www.cnblogs.com/wly923/tag/MongoDB/ 当前标签: MongoDB Mongo服务器集群配置学习三——分片 风行影者 2013-04-14 22:35 ...
- CATransform3D中m34字段的取值含义
转载自:http://zhidao.baidu.com/link?url=OlVQoGOKIBmaXKgQisOLtzliTLPvreOOsRmny3yebA1Wb6-B3KtuKlRXmv0tO3y ...
- iptables 实现centos内网机器访问外网
环境:一台带外网和内网的机器,另一台只有内网,默认不能上网.两台机器都是centos系统带外网机器的外网ip为 123.221.20.11, 内网ip为 192.168.15.100内网机器的内网ip ...
- 用JS的for循环打印九九乘法表
需要使用两个for循环嵌套,代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta cha ...