Xml的Spring三层项目架构,你学会了吗?

开发 架构
对传统三层项目构建的修改:由上述测试分析中"对象访问的变化可知",需要用到的实现类有:UserController,UserServiceImpl,UserMapperImpl在Spring接管下,需要在bean工厂中,注册上述实体类的对象,将原先需要程序员手动创建管理的对象交给Spring框架去接手管理

业务背景

  • 需求:使用三层架构开发,将用户信息导入到数据库中
  • 目标:初步熟悉三层架构开发
  • 核心操作:开发两套项目,对比Spring接管下的三层项目构建和传统三层项目构建的区别
  • 注意:本例中的数据访问层,先不连接数据库,只是进行简单数据模拟

非Spring接管下的三层项目构建

实体类 + 各访问层

  • 实体类:com.example.pojoUser 实体类User实体类默认含有:无参构造方法 + 全属性的(有参构造方法 + getter,setter方法 + toString方法)
  • 数据访问层:com.example.daoUserMapper.java(接口)UserMapperImpl.java (实现类)
  • 业务逻辑层:com.example.serviceUserService.java (接口)UserServiceImpl.java (实现类)
  • 界面层:com.example.controllerUserController.java

项目结构

实体类

package com.example.pojo;

public class User {
private String name;
private int age;
private String address;
}

数据访问层

  • 接口
package com.example.dao;

import com.example.pojo.User;

/**
* 数据访问层接口
*/
public interface UserMapper {
//导入用户信息
int insertUser(User user);
}
  • 实现类
package com.example.dao;

import com.example.pojo.User;

/**
* 数据访问层的实现类
*/
public class UserMapperImpl implements UserMapper{

//模拟用户信息导入
@Override
public int insertUser(User user) {
System.out.println("用户: " + user.getName() + ", 导入成功!");
return 1;
}
}

业务逻辑层

  • 接口
package com.example.Service;

import com.example.pojo.User;

/**
* 业务逻辑层接口
*/
public interface UserService {
//导入用户数据的功能
int insertUser(User user);
}
  • 实现类
package com.example.Service.impl;

import com.example.Service.UserService;
import com.example.dao.UserMapper;
import com.example.dao.UserMapperImpl;
import com.example.pojo.User;

/**
* 业务逻辑层实现类
*/
public class UserServiceImpl implements UserService {
//数据访问层接口指向数据访问层实现类
UserMapper userMapper = new UserMapperImpl();

@Override
public int insertUser(User user) {
return userMapper.insertUser(user);
}
}

界面层

package com.example.controller;

import com.example.Service.UserService;
import com.example.Service.impl.UserServiceImpl;
import com.example.pojo.User;

/**
* 界面层
*/
public class UserController {
//业务逻辑层接口指向业务逻辑层实现类
UserService userService = new UserServiceImpl();

public int insertUser(User user){
return userService.insertUser(user);
}
}

测试

package com.example.test;

import com.example.controller.UserController;
import com.example.pojo.User;
import org.junit.Test;

public class TestInsert {
//测试非Spring框架的简单三层架构
@Test
public void testInsertUser(){
UserController userController = new UserController();
int num = userController.insertUser(new User("荷包蛋", 20, "黑河"));
if(num == 1){
System.out.println("非Spring框架的简单三层架构,运行成功!");
}else{
System.out.println("非Spring框架的简单三层架构,运行失败!");
}
}
}

输出结果

用户: 荷包蛋, 导入成功!
Spring接管下的简单三层架构,运行成功!

Process finished with exit code 0

测试分析

  • 测试执行流程示意图

测试分析层级变化:界面层 --> 业务逻辑层 --> 数据访问层 --> 业务逻辑层 --> 界面层对象访问的变化:界面层对象 --> 业务逻辑层接口指向业务逻辑层实现类 --> 数据访问层接口指向数据访问层实现类 --> 数据访问层实现类完成对数据的操作方法调用变化:界面层对象的insertUser(User u) --> 业务逻辑层实现类的insertUser(User u) --> 数据访问层实现类的insertUser(User u)

Spring接管下的三层项目构建

对传统三层项目构建的修改:由上述测试分析中"对象访问的变化可知",需要用到的实现类有:UserController,UserServiceImpl,UserMapperImpl在Spring接管下,需要在bean工厂中,注册上述实体类的对象,将原先需要程序员手动创建管理的对象交给Spring框架去接手管理

业务逻辑层

实现类修改为:

//此时的业务逻辑层实现类:不再手动创建数据访问层的对象,交给Spring容器来管理,新增:setter方法和无参构造函数

package com.example.Service.impl;

import com.example.Service.UserService;
import com.example.dao.UserMapper;
import com.example.pojo.User;

/**
* 业务逻辑层实现类
*/
public class UserServiceImpl implements UserService {
//数据访问层接口指向数据访问层实现类
public UserMapper userMapper;

public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}

public UserServiceImpl() {
}

@Override
public int insertUser(User user) {
return userMapper.insertUser(user);
}
}

界面层

  • 所做修改与对业务逻辑层的实现类的修改类似:
package com.example.controller;

import com.example.Service.UserService;
import com.example.pojo.User;

/**
* 界面层
*/
public class UserController {
//业务逻辑层接口指向业务逻辑层实现类
UserService userService;

public void setUserService(UserService userService) {
this.userService = userService;
}

public UserController() {
}

public int insertUser(User user){
return userService.insertUser(user);
}
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- bean工厂 -->
<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">

<!-- 注册UserMapper实现类对象-->
<bean id="uMapperImpl" class="com.example.dao.UserMapperImpl">
</bean>

<!-- 注册UserService实现类对象-->
<bean id="uServiceImpl" class="com.example.Service.impl.UserServiceImpl">
<property name="userMapper" ref="uMapperImpl"/>
</bean>

<!-- 注册UserController对象-->
<bean id="uController" class="com.example.controller.UserController">
<property name="userService" ref="uServiceImpl"/>
</bean>

</beans>

测试

package com.example.test;

import com.example.controller.UserController;
import com.example.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestInsert {
//测试Spring接管下的简单三层架构
@Test
public void testInsertUser(){
//创建Spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//取出界面层对象
UserController uController = (UserController) applicationContext.getBean("uController");
//调用界面层对象方法
int num = uController.insertUser(new User("荷包蛋", 20, "黑河"));
if(num == 1){
System.out.println("Spring接管下的简单三层架构,运行成功!");
}else{
System.out.println("Spring接管下的简单三层架构,运行失败!");
}
}
}

输出结果

用户: 荷包蛋, 导入成功!
Spring接管下的简单三层架构,运行成功!

Process finished with exit code 0


责任编辑:武晓燕 来源: 今日头条
相关推荐

2022-12-26 07:48:04

敏捷项目

2024-03-04 07:41:18

SpringAOPOOP​

2023-05-05 08:29:15

Spring后台服务器

2022-07-11 09:00:37

依赖配置文件Mybati

2022-04-26 08:41:54

JDK动态代理方法

2024-01-26 08:24:16

Dalvik架构ART

2022-07-08 09:27:48

CSSIFC模型

2024-01-19 08:25:38

死锁Java通信

2023-07-26 13:11:21

ChatGPT平台工具

2024-02-04 00:00:00

Effect数据组件

2023-01-10 08:43:15

定义DDD架构

2024-01-02 07:04:23

2023-03-09 11:34:00

项目CMakeST

2024-01-30 18:29:29

微服务架构Ingress

2024-02-02 11:03:11

React数据Ref

2023-01-30 09:01:54

图表指南图形化

2023-12-12 08:02:10

2023-10-10 11:04:11

Rust难点内存

2024-01-02 12:05:26

Java并发编程

2023-08-01 12:51:18

WebGPT机器学习模型
点赞
收藏

51CTO技术栈公众号