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里 ...
随机推荐
- 566. Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- Android颜色配置器
一.Android Color设置 1.在xml文件中 想设置颜色直接设置background的属性或者其他的color属性.随便设置一个颜色如#000,再点击左边的颜色方块,弹出颜色选择器选择颜色 ...
- WSGI及gunicorn指北(一)
作为一个Python Web 开发工程师,pyg0每天都喜滋滋的写着基于各种web框架的业务代码. 突然有一天,技术老大过来跟pyg0说,嘿,我们要新上线一个服务,你来帮我部署一下吧.不用太复杂.用g ...
- seaborn使用(绘图函数)
seaborn使用(绘图函数) 数据集分布的可视化 分类数据的绘图 线性关系可视化 一.数据集分布的可视化 distplot kdeplot rugplot 1.distplot() 灵活的绘制单变量 ...
- jQuery 遍历 – 祖先
祖先是父.祖父或曾祖父等等. 通过 jQuery,您能够向上遍历 DOM 树,以查找元素的祖先. 向上遍历 DOM 树 这些 jQuery 方法很有用,它们用于向上遍历 DOM 树: parent() ...
- MySQL DATEDIFF() 函数
定义和用法 DATEDIFF() 函数返回两个日期之间的天数. 语法 DATEDIFF(date1,date2) date1 和 date2 参数是合法的日期或日期/时间表达式. 注释:只有值的日期部 ...
- matplotlib画散点图,并在散点处打上相应标签
运行环境: py3.6 matplotlib 2.1.2 x = [2,4,6,7,8,5,4,3] y = [3,6,5,8,4,3,2,4] txt = ['我','今','晚','上','吃', ...
- iOS关于时间的处理
转自:iOS关于时间的处理 做App避免不了要和时间打交道,关于时间的处理,里面有不少门道,远不是一行API调用,获取当前系统时间这么简单.我们需要了解与时间相关的各种API之间的差别,再因场景而异去 ...
- 让你的代码量减少3倍!使用kotlin开发Android(一)
让你的代码量减少3倍!使用kotlin开发Android(一) 创建Kotlin工程 本文同步自博主的私人博客:wing的地方酒馆 写在前面 使用kotlin开发android已经两周多了.得到的好处 ...
- JDK、JRE和JVM的关系
JDK中包含了JRE,JRE中包含了JVM. 详解: JDK是JAVA的核心,包括JRE(JAVA 虚拟环境).编译器等,JDK的主流产品是由SUN公司开发的,JDK本身是用JAVA编写的,安装包的S ...