JavaWeb后端-Web基础-分层解耦
三层架构

- controller:控制层,接受前端发送的请求,对请求进行处理,并响应数据
/*** 用户信息Controller*/
@RestController()
public class UserController {
// @RequestMapping("/list")
// public List<User> list() throws Exception {
// //1、加载并读取user.txt文件,获取用户数据
// //InputStream in = new FileInputStream(new File("D:\\JavaDevelop\\javaWeb\\web-ai-code\\web-ai-project01\\springboot-web-01\\src\\main\\resources\\user.txt"))
// InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");
// ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8,new ArrayList<>());
// //2、解析用户信息,封装为User对象->list集合
// List<User> userList = lines.stream().map(line -> {
// String[] parts = line.split(",");
// Integer id = Integer.parseInt(parts[0]);
// String Username = parts[1];
// String password = parts[2];
// String name = parts[3];
// Integer age = Integer.parseInt(parts[4]);
// LocalDateTime updateTime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// return new User(id, Username, password, name, age, updateTime);
// }).collect(Collectors.toList());
//
// //3、返回数据(json)
// return userList;
// }private UserService userService=new UserServiceImpl();
@RequestMapping("/list")
public List<User> list() throws Exception {//1、调用service获取数据List<User> userList = userService.findAll();//2、返回数据(json)return userList;
}
}
- service:业务逻辑层,处理具体的业务逻辑
public class UserServiceImpl implements UserService {private UserDao userDao=new UserDaoImpl();@Overridepublic List<User> findAll() {//1、调用dao获取数据List<String> lines = userDao.findAll();//2、解析用户信息,封装为User对象->list集合List<User> userList = lines.stream().map(line -> {String[] parts = line.split(",");Integer id = Integer.parseInt(parts[0]);String Username = parts[1];String password = parts[2];String name = parts[3];Integer age = Integer.parseInt(parts[4]);LocalDateTime updateTime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));return new User(id, Username, password, name, age, updateTime);}).collect(Collectors.toList());return userList;}
}
- dao:数据访问层(Data Access Object)(持久层),负责数据访问操作,包括数据的增删改查
public class UserDaoImpl implements UserDao {@Overridepublic List<String> findAll() {//1、加载并读取user.txt文件,获取用户数据//InputStream in = new FileInputStream(new File("D:\\JavaDevelop\\javaWeb\\web-ai-code\\web-ai-project01\\springboot-web-01\\src\\main\\resources\\user.txt"))InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8,new ArrayList<>());return lines;}
}
分层解耦
耦合:衡量软件中各个层/各个模块的依赖关联程度
内聚:软件中各个功能模块内部的功能联系

控制反转:Inversion Of Control,简称IOC,对象的创建控制权由程序自身转移到外部(容器)
依赖注入:Dependency Injection,简称DI。容器为应用程序提供运行时所依赖的资源,称为依赖注入
Bean对象:IOC容器中创建、管理的对象
IOC&DI入门
步骤
1、将Dao及Service层的实现类,交给IOC容器管理(类前注解@Component)
@Component//将当前类交给IOC容器管理
public class UserDaoImpl implements UserDao {@Overridepublic List<String> findAll() {//1、加载并读取user.txt文件,获取用户数据//InputStream in = new FileInputStream(new File("D:\\JavaDevelop\\javaWeb\\web-ai-code\\web-ai-project01\\springboot-web-01\\src\\main\\resources\\user.txt"))InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8,new ArrayList<>());return lines;}
}
@Component
public class UserServiceImpl implements UserService {@Autowired//应用程序运行时会自动查找该类的bean对象,并赋值给该成员变量private UserDao userDao;@Overridepublic List<User> findAll() {//1、调用dao获取数据List<String> lines = userDao.findAll();//2、解析用户信息,封装为User对象->list集合List<User> userList = lines.stream().map(line -> {String[] parts = line.split(",");Integer id = Integer.parseInt(parts[0]);String Username = parts[1];String password = parts[2];String name = parts[3];Integer age = Integer.parseInt(parts[4]);LocalDateTime updateTime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));return new User(id, Username, password, name, age, updateTime);}).collect(Collectors.toList());return userList;}
}
2、为Controller及Service注入运行时所依赖的对象(成员变量前注解@Autowired)
@Component
public class UserServiceImpl implements UserService {@Autowired//应用程序运行时会自动查找该类的bean对象,并赋值给该成员变量private UserDao userDao;@Overridepublic List<User> findAll() {//1、调用dao获取数据List<String> lines = userDao.findAll();//2、解析用户信息,封装为User对象->list集合List<User> userList = lines.stream().map(line -> {String[] parts = line.split(",");Integer id = Integer.parseInt(parts[0]);String Username = parts[1];String password = parts[2];String name = parts[3];Integer age = Integer.parseInt(parts[4]);LocalDateTime updateTime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));return new User(id, Username, password, name, age, updateTime);}).collect(Collectors.toList());return userList;}
}
@RestController()
public class UserController {
// @RequestMapping("/list")
// public List<User> list() throws Exception {
// //1、加载并读取user.txt文件,获取用户数据
// //InputStream in = new FileInputStream(new File("D:\\JavaDevelop\\javaWeb\\web-ai-code\\web-ai-project01\\springboot-web-01\\src\\main\\resources\\user.txt"))
// InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");
// ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8,new ArrayList<>());
// //2、解析用户信息,封装为User对象->list集合
// List<User> userList = lines.stream().map(line -> {
// String[] parts = line.split(",");
// Integer id = Integer.parseInt(parts[0]);
// String Username = parts[1];
// String password = parts[2];
// String name = parts[3];
// Integer age = Integer.parseInt(parts[4]);
// LocalDateTime updateTime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// return new User(id, Username, password, name, age, updateTime);
// }).collect(Collectors.toList());
//
// //3、返回数据(json)
// return userList;
// }@Autowiredprivate UserService userService;
@RequestMapping("/list")
public List<User> list() throws Exception {//1、调用service获取数据List<User> userList = userService.findAll();//2、返回数据(json)return userList;
}
}
IOC详解

注意:
声明bean时,可通过注解的value属性指定bean的名字,若没有指定,默认为类名首字母小写
DI详解
基于@Autowired进行依赖注入的常见方式:
-
优点:代码简介
缺点:隐藏了类之间的依赖关系,可能破坏类的封装性 -

优点:清晰的看到类的依赖关系,提高代码安全性
缺点:代码繁琐,若构造参数过多,可能导致构造函数臃肿
注意:若只有一个构造函数,@Autowired注解可省略 -

优点:保证了类的封装性,依赖关系更清晰
若存在多个相同的bean,会报错
解决方案

