In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how SpringBoot calls the service layer in the custom class". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how SpringBoot calls the service layer in the custom class".
Background:
A TCP server is made to access the intelligent device, and then the location and other key information sent by the device in real time need to be stored in the database.
In order to consider that the rest interface may be provided in the future, we adopt to integrate the TCP server into the SpringBoot framework, of course, in order to realize data access by using the mybatis framework as soon as possible, and then solve various problems such as how to start and log out in turn, and then we need to write the database, call the DAO layer directly and compile the error report when processing the message of the TCP server.
Instead, call the Service layer, compile normally, run to the place of the call, report a null pointer exception, trace to the exception location, and find that service is empty, that is, the controller layer is invalid by injecting service layer through @ Autowired as before.
Solution: 1. The code @ Componentpublic class ServerHandler extends IoHandlerAdapter {@ Autowired protected HealthDataService healthDataService; private static ServerHandler serverHandler; @ PostConstruct / / implements the operation public void init () {serverHandler = this; serverHandler.healthDataService = this.healthDataService before initializing bean through @ PostConstruct / / instantiate the statically instantiated testService during initialization} / / Test calls public void test () {serverHandler. HealthDataService.;} 2. Description:
Load the classes that need to call the Service layer of Spring for the component through the @ Component annotation
Also get the Bean object of the Service layer through @ Autowired
Declare a static variable for the class to facilitate the next step of storing the bean object
Highlight: by annotating @ PostConstruct, initialize the static object and its static member variable healthDataService during initialization. The principle is to get the service layer bean object and store it statically to prevent it from being released.
The pits that have been waded through:
At the beginning of the call, it always felt very simple. In the past, springmvc wrote a configuration and marked the object as bean, you can call the beans of the Spring IOC container at will, but this is SpringBoot, it is estimated that there is still a difference. Once you tried the first three pages of help from Baidu, it was basically unsuccessful. These include:
1) declare the tool class as a spring component, such as @ controller @ compent, etc., and add the package where the tool class is located in the spring automatic scan package setting; invalid
2) new an invalid service;
Springboot transaction processing in which multiple service calls each other
If you want to call method B of another service in method An of service, both method An and method B have database insert operations, and the @ Transaction annotation has been added, but when an exception is thrown in method B, the insert statement in method A can still be executed successfully.
The annotation configuration is as follows: @ Transactional (isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED)
I am puzzled. After looking up the relevant information, the problem still lies in the configuration of @ Transaction annotation, which requires configuration exception rollback.
@ Transactional (isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
In this way, when an exception is thrown in method B, the operation in A will also be rolled back, and the transaction will play a controlling role.
Thank you for your reading, the above is the content of "how SpringBoot calls the service layer in the custom class". After the study of this article, I believe you have a deeper understanding of how to call the service layer in the custom class, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 287
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.