Reprint: http://blog.csdn.net/georgeshaw1/article/details/74943089
Background:
A TCP server is made to access the smart device, and then key information such as the positioning of the device to be sent in real time is stored in the database. In order to consider the possibility of providing a rest interface in the future, the integration of the TCP server into the Springboot framework, of course, is also to enable the fastest use of the MyBatis framework for data access, and then to solve the problem of how to start, how to log off, and so on, and then when the TCP You need to write the database, call the DAO layer directly, compile the error. Instead, call the service layer, compile normal, run to the call place, report null pointer anomaly, tracking to the abnormal location, found that the service is empty, that is, according to the previous controller layer through @autowired injection service layer failure.
Solution:
1. Code on
@Component public
class Serverhandler extends Iohandleradapter {
@Autowired
protected Healthdataservice Healthdataservice;
private static Serverhandler Serverhandler;
@PostConstruct//operations performed before the bean is initialized via @postconstruct public
void init () {
Serverhandler = this;
Serverhandler.healthdataservice = This.healthdataservice;
Testservice instantiated
}
//test calls Public
void Test () {
Serverhandler .healthdataservice.< when the initial initialization is made Your service layer method >;
}
1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15
2. Description:
The class that will need to invoke the spring's service layer is loaded by the @component annotation for the component;
Also obtains the service layer bean object through the @autowired;
Declare a static variable for the class to facilitate the next store of the Bean object;
Focus: Through annotation @postconstruct, initialize static object and its static member variable Healthdataservice when initializing, principle is to get service Layer Bean object, static store down, prevent to be released.
Those wading holes:
At the beginning of the call, always feel very simple, the previous SPRINGMVC to write a configuration, the object is labeled as beans can be arbitrary call the spring IOC container beans, but this is springboot, estimated that there is a difference, one test Baidu out of the first three pages of Help, Basically did not succeed. Including:
1 Declare the tool class as a spring component, such as @controller @compent, and add the package containing the tool class in the Spring Auto scan package setting;
2 new service; invalid;
Wait a minute.