1 解析
1.1 什么是泛型?
1.2 泛型有什么作用?
1.3 泛型装配样式?
2 代码演练
2.1 泛型应用
1 解析
1.1 什么是泛型?
Java泛型设计原则:只要在编译时期没有出现警告,那么运行时期就不会出现ClassCastException异常.
泛型:把类型明确的工作推迟到创建对象或调用方法的时候才去明确的特殊的类型
1.2 泛型有什么作用?
早期Java是使用Object来代表任意类型的,但是向下转型有强转的问题,这样程序就不太安全
首先,我们来试想一下:没有泛型,集合会怎么样
- Collection、Map集合对元素的类型是没有任何限制的。本来我的Collection集合装载的是全部的Dog对象,但是外边把Cat对象存储到集合中,是没有任何语法错误的。
- 把对象扔进集合中,集合是不知道元素的类型是什么的,仅仅知道是Object。因此在get()的时候,返回的是Object。外边获取该对象,还需要强制转换
1.3 泛型装配样式?
@Configuration public class AnimalConfig { @Autowired private Animal<String> dog; @Autowired private Animal<Integer> cat; @Bean public Dog dog() { return new Dog(); } @Bean public Cat cat() { return new Cat(); }
2 代码演练
2.1 泛型应用
实体类:
package com.imooc.beanannotation.javabased; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AnimalConfig { @Autowired private Animal<String> dog; @Autowired private Animal<Integer> cat; @Bean public Dog dog() { return new Dog(); } @Bean public Cat cat() { return new Cat(); } @Bean(name="testAnimal") public Animal testAnimal(){ System.out.println("dog.class is "+dog.getClass().getName()); System.out.println("cat.class is "+cat.getClass().getName()); return new Dog(); } }
测试类:
package com.imooc.test.beanannotation; import org.junit.Test; import com.imooc.beanannotation.javabased.Animal; import com.imooc.beanannotation.javabased.Dog; import com.imooc.beanannotation.javabased.MyDriverManager; import com.imooc.beanannotation.javabased.StringStore; import com.imooc.test.base.UnitTestBase; public class TestJavaBased extends UnitTestBase{ public TestJavaBased(){ super("classpath*:spring-beanannotation.xml"); } @Test public void testStoreConfig(){ System.out.println(super.getbean("store").getClass().getName()); } @Test public void testMyDriverStore(){ MyDriverManager myDriverStore =super.getbean("myDriverStore"); System.out.println(myDriverStore.getClass().getName()); } @Test public void testScope(){ StringStore sStore1 = super.getbean("getStringStore"); System.out.println(sStore1.hashCode()); StringStore sStore2 = super.getbean("getStringStore"); System.out.println(sStore2.hashCode()); } @Test public void testG(){ Animal animal =super.getbean("testAnimal"); } }
dao:
package com.imooc.beanannotation.javabased; public interface Animal<T> { }
impl1:
package com.imooc.beanannotation.javabased; public class Cat implements Animal<Integer> { }
impl2:
package com.imooc.beanannotation.javabased; public class Dog implements Animal<String> { }
xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.imooc.beanannotation"></context:component-scan> </beans>
打印结果:
四月 01, 2019 9:46:35 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@49ab1c60: startup date [Mon Apr 01 21:46:35 CST 2019]; root of context hierarchy 四月 01, 2019 9:46:36 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanannotation.xml] 四月 01, 2019 9:46:36 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [config.xml] dog.class is com.imooc.beanannotation.javabased.Dog cat.class is com.imooc.beanannotation.javabased.Cat 四月 01, 2019 9:46:37 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@49ab1c60: startup date [Mon Apr 01 21:46:35 CST 2019]; root of context hierarchy