xml的Spring三层项目架构

简介: 需求:使用三层架构开发,将用户信息导入到数据库中目标:初步熟悉三层架构开发核心操作:开发两套项目,对比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框架去接手管理

  • 在maven项目中添加Spring依赖和applicationContext.xml的操作不再赘述,可参考spring博客集中Spring 01的博客

业务逻辑层

  • 实现类修改为
//此时的业务逻辑层实现类:不再手动创建数据访问层的对象,交给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
相关文章
|
8天前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
40 7
|
10天前
|
传感器 监控 安全
智慧工地云平台的技术架构解析:微服务+Spring Cloud如何支撑海量数据?
慧工地解决方案依托AI、物联网和BIM技术,实现对施工现场的全方位、立体化管理。通过规范施工、减少安全隐患、节省人力、降低运营成本,提升工地管理的安全性、效率和精益度。该方案适用于大型建筑、基础设施、房地产开发等场景,具备微服务架构、大数据与AI分析、物联网设备联网、多端协同等创新点,推动建筑行业向数字化、智能化转型。未来将融合5G、区块链等技术,助力智慧城市建设。
|
13天前
|
人工智能 JavaScript 安全
【01】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-需求改为思维导图-设计数据库-确定基础架构和设计-优雅草卓伊凡商业项目实战
【01】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-需求改为思维导图-设计数据库-确定基础架构和设计-优雅草卓伊凡商业项目实战
57 13
【01】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-需求改为思维导图-设计数据库-确定基础架构和设计-优雅草卓伊凡商业项目实战
|
21天前
|
XML Java 开发者
Spring底层架构核心概念解析
理解 Spring 框架的核心概念对于开发和维护 Spring 应用程序至关重要。IOC 和 AOP 是其两个关键特性,通过依赖注入和面向切面编程实现了高效的模块化和松耦合设计。Spring 容器管理着 Beans 的生命周期和配置,而核心模块为各种应用场景提供了丰富的功能支持。通过全面掌握这些核心概念,开发者可以更加高效地利用 Spring 框架开发企业级应用。
74 18
|
1月前
|
开发框架 前端开发 .NET
一个适用于 .NET 的开源整洁架构项目模板
一个适用于 .NET 的开源整洁架构项目模板
57 26
|
1月前
|
搜索推荐 NoSQL Java
微服务架构设计与实践:用Spring Cloud实现抖音的推荐系统
本文基于Spring Cloud实现了一个简化的抖音推荐系统,涵盖用户行为管理、视频资源管理、个性化推荐和实时数据处理四大核心功能。通过Eureka进行服务注册与发现,使用Feign实现服务间调用,并借助Redis缓存用户画像,Kafka传递用户行为数据。文章详细介绍了项目搭建、服务创建及配置过程,包括用户服务、视频服务、推荐服务和数据处理服务的开发步骤。最后,通过业务测试验证了系统的功能,并引入Resilience4j实现服务降级,确保系统在部分服务故障时仍能正常运行。此示例旨在帮助读者理解微服务架构的设计思路与实践方法。
103 17
|
1月前
|
监控 JavaScript 数据可视化
建筑施工一体化信息管理平台源码,支持微服务架构,采用Java、Spring Cloud、Vue等技术开发。
智慧工地云平台是专为建筑施工领域打造的一体化信息管理平台,利用大数据、云计算、物联网等技术,实现施工区域各系统数据汇总与可视化管理。平台涵盖人员、设备、物料、环境等关键因素的实时监控与数据分析,提供远程指挥、决策支持等功能,提升工作效率,促进产业信息化发展。系统由PC端、APP移动端及项目、监管、数据屏三大平台组成,支持微服务架构,采用Java、Spring Cloud、Vue等技术开发。
|
2月前
|
XML Java 数据格式
【SpringFramework】Spring IoC-基于XML的实现
本文主要讲解SpringFramework中IoC和DI相关概念,及基于XML的实现方式。
116 69
|
2月前
|
JSON 前端开发 Java
|
2月前
|
存储 JSON 前端开发
【Spring项目】表白墙,留言板项目的实现
本文主要介绍了表白墙项目的实现,包含前端和后端代码,以及测试