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

This commit is contained in:
2026-05-17 15:56:49 +08:00
parent f72aadf813
commit 7f3ef54c5e
7 changed files with 19 additions and 7 deletions

View File

@@ -46,6 +46,10 @@
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -2,8 +2,11 @@ package com.inmind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
@SpringBootApplication
//@ComponentScan({"com.inmind","dao"})//一旦手动添加了@ComponentScan那么原始扫描路径就失效了必须全部手动设置
public class SpringbootWebReqRespApplication {
public static void main(String[] args) {

View File

@@ -6,6 +6,8 @@ import com.inmind.service.EmpService;
import com.inmind.service.impl.EmpServiceA;
import com.inmind.utils.XmlParserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

View File

@@ -3,11 +3,12 @@ package com.inmind.dao.impl;
import com.inmind.dao.EmpDao;
import com.inmind.pojo.Emp;
import com.inmind.utils.XmlParserUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import java.util.List;
@Component //将当前类交给IOC容器管理,成为IOC容器的bean -- 控制反转
//@Component //将当前类交给IOC容器管理,成为IOC容器的bean -- 控制反转
@Repository
public class EmpDaoA implements EmpDao {
@Override
public List<Emp> listEmp() {

View File

@@ -1,11 +1,9 @@
package com.inmind.service.impl;
import com.inmind.dao.EmpDao;
import com.inmind.dao.impl.EmpDaoA;
import com.inmind.pojo.Emp;
import com.inmind.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;

View File

@@ -4,11 +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.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.List;
@Component
//@Component
@Service
public class EmpServiceB implements EmpService {
//获取dao层的对象获取数据
@Autowired

View File

@@ -3,12 +3,15 @@ package com.inmind.utils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Component;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
@Component
public class XmlParserUtils {
public static <T> List<T> parse(String file , Class<T> targetClass) {