大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
最近对系统进行改造,发现在泛型实例初始化的时候,得不到想要的泛型。或者需要强制转换。
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
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/159970.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...