JAVA全系列 教程
3762个小节阅读:7090.7k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
DI支持注入bean类型、基本数据类型和字符串、List集合、Set集合、Map集合、Properties对象类型等,他们的写法如下:
准备注入属性的类
xxxxxxxxxx
public class StudentService2 {
private StudentDao studentDao; // bean属性
private String name; //字符串类型
private int count; //基本数据类型
private List<String> names; // 字符串类型List集合
private List<Student> students1; // 对象类型List集合
private Set<Student> students2; // 对象类型Set集合
private Map<String,String> names2; // 字符串类型Map集合
private Map<String,Student> students3; // 对象类型Map集合
private Properties properties; //Properties类型
// 省略getter/setter/toString
}
准备测试方法
xxxxxxxxxx
@Test
public void t2(){
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
StudentService studentService = (StudentService) ac.getBean("studentService");
System.out.println(studentService);
}
写法一:
xxxxxxxxxx
<bean id="studentDao" class="com.itbaizhan.dao.StudentDaoImpl"></bean>
<bean id="studentService2" class="com.itbaizhan.service.StudentService2">
<property name="studentDao" ref="studentDao"></property>
</bean>
写法二:
xxxxxxxxxx
<bean id="studentDao" class="com.itbaizhan.dao.StudentDaoImpl"></bean>
<bean id="studentService2" class="com.itbaizhan.service.StudentService2">
<property name="studentDao" >
<ref bean="studentDao"></ref>
</property>
</bean>
简单数据类型包含基本数据类型和字符串,写法如下:
xxxxxxxxxx
<bean id="studentService2" class="com.itbaizhan.service.StudentService2">
<!-- 写法一 name:属性名 value:属性值-->
<property name="name" value="百战"></property>
<!-- 写法二 name:属性名 value:属性值-->
<property name="count">
<value>10</value>
</property>
</bean>
1. 在spring的依赖注入中,<property>
中设置属性值可以注入字符串
A ref
B value
C list
D set
答案
1=>B