Springboot普通类调用service,dao中的方法

最近在做一个项目,springboot项目启动后,发送请求到socket服务端进行请求相关数据,即socket服务端的普通java类进行请求数据库,之前百度了好多,都是报空指针异常,试了好久才猛然发现,在启动springboot的时候,结果已经打印在控制台上了。代码如下:

package com.rosam.ywttest.util;

import com.rosam.ywttest.dao.YwtDAO;
import com.rosam.ywttest.entity.User;
import com.rosam.ywttest.service.YwtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;


//把Judge类实例化到spring容器中
@Component
public class Judge{
    //注入service的类
    @Autowired
    private YwtService ywtService;

    @Autowired
    private YwtDAO ywtDAO;

    private static Judge judge;  //这里有static

    private User user;

    /* 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,
    类似于Servlet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。*/
    @PostConstruct
    public void init() {
        System.out.println("实例化成功");
        judge = this;
//        judge.ywtService = this.ywtService;

//        user = judge.ywtService.find(1);
        judge.ywtDAO=this.ywtDAO;
        user=judge.ywtDAO.find(1);

        System.out.println(user.getName());

    }
}

这段代码会在Springboot中的Application.java启动的时候,init函数就会执行,利用这个我们就可以在执行普通类的public static void main(String[] args)之前,就得到数据库的结果。
在普通类上的@Component是必须的。
下面是自己在socket服务端的代码,对以上代码的一个应用。

package ywt.socketserver;


@Component
public class AccountListServer {

    @Autowired
    private QueryAccountListDao queryAccountListDao;

    @Autowired
    private MapTestDao mapTestDao;

    private static AccountListServer accountListServer;

    public static List<DataList9> list;
     @PostConstruct
        public void init() {
            System.out.println("实例化成功");
            accountListServer = this;
            accountListServer.mapTestDao = this.mapTestDao;

             list= accountListServer.mapTestDao.findData();
             System.out.println(list.get(0));
             System.out.println(list);
        }


    public static void main(String[] args) {

        try {
              ServerSocket serverSocket = new ServerSocket(8885);
              System.out.println("启动服务器....");
              Socket socket = serverSocket.accept();

                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                //读取客户端发送来的消息
                String mess = br.readLine();
                System.out.println("我收到了客户端的东西"+mess);
                JSONObject object = JSONObject.fromObject(mess);
                //已接收到客戶端发来的内容,下面进行处理
                String dateTime1=(String)((JSONObject)object.get("reqData")).get("dateTime");   
                System.out.println("dateTime="+dateTime1);

                //从数据库中读取一条记录
                //从前面的init中已经读取到了ywtaccountlist表中的数据
                JSONObject jsonParam = new JSONObject();

                jsonParam.put("version", "1.0");
                jsonParam.put("charset", new ReadCharsetProperties().charset);// 支持GBK和UTF-8两种编码
                Signature signature=new Signature();
                jsonParam.put("signType", "SHA-256");
                jsonParam.put("reqData", list.get(0));


                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

                bw.write(jsonParam.toString()+"\n");
                bw.flush();


            bw.close();
            br.close();
            socket.close();
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_38070406/article/details/80777352