当前位置:网站首页>Summary in the development process BaseService provides a public access service file for all controllers or services to reduce repeated injection
Summary in the development process BaseService provides a public access service file for all controllers or services to reduce repeated injection
2022-07-23 07:37:00 【Aurora rain】
BaseService Practical significance
BaseService You can also call it ServiceUtil Wait, this is more casual , In general, this class is used to make something different Controller perhaps Service Reuse... In @Autowired perhaps @Resource The injected attributes are injected only once , Thus reducing multiple Controller perhaps Service Repeated injection in , Can reduce the amount of code , Looks Cleaner , At the same time, only oneortwo basic service class , It will also be much easier .
The first one is Abstract classes are injected uniformly , Use controller perhaps Service Inherit this class
The specific example code is as follows :
@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;
........
........
........
// At the same time, it can provide some common methods to provide external controller Use , Only service injection is ok
other Controller perhaps Service Inherit in the following way , Then repeated injection can be omitted , In order to reduce the Controller Injection in , Even directly in different Service Inherit ,controller You can use it completely .
But this way, through inheritance, classes that are not used may also be inherited , It didn't end up using . This is a good way, but there is still some suspicion of waste .
@Service("CubeService")
public class CubeService extends BaseService {
// normal Service The business process in
........................
........................
........................
........................
........................
}
adopt Util The way of tool class Inject static service classes directly by controller perhaps Service Use
This way looks better , Pass different service classes directly static final Static declaration in class ( You need to start with applicationContext In order to get ), When it becomes a static attribute , Other controller perhaps service Directly by class name . To get the corresponding single attribute , So that one or several services can be accurately referenced , No waste , But it may look a little longer .
The sample code is as follows :
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);
.....................
.....................
.....................
.....................
.....................
}
among ToolSpring In order to achieve Spring Interface ApplicationContextAware Of a class adopt Class name or class class To get the corresponding ApplicationContext Medium Bean Get the class injected into the container )
See my other articles for details
Spring ApplicationContextAware Function and use learning summary spring bean Initialization process summary
ApplicationContext obtain Spring Medium Bean Understanding and way , Gain understanding of injection
spring frame stay Util General tool classes are loaded spring Medium Bean object
Among others controller perhaps Service Directly call
import static com.xxxxx.xxxx.xxxx.xxxx.util.ServiceUtils.eService;
eService.xxxxx Method ( Enter the reference );
or
ServiceUtils.eService.xxxxx Method ( Enter the reference );
边栏推荐
- How to make a high-quality VR panorama? Are there any simple ones that can be taken?
- C language file operation (including all knowledge points, detailed explanation of related functions and codes)
- 【JS】将字符串保存成文件到本地(.txt、.json、.md...)
- 如何制作优质的VR全景?有简单基础的能拍吗?
- 银行虚拟人技术应用领域及作用介绍
- GNU pseudo instruction definition function
- Countermeasure and defense method based on softmax activation transformation
- Understanding service governance in distributed development
- 你的 NFT 会消失吗?DFINITY 提供 NFT 存储最佳方案
- Wechat hotel reservation applet graduation project (7) Interim inspection report
猜你喜欢
随机推荐
小程序毕设作品之微信酒店预订小程序毕业设计(6)开题答辩PPT
93.(leaflet篇)leaflet态势标绘-进攻方向修改
“外卖员的时间没有程序员值钱”:读过书就把自己当人上人?得电
【无标题】分享一个基于Abp Vnext开发的API网关项目
Digital collections start the 100 billion level market
《postgresql指南--内幕探索》第二章 进程与内存架构
類和對象(1)
Understanding service governance in distributed development
每日刷题记录 (三十一)
ETL工具(数据同步)
Basic commands of redis' five basic data types
JS determines the scrolling element and solves the tab to switch the scrolling position independently
Wechat campus second-hand book trading applet graduation design finished product (4) opening report
升级poi-tl版本1.12.0与旧版poi(4.1.2)、easyexcel之间的依赖冲突解决
Flutter memory leak detection
Ambire 钱包开启 Twitter Spaces 系列
智能商务的数据分析平台设计与实现
Get to know layer in fluent for the first time
Wechat hotel reservation applet graduation project (6) opening defense ppt
自定义View:悬浮球与加速球








