JAVA全系列 教程
3762个小节阅读:7089.3k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
被注入类编写有参的构造方法
xxxxxxxxxx
public class StudentService {
private StudentDao studentDao;
public StudentService(StudentDao studentDao) {
this.studentDao = studentDao;
}
}
给需要注入属性值的<bean>
中设置<constructor-arg>
xxxxxxxxxx
<bean id="studentDao" class="com.itbaizhan.dao.StudentDaoImpl"></bean>
<bean id="studentService" class="com.itbaizhan.service.StudentService">
<!-- 依赖注入 -->
<!-- name:对象的属性名 ref:配置文件中注入对象的id值 -->
<constructor-arg name="studentDao" ref="studentDao"></constructor-arg>
</bean>
测试是否注入成功
xxxxxxxxxx
@Test
public void t2(){
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
StudentService studentService = (StudentService) ac.getBean("studentService");
System.out.println(studentService.findStudentById(1));
}
1. 在spring中,<bean>
中配置标签可以调用setter方法进行依赖注入
A <property>
B <value>
C <setter>
D <constructor-arg>
2. 在spring中,<bean>
中配置标签可以调用构造方法进行依赖注入
A <property>
B <value>
C <setter>
D <constructor-arg>
1=>A 1=>D