javaEEday05-分层解耦-IOC&DI-DI详解

This commit is contained in:
2026-05-17 16:30:04 +08:00
parent 7f3ef54c5e
commit bf40a0d815
2 changed files with 8 additions and 1 deletions

View File

@@ -5,7 +5,9 @@ import com.inmind.pojo.Result;
import com.inmind.service.EmpService;
import com.inmind.service.impl.EmpServiceA;
import com.inmind.utils.XmlParserUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -18,6 +20,8 @@ public class EmpController {
//定义业务层对象
@Autowired //运行时,IOC容器会提供该类型的bean对象,并赋值给该变量---依赖注入
@Qualifier("empServiceB") //方式二解决同一类型有多个bean对象
//@Resource(name = "empServiceA") 方式三解决同一类型有多个bean对象
private EmpService empService;//多态
//定义一个获取员工列表的接口

View File

@@ -4,9 +4,12 @@ import com.inmind.dao.EmpDao;
import com.inmind.pojo.Emp;
import com.inmind.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import java.util.List;
//@Primary 方式一解决同一类型有多个bean对象
@Service
public class EmpServiceA implements EmpService {
//获取dao层的对象获取数据
@Autowired