• Spring——Spring核心基于注解方式的DI实现IoC的设计思想-搭建三层架构项目样例


    说明:本篇博文介绍的是:通过使用注解的方式搭建一个基础三层架构模型。

    DI和IoC概念回顾:

         IoC(控制反转):全称为:Inverse of Control。从字面上理解就是控制反转了,将对在自身对象中的一个内置对象的控制反转,反转后不再由自己本身的对象进行控制这个内置对象的创建,而是由第三方系统去控制这个内置对象的创建。
        DI(依赖注入):全称为Dependency Injection,意思自身对象中的内置对象是通过注入的方式进行创建。

    界面层注解使用@Controller
    业务逻辑层注解使用@Service
    数据访问层使用注解@Repository

    界面层controller

    UsersController

    @Controller
    public class UsersController {
    
        //注意:所有的界面层都会有业务逻辑层的对象(需要调用它的对象方法)
        //使用注解形式创建业务逻辑层对象
        @Autowired
        UsersService usersService ;//= new UsersServiceImpl();
    
        //界面层的功能实现:对外提供访问功能
        public int insert(Users users){
            return usersService.insert(users);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    数据访问层dao

    UsersMapper

    数据访问层接口

    public interface UsersMapper {
        //增加用户到数据库
        int insert(Users users);
    }
    
    • 1
    • 2
    • 3
    • 4

    UsersMapperImpl

    数据访问层实现类

    @Repository
    public class UsersMapperImpl implements UsersMapper{
        @Override
        public int insert(Users users) {
            System.out.println(users.getUserName()+"用户创建成功了");
            return 1;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    实体类pojo

    Users

    public class Users {
    
        private int userId;
        private String userName;
        private int userAge;
    
        public Users() {
        }
    
        @Override
        public String toString() {
            return "Users{" +
                    "userId=" + userId +
                    ", userName='" + userName + '\'' +
                    ", userAge=" + userAge +
                    '}';
        }
    
        public int getUserId() {
            return userId;
        }
    
        public void setUserId(int userId) {
            this.userId = userId;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public int getUserAge() {
            return userAge;
        }
    
        public void setUserAge(int userAge) {
            this.userAge = userAge;
        }
    
        public Users(int userId, String userName, int userAge) {
            this.userId = userId;
            this.userName = userName;
            this.userAge = userAge;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    业务逻辑层service

    业务逻辑层实现类Impl

    UsersServiceImpl

    @Service
    public class UsersServiceImpl implements UsersService {
    
        //注意:所有的业务逻辑层必须有数据访问层的对象
        //使用注解的方式创建引用类型的对象
        @Autowired
        private UsersMapper usersMapper ;//= new UsersMapperImpl();
    
        @Override
        public int insert(Users users) {
    
            //此处可以添加更为复杂的业务功能
    
            //这里直接将数据访问层的用户对象实现添加
            return usersMapper.insert(users);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    UsersService

    业务逻辑层接口

    public interface UsersService {
    
        //实现用户添加的功能
        int insert(Users users);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    resources

    spring配置文件

    对于使用注解方式的DI,只需要在配置文件中指明需要扫描的包即可

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
        
        <context:component-scan base-package="com.lcl">context:component-scan>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    项目核心配置文件pom.xml

    
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
    
        <groupId>com.lclgroupId>
        <artifactId>spring_005_sanceng_annotaionartifactId>
        <version>1.0version>
    
        <name>spring_002_sancengname>
        
        <url>http://www.example.comurl>
    
        <properties>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <maven.compiler.source>1.8maven.compiler.source>
            <maven.compiler.target>1.8maven.compiler.target>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.11version>
                <scope>testscope>
            dependency>
    
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>5.2.5.RELEASEversion>
            dependency>
        dependencies>
    
        <build>
    
        build>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    测试类

    public class MyTest {
    
        @Test
        public void testUser(){
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
            //使用注解创建的对象会默认使用驼峰命名法,所以需要注意getBean方法内填写的内容
            UsersController usersController = (UsersController) ac.getBean("usersController");
            int number = usersController.insert(new Users(100, "李华", 22));
            System.out.println(number);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    完整项目目录如下:
    在这里插入图片描述

  • 相关阅读:
    基于JavaWeb的宿舍管理系统设计与实现
    Linux开源存储全栈详解:从Ceph到容器存储
    【树莓派】raspberry pi控制超声波测距
    用CMake编译项目 & CMake和g++的区别
    编写两位数合并为一个数的程序,用C++及C语言分别实现。
    nextcloud安全与设置警告解决办法,关于您的设置有一些警告
    Java: Proxy Patterns
    网络编程套接字(一) 【简单的Udp网络程序】
    【C++基础入门】41.C++中父子间的冲突
    ide快捷键以及js的小知识
  • 原文地址:https://blog.csdn.net/weixin_44606952/article/details/126670854