spring4泛型初探----一个小例子

本文探讨了Spring4中泛型依赖注入的功能及其与Spring3的区别。通过具体代码示例展示了如何实现不同类型Store的泛型注入,并解释了泛型带来的类型安全性和代码可维护性提升。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

泛型的出现,是为了让代码更规整。
例如
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值