Spring课程 Spring入门篇 4-8 Spring bean装配之基于java的容器注解说明--基于泛型的自动装配
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
Spring课程 Spring入门篇 4-8 Spring bean装配之基于java的容器注解说明--基于泛型的自动装配的更多相关文章
- Spring @Bean注解 (基于java的容器注解)
基于java的容器注解,意思就是使用Java代码以及一些注解,就可以取代spring 的 xml配置文件. 1-@Configuration & @Bean的配合 @Configuration ...
- Spring课程 Spring入门篇 4-7 Spring bean装配之基于java的容器注解说明--@Scope 控制bean的单例和多例
1 解析 1.1 bean的单例和多例的应用场景 1.2 单例多例的验证方式 1.3 @Scope注解单例多例应用 2 代码演练 2.1 @Scope代码应用 1 解析 1.1 bean的单例和多例的 ...
- Spring课程 Spring入门篇 4-6 Spring bean装配之基于java的容器注解说明--@ImportResource和@Value java与properties文件交互
1 解析 1.1 这两个注解应用在什么地方 1.2 应用方式 1.3 xml方式实现取值 2 代码演练 2.1 @ImportResource和@Value代码演练 1 解析 1.1 这两个注解应用在 ...
- Spring课程 Spring入门篇 4-5 Spring bean装配之基于java的容器注解说明--@Bean
1 解析 2.1 @bean注解定义 2.2 @bean注解的使用 2 代码演练 2.1 @bean的应用不带name 2.2 @bean的应用带name 2.3 @bean注解调用initMet ...
- Spring学习十一----------Bean的配置之基于Java的容器注解@Bean
© 版权声明:本文为博主原创文章,转载请注明出处 @Bean -@Bean标识一个用于配置和初始化一个由SpringIOC容器管理的新对象的方法,类似于XML配置文件的<bean/> -可 ...
- Spring Boot -01- 快速入门篇(图文教程)
Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...
- Spring实践系列-入门篇(一)
本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...
- Spring Cloud Alibaba入门篇
学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ...
- Spring Data JPA 入门篇
Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...
随机推荐
- loj #2255. 「SNOI2017」炸弹
#2255. 「SNOI2017」炸弹 题目描述 在一条直线上有 NNN 个炸弹,每个炸弹的坐标是 XiX_iXi,爆炸半径是 RiR_iRi,当一个炸弹爆炸时,如果另一个炸弹所在位置 X ...
- 关于fatal error LINK1123:failure during conversion to COFF:file invalid or corrupt
今天用Visual Studio 2010编译postgresql工程时突然遇到下面这个编译错误: fatal error LINK1123:failure during conversion to ...
- SDUT OJ 多项式求和
多项式求和 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Description 多项式描述 ...
- libcaffe.so.1.0.0: cannot open shared object file: No such file or directory 运行时报错
caffe安装好后lib没有配置到/usr/lib或/usr/local/lib中,需手动配置: sudo vim ~/.bashrc export LD_LIBRARY_PATH=your_path ...
- C#实现,一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第35位数是多少, 用递归算法实现
方法函数可以通过调用自身来进行递归.计算理论可以证明递归的作用可以完全取代循环. static void Main(string[] args) { Console.WriteLine(Ra()); ...
- Windows10 下安装 MySQL Workbench + Thinkphp
昨天,搭建了最基本的 W + I + M + P 环境,今天把 workbench 装上,毕竟效率是第一位的,还不是吾装的时候. MySQL.org 下载最新的 workbench,一路安装倒是没有任 ...
- 洛谷 P3388 【模板】割点(割顶)
题目链接 题解 今天复习了一下割点. 关于\(tarjan\)这里不多讲 \(dfn\)和\(low\)数组的定义想必大家都知道 仔细观察一下,可以发现 假设便利\(u->v\)这条边 如果 \ ...
- 【2-SAT】【并查集】NOIp模拟题 植树方案 题解
一个类似2-SAT的思想,但是简化了很多.只需要用到并查集实现. 题目描述 企鹅国打算种一批树.所谓树,就是由$N$个结点与$N-1$条边连接而成的连通无向图.企鹅国的国王对于这些树有下列要求 ...
- nvm安装
1.下载安装包,地址:https://github.com/coreybutler/nvm-windows 2.解压后,点击 nvm-setup 安装,一路默认安装,next下去 3.打开安装位置,然 ...
- 那些熟悉又陌生的 css2、css3 样式,持续复习
initial关键字: 除了 Internet Explorer,其他的主流浏览器都支持 initial 关键字. Opera 15 之前的版本不支持 initial 关键字. initial ...