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里 ...
随机推荐
- DDL/DML/DCL区别概述
DDL DDL的概述 DDL(Data Definition Language 数据定义语言)用于操作对象和对象的属性,这种对象包括数据库本身,以及数据库对象,像:表.视图等等,DDL对这些对象和属性 ...
- centos 7 破解密码
CentOS 7 root密码的重置方式和CentOS 6完全不一样,CentOS 7与之前的版本6变化还是比较大的,以进入单用户模式修改root密码为例. 1.重启开机按esc 2.按e ...
- js模拟form表单提交数据, js模拟a标签点击跳转,避开使用window.open引起来的浏览器阻止问题
js模拟form表单提交数据, js模拟a标签点击跳转,避开使用window.open引起来的浏览器阻止问题 js模拟form表单提交数据源码: /** * js模拟form表单提交 * @param ...
- 转:window与linux互相拷贝文件
window与linux互相拷贝文件 借助 PSCP 命令可以实现文件的互拷: 1.下载pscp.exe 文件 (我的资源文件中有) 2.如果想在所有目录可以执行,请更改环境变量. w ...
- JavaScript Window History
window.history 对象包含浏览器的历史. Window History window.history对象在编写时可不使用 window 这个前缀. 为了保护用户隐私,对 JavaScrip ...
- Android RRO机制的运用-----google开机向导客制化
上周五的时候领导分了一个任务,客户让在google开机向导里面增加一页,首先就想到了android的Overlay,然后网上搜了下,发下有很多人写了这方面的技术.而且写的都还不错,所以本篇只当记录作用 ...
- NestedScrollView嵌套ViewPager
NestedScrollView嵌套ViewPager 效果图 重写ViewPager package com.kongqw.kbox.view; import android.content.Con ...
- python用openpyxl操作excel
python操作excel方法 1)自身有Win32 COM操作office但讲不清楚,可能不支持夸平台,linux是否能用不清楚,其他有专业处理模块,如下 2)xlrd:(读excel)表,xlrd ...
- logstash处理文件进度记录机制
假如使用如下配置处理日志 input { file { path => "/home/vagrant/logstash/logstash-2.2.2/dbpool-logs/dev/c ...
- ROS机器人程序设计(原书第2版)补充资料 (零) 源代码、资料和印刷错误修订等 2017年01月01日更新
ROS机器人程序设计(原书第2版)补充资料 (零) 源代码等 ROS官网 版)部分内容修订 页:第1行,删去$ 页:第6行,float64 y 前面加一个空格 页:中间创建主题:下面程序不用换行,(& ...