首页 > 其他分享 >Spring 05: 用DI(依赖注入)优化Spring接管下的三层项目架构

Spring 05: 用DI(依赖注入)优化Spring接管下的三层项目架构

时间:2022-08-21 13:44:52浏览次数:52  
标签:05 Spring DI public org import com example

背景

  • 用注解改造前面Spring博客集里(指 Spring 02)Spring接管下的三层项目架构
  • 对前面Spring博客集里(指 Spring 04)@Controller + @Service + @Repository 3个注解的用法进行演示

实现类

数据访问层

  • 数据访问层的实现类:添加@Repository注解,专门用来创建数据访问层对象
package com.example.dao;

import com.example.pojo.User;
import org.springframework.stereotype.Repository;

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

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

业务逻辑层

  • 业务逻辑层的实现类:添加@Service注解,专门用来创建业务逻辑层对象
  • 同时,由于持有数据访问层的接口类型的成员变量,需要添加@Autowired注解
  • 注意:对于@Autowired,这里是同源类型注入的情况3:存在接口和实现类关系的注入。上面注册了数据访问层的实现类对象,这里注入接口变量中,面向接口编程
package com.example.Service.impl;

import com.example.Service.UserService;
import com.example.dao.UserMapper;
import com.example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 业务逻辑层实现类
 */
@Service
public class UserServiceImpl implements UserService {

    //数据访问层接口指向数据访问层实现类
    @Autowired
    UserMapper userMapper;
    
    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }
}

界面层

  • 界面层:添加@Controller注解,专门用来创建界面层对象

  • 同时,由于持有业务逻辑层的接口类型的成员变量,需要添加@Autowired注解,也是同源类型注入的情况3:存在接口和实现类关系的注入

package com.example.controller;

import com.example.Service.UserService;
import com.example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * 界面层
 */
@Controller
public class UserController {
    
    //业务逻辑层接口指向业务逻辑层实现类
    @Autowired
    UserService userService;
    
    public int insertUser(User user){
        return userService.insertUser(user);
    }
}

applicationContext.xml

  • 项目结构

image

  • 添加包扫描:由上面的项目结构可知,这里的包扫描可行但不是最优做法,因为pojo包根本不会交给容器创建对象但也要扫描,而且要进入impl包才能扫描到UserServiceImpl类。扫描做法以后会改进
<?xml version="1.0" encoding="UTF-8"?>
<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.example"/>
    
</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 ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从容器中获取UserController对象
        UserController uc = (UserController) ac.getBean("userController");
        //完成用户数据的导入
        uc.insertUser(new User("荷包蛋", 20, "黑河"));
    }
}

测试输出

用户: 荷包蛋, 导入成功!

Process finished with exit code 0

标签:05,Spring,DI,public,org,import,com,example
From: https://www.cnblogs.com/nefu-wangxun/p/16609882.html

相关文章

  • Java学习 (25) 对象篇(05)抽象类&接口
    目录抽象类语法实例注意点具体讲解视频(狂神说Java)接口语法实例具体讲解视频(狂神说Java)抽象类abstract修饰符可以用来修饰方法也可以修饰类,如果修饰方法,那么该方法就是......
  • CF(div2)816 A~C
    ACrossmarket思维矩阵走路径,发现走Z字型怎么走都是一样的耗费,所以直接O(1)算出来就好/**|~~~~~~~|*......
  • spring5 事务
    1.事务介绍1.1事务添加到javaEE的service1.2声明式事务和编程试1.3声明式xml事务开发注解方式1.4在Spring中进行事务开发底层用的aop原理1.5Spring事......
  • redis的概述以及redis的下载与安装
    redis的概述概念:redis是一款高性能的NOSQL系列的非关系型数据库关系型数据库:1、数据之间有关联关系2、数据存储在硬盘文件中非关系型数据库:1、数据......
  • div内多行文字, 溢出部分用省略号显示
     1/*内容*/2.content{3width:100%;4height:70px;5font-size:small;6margin-top:10px;78text-overflow:-o-ellipsis-lastline;9......
  • Codeforces Round #816 (Div. 2)
    题面A.Crossmarket不妨设\(n\gem\),则答案为\(n+2(m-1)\)。特别地,\(n=1,m=1\)时答案为\(0\),注意特判。ViewCode#include<stdio.h>#include<algorithm>int......
  • [Codeforces Round #816 (Div. 2)] D. 2+ doors
    这次Div.2比之前我打的有些要难啊,前三道题就耗了好多时间,D题干脆摆烂了。。。还是太逊了对于一个\(x\),有\(x|y_i=z_i\),那么我们设\(num[x]=z_1\)&\(z_2\)&\(z_3\)..........
  • Python小游戏——外星人入侵(保姆级教程)第一章 05
    系列文章目录第一章:武装飞船05:重构:模块game_functions一、重构在大型项目中,经常需要在添加新代码前重构既有代码。重构旨在简化既有代码的结构,使其更容易扩展。在本节......
  • SpringBoot的基本概念(1)
     1.为什么使用springboot springboot容易上手,做了jar包的版本控制,不用考虑maven依赖,方便敏捷开发,内置tomcat减少开发配置,由于SPI提供对外的starter扩展。 2.spr......
  • redis概述和redis下载安装
    redis概述1.概念:redis是一款高性能的NOSQL系列的非关系型数据库1.1.什么是NOSQLNoSQL(NoSQL=NotOnlySQL),意即“不仅仅是SQL”,是一项全新的数据库理念,......