JAVA全系列 教程
3762个小节阅读:7094.2k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
Spring有多种获取容器中对象的方式:
配置文件
xxxxxxxxxx
<bean name="studentDao" class="com.itbaizhan.dao.StudentDaoImpl2"></bean>
<bean id="studentDao" class="com.itbaizhan.dao.StudentDaoImpl2"></bean>
获取对象
xxxxxxxxxx
StudentDao studentDao = (StudentDao) ac.getBean("studentDao");
配置文件
xxxxxxxxxx
<bean name="studentDao" class="com.itbaizhan.dao.StudentDaoImpl2"></bean>
获取对象
xxxxxxxxxx
StudentDao studentDao2 = ac.getBean(StudentDao.class);
可以看到使用类型获取不需要强转。
虽然使用类型获取不需要强转,但如果在容器中有一个接口的多个实现类对象,则获取时会报错,此时需要使用类型+id/name获取
配置文件
xxxxxxxxxx
<bean name="studentDao" class="com.itbaizhan.dao.StudentDaoImpl2"></bean>
<bean name="studentDao1" class="com.itbaizhan.dao.StudentDaoImpl"></bean>
获取对象
xxxxxxxxxx
StudentDao studentDao2 = ac.getBean("studentDao",StudentDao.class);
1. 在Spring中,以下无法获取容器中对象的方式是
A 通过id属性获取
B 通过类型获取
C 通过类型+id获取
D 通过scope属性获取
1=>D