SpringBoot calls service layer mapper layer in custom class
Recently, webscoket is being integrated, because you need to customize the websocket class in the websocket. When sending information on the backend, you need to call the code of the service layer mapper layer, or write a tool class yourself, which is used in the custom class.@Autowired
It will report a null pointer exception, so you cannot use the normal injection method. There are many tutorials that can be used on Baidu. I will write one here that I have tried and can use.
Solution
1. Upload the code
@Component public class ServerHandler extends IoHandlerAdapter { @Autowired protected HealthDataService healthDataService; private static ServerHandler serverHandler ; @PostConstruct //Operation before initializing beans is implemented through @PostConstruct public void init() { serverHandler = this; = ; // Instantiate the static testService when initially making the initialization } //Test call public void test(){ .<yourserviceLayer method>; }
2. Explanation
- Load the class that needs to call Spring's Service layer as component through @Component annotation;
- 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 bean objects;
- Key points: By annotating @PostConstruct, the static object and its static member variable healthDataService are initialized during initialization. The principle is to get the service layer bean object and store it statically to prevent it from being released.
Import mapper layer as above
/** I wrote my own tool class */ @Component public class GetChartDataUtils { private static GetChartDataUtils getChartDataUtils ; @Autowired private OrderMapper orderMapper; @PostConstruct //Operation before initializing beans is implemented through @PostConstruct public void init() { getChartDataUtils = this; =; // Instantiate the static orderMapper when initially making the orderMapper } public static Trend<PosMonth> getOrgMonthTrend() { Trend<PosMonth> posMonthTrend = new Trend<>(); ("Customer Source Analysis Trends"); (300); ("Ten thousand"); List<PosMonth> posMonths = new ArrayList<PosMonth>(); //Encapsulate data for each departure location every month PosMonth zzgs = new PosMonth("Zhengzhou Business School",("Zhengzhou Business School"));//The monthly data of Zhengzhou Business School is located at Zhengzhou PosMonth zzjt = new PosMonth("Henan Transportation College",("Henan Transportation College"));//The starting point is Henan Transportation University's monthly data PosMonth zkzx = new PosMonth("Zhoukou Central Station",("Zhoukou Central Station"));//The monthly data of Zhoukou Central Station is the departure point PosMonth zzly = new PosMonth("Zhengzhou Tourism College",("Zhengzhou Tourism College"));//The monthly data of Zhengzhou Tourism College is located in Zhengzhou //Data encapsulation into trend chart (zzgs); (zzjt); (zkzx); (zzly); (posMonths); return posMonthTrend; } public static Trend<PosMonth> getDisMonthTrend() { Trend<PosMonth> posMonthTrend = new Trend<>(); ("Trends Analysis"); (300); ("Ten thousand"); List<PosMonth> posMonths = new ArrayList(); //Encapsulate the data of each destination every month PosMonth zzgs = new PosMonth("Zhoukou Railway Station",("Zhoukou Railway Station"));//The destination is Zhoukou Railway Station's monthly data PosMonth zzjt = new PosMonth("Zhoukou City Center Station",("Zhoukou City Center Station"));//The destination is Zhoukou City Central Station's monthly data PosMonth zkzx = new PosMonth("Huaiyang, Zhoukou City",("Huaiyang, Zhoukou City"));//The destination is Huaiyang, Zhoukou City monthly data PosMonth zzly = new PosMonth("Xiangcheng, Zhoukou City",("Xiangcheng, Zhoukou City"));//The destination is Xiangcheng, Zhoukou City, monthly data //Data encapsulation into trend chart (zzgs); (zzjt); (zkzx); (zzly); (posMonths); return posMonthTrend; } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.