SpringBoot calls other Spring layer operations such as service layer in the custom class

  • 2021-09-12 01:01:30
  • OfStack

Background:

An TCP server is built to access smart devices, and then key information such as positioning sent by devices in real time needs to be stored in the database.

In order to consider the possibility of providing rest interface to the outside world in the future, TCP server is integrated into SpringBoot framework, of course, it is also to realize data access by using mybatis framework as quickly as possible, and then various problems such as how to start and how to log out are solved in turn. Then, when TCP server handles messages, it is necessary to write database, directly call DAO layer, compile and report errors.

Calling Service layer instead, compiling normally, running to the calling place, reporting null pointer exception, tracking to the abnormal position, and finding that service is empty, that is, according to the previous controller layer, injecting service layer through @ Autowired failed.

Solution:

1. Code on


@Component
public class ServerHandler extends IoHandlerAdapter {
    @Autowired
    protected HealthDataService healthDataService;
    private static ServerHandler  serverHandler ;
    @PostConstruct // Pass @PostConstruct Implement initialization bean Previous actions 
    public void init() {  
        serverHandler = this;  
        serverHandler.healthDataService = this.healthDataService;        
        //  That will be static when it is first made testService Instantiation 
    }  
    // Test call 
    public void test(){
        serverHandler .healthDataService.< Yours service Layer method >;
    }

2. Description:

Load the class that needs to call the Service layer of Spring for the component through @ Component annotation;

The Bean object of the Service layer is also obtained through @ Autowired;

Declare a static variable for the class, which is convenient for storing bean objects in the next step;

Focus: 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.

Those pits that have been visited:

When I first started calling, I always felt very simple. Before springmvc wrote a configuration and marked the object as bean, I could call beans of Spring IOC container at will. However, this is SpringBoot, and it is estimated that there is still a difference. I tried the first three pages of help from Baidu once, but it was basically unsuccessful. Including:

1) Declare the tool class as an spring component, such as @ controller @ compent, etc. Add the package where the tool class is in the spring Auto Scan Package setting; Invalid

2) new1 service; Invalid;

springboot Transactions Invoked by Multiple service

If you want to call the method B of another service in the method A of one service, both the method A and the method B have database insertion operation, and @ Transaction annotation is also added, but when an exception is thrown in the B method, the insertion statement in A can still be executed successfully.

The annotation configuration is as follows:


@Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED ) 

I can't understand it. After searching for relevant information, the problem still lies in the configuration of @ Transaction annotation, and it is necessary to configure exception rollback.


@Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)

In this way, when an exception is thrown in the B method, the operation in A is rolled back, and the transaction takes control.


Related articles: