Spring 05: 用DI(依赖注入)优化Spring接管下的三层项目架构
背景
- 用注解改造前面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
- 项目结构

- 添加包扫描:由上面的项目结构可知,这里的包扫描可行但不是最优做法,因为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
Spring 05: 用DI(依赖注入)优化Spring接管下的三层项目架构的更多相关文章
- Spring 02: Spring接管下的三层项目架构
业务背景 需求:使用三层架构开发,将用户信息导入到数据库中 目标:初步熟悉三层架构开发 核心操作:开发两套项目,对比Spring接管下的三层项目构建和传统三层项目构建的区别 注意:本例中的数据访问层, ...
- Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入
Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入 Spring4整合quartz2.2.3中Job任务使用@Autowired不能注入 >> ...
- 【转】跟我一起学Spring 3(4)–深入理解IoC(控制反转)和DI(依赖注入)
在继续下面的章节之前,我们要先说说大名鼎鼎的IoC和DI. 我们经常会听说IoC,也就是Inversion of Controller,控制反转.事实上,IoC并不是一个新鲜的概念,最早可能是在198 ...
- Java开发学习(六)----DI依赖注入之setter及构造器注入解析
一.DI依赖注入 首先来介绍下Spring中有哪些注入方式? 我们先来思考 向一个类中传递数据的方式有几种? 普通方法(set方法) 构造方法 依赖注入描述了在容器中建立bean与bean之间的依赖关 ...
- Spring 依赖注入优化
Spring 依赖注入优化 原创: carl.zhao SpringForAll社区 今天 Spring 最大的好处就是依赖注入,关于什么是依赖注入,在Stack Overflow上面有一个问题,如何 ...
- 初识Spring框架实现IOC和DI(依赖注入)
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的, IoC是 ...
- Spring控制反转与依赖注入(IOC、DI)
IOC: 反转控制 Inverse Of Control DI:依赖注入 Dependency Injection 目的:完成程序的解耦合 解释:在应用系统的开发过程中,有spring负责对象的创 ...
- Spring详解(三)------DI依赖注入
上一篇博客我们主要讲解了IOC控制反转,也就是说IOC 让程序员不在关注怎么去创建对象,而是关注与对象创建之后的操作,把对象的创建.初始化.销毁等工作交给spring容器来做.那么创建对象的时候,有可 ...
- Spring:(二)DI依赖注入方式
DI 依赖注入 DI(Dependency Injection)依赖注入,说简单一点就将类里面的属性在创建类的过程中给属性赋值,即将对象依赖属性(简单值,集合,对象)通过配置设值给该对象. 属性注入的 ...
随机推荐
- Python with语句和上下文管理器
open("FishC.txt","w")#此处需注意如果被打开的文件中,已有内容,那么用w的方式打开,则会导致原文件内容被截断,也就是相当于被清空了,然后重新 ...
- Spring cloud gateway 如何在路由时进行负载均衡
本文为博主原创,转载请注明出处: 1.spring cloud gateway 配置路由 在网关模块的配置文件中配置路由: spring: cloud: gateway: routes: - id: ...
- Linux查看系统参数配置
Linux查看系统参数 1.查看内存(以GB为单位) [root@rac1 ~]# free -g total :内存总数,物理内存总数 used :已使用内存 free :空闲的内存数 shared ...
- 一些好用的javascript/typescript方法封装分享
1.数字格式化 JS版-直接写到原型链上 /** * @author: silencetea * @name: * @description: 数字格式化,默认每三位用英文逗号分隔 * @param ...
- iNeuOS工业互联网操作系统,增加搜索应用、多数据源绑定、视图背景设置颜色、多级别文件夹、组合及拆分图元
目 录 1. 概述... 2 2. 搜索应用... 2 3. 多数据源绑定... 3 4. 视图背景设置颜色... 4 5. 多级别文件夹 ...
- C语言学习之我见-strcpy()字符串复制函数
strcpy()函数,用于两个字符串值的复制. (1)函数原型 char * strcpy(char * _Dest,const char * _Source); (2)头文件 string.h (3 ...
- Linux shell 2>&1的意思
在脚本里经常看到 ./xxx.sh > /dev/null 2>&1 ./xxx.sh > log.file 2>&1 在shell中输入输出都有对应的文件描述 ...
- Ubuntu系统iptables安全防护整改计划
端口开放 默认防火墙是开放所有端口的,如果拿来做应用服务器,就很危险,所以要把防火墙用起来,只将需要的端口开放,ubuntu用的是iptables防火墙. iptables处理流程 iptables ...
- windows下docker部署报错
报错信息:Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:8848 -> 0.0.0 ...
- Mybatis SqlNode源码解析
1.ForEachSqlNode mybatis的foreach标签可以将列表.数组中的元素拼接起来,中间可以指定分隔符separator <select id="getByUserI ...