Home > Java > javaTutorial > body text

How SpringBoot calls the service layer in a custom class

WBOY
Release: 2023-05-11 23:01:20
forward
1276 people have browsed it

Background:

A TCP server was built to connect to smart devices, and then the key information such as positioning and other information sent by the device in real time needs to be stored in the database.

In order to consider the possibility of providing a rest interface to the outside world in the future, the TCP server is integrated into the SpringBoot framework. Of course, it is also to use the mybatis framework to achieve data access as quickly as possible, and then solve various problems such as how to start, how to log out, etc. The problem is that when processing TCP server messages, you need to write to the database, directly call the DAO layer, and compile and report an error.

Instead of calling the Service layer, it compiles normally and runs to the calling place. A null pointer exception is reported. The exception location is traced and the service is found to be empty. In other words, the service layer injected through @Autowired through the previous controller layer fails.

Solution:

1. Upload the code

@Component
public class ServerHandler extends IoHandlerAdapter {
    @Autowired
    protected HealthDataService healthDataService;
    private static ServerHandler  serverHandler ;
    @PostConstruct //通过@PostConstruct实现初始化bean之前进行的操作
    public void init() {  
        serverHandler = this;  
        serverHandler.healthDataService = this.healthDataService;        
        // 初使化时将已静态化的testService实例化
    }  
    //测试调用
    public void test(){
        serverHandler .healthDataService.<你的service层方法>;
    }
Copy after login

2. Description:

Annotate the class that needs to call Spring's Service layer with @Component as Component loading;

Also obtain 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: Pass Annotate @PostConstruct to 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 pitfalls:

When I first started calling, I always thought it was very simple. In the past, springmvc wrote a configuration and marked the object as a bean and you could call it at will. The beans of the Spring IOC container are available, but this is SpringBoot, so there is probably still a difference. I tried the first three pages of help from Baidu, but basically no success. Including:

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 a service; invalid;

Transaction processing of multiple services calling each other in springboot

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

The annotation configuration is as follows:

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

I was puzzled. After searching for relevant information, I found that the problem still lies in the configuration of the @Transaction annotation, which requires configuring exception rollback.

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

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.

The above is the detailed content of How SpringBoot calls the service layer in a custom class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!