spring4泛型初探----一个小例子
泛型的出现,是为了让代码更规整。
例如
Set<String> set=new HashSet<>();
set.add("abc");
set.add(123); //编译出错
如果不用泛型,那set里面放什么都可以,放什么都可以倒不是问题,问题在于你取出来的时候,都用object吗。
我们看下面的代码:
package com.bufoon.store;
public interface Store<T>{
public T getObject();
}
package com.bufoon.store;
import org.springframework.stereotype.Component;
@Component
public class IntegerStore implements Store<Integer> {
@Override
public Integer getObject() {
return 12;
}
}
package com.bufoon.store;
import org.springframework.stereotype.Component;
@Component
public class StringStore implements Store<String> {
@Override
public String getObject() {
// TODO Auto-generated method stub
return "ABC";
}
}
package com.bufoon.store;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service
public class MyService {
@Resource
private Store<Integer> integerStore;
@Resource
private IntegerStore s1;
@Resource
private Store<String> s2;
@Resource
private List<Store<Integer>> s3;
public void getResult(){
System.out.println("integerStore "+integerStore.getObject());
System.out.println("s1 "+s1.getObject());
System.out.println("s2 string "+s2.getObject());
System.out.println("s3 size "+s3.size());
}
}
package com.bufoon.store;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext3.xml");
MyService service=(MyService) context.getBean("myService");
service.getResult();
}
}
上述的代码如果在spirng3下运行
回报下面的错误
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myService': Injection of resource
dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type
[com.bufoon.store.Store] is defined: expected single matching bean but found 3: [anotherIntegerStore, integerStore, stringStore]
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:307)
......
at com.bufoon.store.Test.main(Test.java:7)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.bufoon.store.Store] is defined: expected single
matching bean but found 3: [anotherIntegerStore, integerStore, stringStore]
......
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:304)
... 13 more
如果在spring4下运行 一切OK
输出
integerStore 12
s1 12
s2 string ABC
s3 size 1
恩,这spring4与spring3确实有区别。可问题是,即使支持了泛型,能有什么好处呢?
难道是然并卵?
关于泛型的实际应用 大家请看开涛大哥的博客 Spring4新特性——泛型限定式依赖注入
如果要获取泛型的实际参数,大家请看小弟的 获取泛型类的真实参数
额,感觉自己什么都没讲.... 不过关于泛型这边,确实有点抽象,小弟才疏学浅,见笑了
参考资料
http://spring.io/blog/2013/12/03/spring-framework-4-0-and-java-generics
spring4泛型初探----一个小例子的更多相关文章
- java连接mysql的一个小例子
想要用java 连接数据库,需要在classpath中加上jdbc的jar包路径 在eclipse中,Project的properties里面的java build path里面添加引用 连接成功的一 ...
- java操作xml的一个小例子
最近两天公司事比较多,这两天自己主要跟xml打交道,今天更一下用java操作xml的一个小例子. 原来自己操作xml一直用这个包:xstream-1.4.2.jar.然后用注解的方式,很方便,自己只要 ...
- MVVM模式的一个小例子
使用SilverLight.WPF也有很长时间了,但是知道Binding.Command的基本用法,对于原理性的东西,一直没有深究.如果让我自己建一个MVVM模式的项目,感觉还是无从下手,最近写了一个 ...
- 使用Trinity拼接以及分析差异表达一个小例子
使用Trinity拼接以及分析差异表达一个小例子 2017-06-12 09:42:47 293 0 0 Trinity 将测序数据分为许多独立的de Brujin grap ...
- 从一个小例子认识SQL游标
1 什么是游标: 关系数据库中的操作会对整个行集起作用. 例如,由 SELECT 语句返回的行集包括满足该语句的 WHERE 子句中条件的所有行. 这种由语句返回的完整行集称为结果集. 应用程序 ...
- 关于SVN配置文件的一个小例子
1 背景假设 厦门央瞬公司是一家电子元器件设备供应商,其中有个ARM部门,专门负责ARM芯片的方案设计.销售,并在北京.上海各设立了一个办事处.对于工作日志,原先采用邮件方式发给经理,但是这种方式 ...
- Vue2.x源码学习笔记-从一个小例子查看vm实例生命周期
学习任何一门框架,都不可能一股脑儿的从入口代码从上到下,把代码看完, 这样其实是很枯燥的,我想也很少有人这么干,或者这么干着干着可能干不下去了. 因为肯定很无聊. 我们先从一个最最简单的小例子,来查看 ...
- Spring和Hibernate结合的一个小例子
1.新建一个SpringHibernate的maven项目 2.pom文件的依赖为 <dependency> <groupId>junit</groupId> &l ...
- Spring.Net在ASP.NET Mvc里使用的一个小例子
就贴个小例子,就不注意格式了. 1.下载dll NuGet的下载地址:http://docs.nuget.org/docs/start-here/installing-nuget 在vs的NuGet里 ...
随机推荐
- JSP运行过程 JSP脚本 静态动态包含 jsp指令 jsp内置对象jsp四大作用域 jsp动作元素 EL表达式 JSTL 设计模式 JSP开发模式 EL内置对象
Day38 JSP JSP的运行过程具体如下: (1)客户端发出请求,请求访问JSP文件. (2)JSP容器先将JSP文件转换成一个Java源文件(Java Servlet源程序),在转换过程中,如果 ...
- Node.js 调试器
稳定性: 3 - 稳定 V8 提供了强大的调试工具,可以通过 TCP protocol 从外部访问.Node 内置这个调试工具客户端.要使用这个调试器,以debug参数启动 Node,出现提示: % ...
- 线程停止与volatile
1.使用标志位停止线程 在Java中希望停止线程,可以使用设置标志位的方法,如下例所示: class SimpleTask implements Runnable{ private boolean s ...
- 拟将博客迁移到github
其实博客园网站速度挺快的, 但是markdown的显示没有github美观. 尤其是代码高亮这一块. 近日发现github pages + vue + github api + stackedit 能 ...
- Gradle 1.12用户指南翻译——第四十七章. Build Init 插件
本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- iOS关于时间的处理
转自:iOS关于时间的处理 做App避免不了要和时间打交道,关于时间的处理,里面有不少门道,远不是一行API调用,获取当前系统时间这么简单.我们需要了解与时间相关的各种API之间的差别,再因场景而异去 ...
- Git之(六)标签管理
发布一个版本时,我们通常先在版本库中打一个标签(tag),这样,就唯一确定了打标签时刻的版本.将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来.所以,标签也是版本库的一个快照 ...
- ICL Auto Vectorization
简介 此文简单介绍如何使用intel c++编译器实现向量化加速. 全文如下安排: base : 待优化的源代码. vectorization : 第一个向量化版本. aligned : 内存对其对向 ...
- JAVA面向对象-----成员内部类的访问方式
成员内部类的访问方式 1.内部类可以直接访问外部类的成员属性.(孙悟空相当于内部类飞到牛魔王的肚子里面去). 2.外部类需要访问内部类的成员属性时需要创建内部类的对象. 1.在外部类的成员函数中创建内 ...
- Dynamics CRM The difference between UserId and InitiatingUserId in Plugin
对于这两者的不同,MSDN的解释如下 • IExecutionContext.UserId Property: Gets the global unique identifier of the sys ...