springboot泛型注入,spring多例注入
目录
1.弹簧泛型注入2.关于爪哇岛泛型有四种TypeGenericArrayType泛型数组类型参数化类型参数化类型类型参数类型变量通配符类型通配符类型
1.Spring泛型注入
创建一个抽象泛型类BaseDao,有参数化类型英语字母表中第二十个字母
公共抽象类BaseDaoT {公共抽象void eat();}每种动物有不同的行为,猫、狗
公类猫{}公类狗{ }分别继承库连接和关闭类实现不同的行为
@Repositorypublic类卡特道扩展BaseDaoCat { @ Override public void eat(){ system。出去。println(猫吃…));}}@Repositorypublic类道格道扩展BaseDaoDog { @ Override public void eat(){ system。出去。println(吃狗肉…);}}接着创建一个抽象业务类,也有参数化类型t。
注意:此处不能使用@资源注入,会找到多个库连接和关闭类类型的豆子,无法确认注入哪一个豆会报错
需要使用@自动连线注入,它有根据泛型参数匹配的逻辑,会一个个去匹配
公共抽象类baseservice t { @ Autowired private baseDao t baseDao;受保护的void eat(){基础刀。吃();}}子类继承基础服务指定参数化类型实现注入相应的库连接和关闭类
@ service public class cat服务扩展BaseServiceCat { } @ service public class dog服务扩展BaseServiceDog { }代码目录
测试
@ Configuration @ components可以(value= com。莫尼安。测试。泛型’)公共类泛型配置{ }公共类泛型测试{ public static void main(String[]args){ AnnotationConfigApplicationContext application context=new AnnotationConfigApplicationContext();应用程序上下文。寄存器(通用配置。类);应用程序上下文。刷新();狗服务狗服务=应用上下文。获取bean(狗服务。类);卡特彼勒服务=应用环境。获取bean(猫服务。类);狗服务。吃();卡特彼勒服务。吃();}}结果输出,成功注入相应的库连接和关闭类
2. 关于java泛型有四种Type
GenericArrayType泛型数组类型
公共类GenericArrayTypeTest {公共静态类TestClassS { //泛型数组个人分发名单
t;[] lists; // 泛型数组 private S[] ss; private String[] s; } public static void main(String[] args) { Class cl = TestClass.class; for (Field field : cl.getDeclaredFields()) { System.out.println("fileds name:" + field.getName()); Type genericType = field.getGenericType(); System.out.println("Is it genericArrayType:" + (genericType instanceof GenericArrayType)); if (genericType instanceof GenericArrayType) { GenericArrayType genericArrayType = (GenericArrayType) genericType; System.out.println(genericArrayType.getTypeName()); Type genericComponentType = genericArrayType.getGenericComponentType(); System.out.println(genericComponentType); } System.out.println(); } }}result:
filed's name:listsIs it genericArrayType:truejava.util.List<java.lang.String>[]java.util.List<java.lang.String>filed's name:ssIs it genericArrayType:trueS[]Sfiled's name:sIs it genericArrayType:false
ParameterizedType参数化类型
public class ParameterizedTypeTest { public static abstract class Test<T> { public abstract void test(T t); } public static class TestClass<T extends Number> extends Test<Integer> { private List<T> tList; private List<? extends Number> list; private Map<String, Integer> map; @Override public void test(Integer o) { } } public static void main(String[] args) { TestClass<Integer> tt = new TestClass<>(); Class cl = tt.getClass(); Type genericSuperclass = cl.getGenericSuperclass(); assert genericSuperclass instanceof ParameterizedType; ParameterizedType parameterizedType1 = (ParameterizedType) genericSuperclass; System.out.println(parameterizedType1.getActualTypeArguments()[0]); for (Field field : cl.getDeclaredFields()) { System.out.println("fields name:" + field.getName()); Type genericType = field.getGenericType(); System.out.println("Is it ParameterizedType:" + (genericType instanceof ParameterizedType)); if (genericType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) genericType; for (Type type : parameterizedType.getActualTypeArguments()) { System.out.println("actualType:" + type); System.out.println(type.getTypeName()); } } System.out.println(); } }}
result:
class java.lang.Integerfield's name:tListIs it ParameterizedType:trueactualType:TTfield's name:listIs it ParameterizedType:trueactualType:? extends java.lang.Number? extends java.lang.Numberfield's name:mapIs it ParameterizedType:trueactualType:class java.lang.Stringjava.lang.StringactualType:class java.lang.Integerjava.lang.Integer
TypeVariable 类型变量
public class TypeVariableTest { public static class TestClass<S extends Number, T> { private Map<S, T> map; } public static void main(String[] args) throws Exception { Class cl = TestClass.class; Field field = cl.getDeclaredField("map"); Type genericType = field.getGenericType(); ParameterizedType parameterizedType = (ParameterizedType) genericType; for (Type type : parameterizedType.getActualTypeArguments()) { TypeVariable typeVariable = (TypeVariable) type; // 类型变量名 System.out.println(typeVariable.getName()); // 变量上边界 Type[] bounds = typeVariable.getBounds(); System.out.println(Arrays.toString(bounds)); } }}
result:
S[class java.lang.Number]T[class java.lang.Object]
WildcardType 通配符类型
public class WildcardTypeTest { public static class TestClass { private List<? extends Number> lists; private Set<?> sets; private Map<? extends Number, ? super String> map; } public static void main(String[] args) { Class cl = TestClass.class; for (Field field : cl.getDeclaredFields()) { System.out.println("fileds name:" + field.getName()); Type genericType = field.getGenericType(); ParameterizedType parameterizedType = (ParameterizedType) genericType; for (Type type : parameterizedType.getActualTypeArguments()) { // 通配符类型 WildcardType wildcardType = (WildcardType) type; System.out.println(wildcardType.getTypeName()); // 上边界 System.out.println("上边界" + Arrays.toString(wildcardType.getUpperBounds())); // 下边界 System.out.println("下边界" + Arrays.toString(wildcardType.getLowerBounds())); } System.out.println(); } }}
result:
filed's name:lists? extends java.lang.Number上边界[class java.lang.Number]下边界[]filed's name:sets?上边界[class java.lang.Object]下边界[]filed's name:map? extends java.lang.Number上边界[class java.lang.Number]下边界[]? super java.lang.String上边界[class java.lang.Object]下边界[class java.lang.String]
注:spring对泛型的解析主要是在ResolvableType类,掌握上述的基本知识后可以去阅读下相关源码
到此这篇关于Spring实现泛型注入的示例详解的文章就介绍到这了,更多相关Spring泛型注入内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。