【Spring框架03】DI依赖注入,java面试必问问题

本文详细介绍了Spring框架中的依赖注入(DI)原理,包括构造器注入和属性注入,并展示了如何通过XML配置文件进行配置。此外,还讨论了使用p和c标签简化XML配置的方法,以及集合list的注入。对于Java后端开发者和面试准备者,这篇内容提供了深入理解Spring DI的基础知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

}

public void setUserDao(UserDao userDao) {

this.userDao = userDao;

}

}

2.编写引用的对象类

public class UserDao {

public void add(){

System.out.println(“UseDao.add>>>>>>>>>”);

}

}

3.spring.xml配置

标签内 name表示创建对象的名字

标签内 ref表示中的id

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

二.构造器注入(必须存在构造方法)


1.编写构造方法

这里是传参构造

public class AccountService {

public AccountDao accountD

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值