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

时间:2023-03-09 16:20:47
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