当前位置:网站首页>开发过程中的总结 BaseService 为所有的 Controller或Service 提供一个公共获取 Service 的文件,减少重复注入
开发过程中的总结 BaseService 为所有的 Controller或Service 提供一个公共获取 Service 的文件,减少重复注入
2022-07-22 20:43:00 【极光雨雨】
BaseService 实际的意义
BaseService 也可以起名叫 ServiceUtil 等等这个比较随意,总的来说这个类是用来将一些再不同 Controller 或者 Service 中重复使用 @Autowired 或者 @Resource 注入的属性等只做一次注入,从而减少多个 Controller 或者 Service 中反复注入,可以减少代码量,看起来更整洁,同时只维护一两个基本 service 类,也会轻松许多。
第一种 抽象类方式统一注入,使用 controller 或者 Service 继承该类
具体示例代码如下:
@Service("BaseService")
public abstract class BaseService {
private static final MyLog logger = new MyLog(BaseService.class);
protected static final String IDENTITY = "identity";
@Resource(name = "AService")
protected IAService aService;
@Resource(name = "CService")
protected ICService cService;
@Resource(name = "BService")
protected IBService bService;
@Autowired
protected IRService rService;
@Autowired
protected CMappingService cMappingService;
........
........
........
// 同时可以提供一些常用的方法进来提供外部controller 使用,只有服务注入也可以
其他 Controller 或者 Service 中如下方式继承,则可以省去重复注入, 为了减少Controller 中的注入,甚至可以直接在不同的 Service 继承,controller 可以完全不使用。
但这种方式通过继承可能会存在并不使用的类也被继承下来,最终并没有使用。这是一种好的方式但是还是存在部分浪费的嫌疑。
@Service("CubeService")
public class CubeService extends BaseService {
// 正常Service 中的业务处理
........................
........................
........................
........................
........................
}
通过Util工具类的方式 直接注入静态的服务类 为 controller 或者 Service 使用
这种方式看起来更好一些,将不同的服务类直接通过 static final 的方式在类内静态声明(需要先从applicationContext中获取),当其成为一个静态属性时,其他的 controller 或者 service 直接通过类名 . 的方式获取对应的单个属性,从而使得精确引用某一个或几个服务,不会造成浪费,但可能写起来看着略长一些。
示例代码如下:
public class ServiceUtils {
public static final IEService eService = ToolSpring.getBean(IEService.class);
public static final IEBService ebService = ToolSpring.getBean(IEBService.class);
public static final IAService aService = ToolSpring.getBean(IAService.class);
public static final ICService cService = ToolSpring.getBean(ICService.class);
public static final PFactory pFactory = ToolSpring.getBean(PFactory.class);
.....................
.....................
.....................
.....................
.....................
}
其中ToolSpring 为实现了 Spring 接口 ApplicationContextAware 的一个类 通过 类名 或 类class 来获取对应的 ApplicationContext 中的 Bean 获取注入到容器中的类)
具体见我的其他文章
Spring ApplicationContextAware 作用以及使用学习总结 spring bean初始化流程总结
ApplicationContext 获取Spring 中的Bean 理解以及方式,注入的获取理解
spring框架 在Util一般工具类中加载spring中的Bean对象
在其他 controller 或者 Service 中直接如下方式调用
import static com.xxxxx.xxxx.xxxx.xxxx.util.ServiceUtils.eService;
eService.xxxxx方法(入参);
或
ServiceUtils.eService.xxxxx方法(入参);
边栏推荐
- Research on security situation awareness method of Internet of things based on evidence theory
- Persistence of redis
- Get to know layer in fluent for the first time
- 自定义View:悬浮球与加速球
- postman “status“: 500, “error“: “Internal Server Error“, “message“: “The request was rejecte“
- LAN SDN technology hard core insider 13 II from LAN to Internet
- 【ARC127F】±AB
- J9数字论:什么是 Web3.0?Web3.0 有哪些特征?
- SDN application layer DDoS attack defense mechanism based on API call management
- Ambire Gas Tank 推出独家 NFT 投放
猜你喜欢

从BIO到实现简单多人聊天室功能--IO模型

Codeforces Round #808 (Div. 2) A - D

Flutter memory leak detection

小程序毕设作品之微信酒店预订小程序毕业设计(8)毕业设计论文模板

基于以太坊状态数据库的攻击与防御方案

10个Live Demo都展示了啥?看过没看过的都值得再看一遍

直播预告 | openGauss的自治运维平台DBMind实践分享

After 100 billion of revenue, Alibaba cloud ecosystem has a new way to play

智能商务的数据分析平台设计与实现

"The time of a takeaway is not as valuable as that of a programmer": treat yourself as a person after reading a book? Power on
随机推荐
GNU LD脚本命令语言(一)
Is cross modal semantic alignment optimal under comparative learning--- Adaptive sparse attention alignment mechanism IEEE trans MultiMedia
UNO/ESP8266 for TCA9548A模块双通道驱动2块SH1106 1.3“显示
VR全景动物园,成就不一样的动物园名片
After the applet wx.setstoragesync, sometimes it can't get data with getstoragesync
基于证据理论物联网安全态势感知方法研究
《postgresql指南--内幕探索》第一章 数据库集簇、数据库和数据表
LAN SDN technology hard core insider - 02 forward multi-core technology for Moore's law for one second
Understanding service governance in distributed development
上采样方式(反卷积、插值、反池化)
Redis五大基本数据类型的基本命令
A web server where browser users access server files
MySql的DDL和DML和DQL的基本语法
支持多数不规则用户的隐私保护联邦学习框架
文本攻击方法开源代码汇总
redis基本类型常用命令
现货白银走势图大致是怎么样变化的?
C语言 程序环境
【ARC127F】±AB
Null and nullptr