Spring boot 中普通类型如何注入Service或mapper

最近遇到一个难题(大佬可能感觉这太简单了把),对于我这样的小白来说,确实有些头疼。

接下来说一下我遇到的问题,在spring boot中创建了一个UDP客户端,用于监听UDP服务端发送到数据。在实现这一功能时遇到主要遇到了两个难题

1.由于之前都是通过controller调用service层来实现访问,现在要建立一个持久的连接来实现监听某一端口的数据,由于做的项目不多,经验不足,spring也没怎么学过所以困扰了很久。只是在main方法中开启一个线程简单的new创建实例解决了该问题,虽然不知道这样做对不对,但是能实现功能。(如有更好的办法请告诉小白,谢谢)

@SpringBootApplication
@MapperScan("com.example.net.udpservicertest.mapper")

public class UdpServicerTestApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(UdpServicerTestApplication.class, args);
        new Thread(()->{

            try {
                UDPService udpService = new UDPService();
                udpService.startSocketServer();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();




    }

}

客户端虽然能够启动,但是新的问题又来了

2.在拿到数据之后,掉service时出现空指针。这又困扰了小白两天,通过查博客得到了这样的解决方案

https://blog.csdn.net/yft_android/article/details/79166793

(1)首先需要新建一个类,实现 ApplicationContextAware 接口。

要@Conponment注解

   @Component
        public class SpringUtils implements ApplicationContextAware {

            private static ApplicationContext applicationContext = null;

            @Override
            public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
                if(SpringUtils.applicationContext == null){
                    SpringUtils.applicationContext  = applicationContext;
                }
            }


            //获取applicationContext
            public static ApplicationContext getApplicationContext() {
                return applicationContext;
            }

            //通过name获取 Bean.
            public static Object getBean(String name){
                return getApplicationContext().getBean(name);
            }

            //通过class获取Bean.
            public static <T> T getBean(Class<T> clazz){
                return getApplicationContext().getBean(clazz);
            }

            //通过name,以及Clazz返回指定的Bean
            public static <T> T getBean(String name,Class<T> clazz){
                return getApplicationContext().getBean(name, clazz);
            }

        }

(2)在UDP类中获取ApplicationContext对象,然后去获取需要的service 或者 dao。

@Service注解,将自动注册到Spring容器不需要再在applicationContext.xml文件定义bean了,类似的还包括@Component、@Repository、@Controller。

ApplicationContext又是spring管理bean的容器,加载配置文件的时候,就会将Spring管理的类都实例化。所以就能够注入到该实例。

public class UDPService {
    private ApplicationContext applicationContext=SpringUtils.getApplicationContext();
    private UserServiceImpl userService=applicationContext.getBean(UserServiceImpl.class);



    public void startSocketServer() throws Exception {
        DatagramSocket ds = new DatagramSocket(10000);
        while (true) {
            // 2.创建数据包
            byte[] buf = new byte[1024];
            DatagramPacket dp = new DatagramPacket(buf, buf.length);

            // 3.使用接受方法将数据存储到数据包中
            ds.receive(dp);// 阻塞式的

            // 4.通过数据包对象的方法,解析其中的数据 数据内容
        //    String ip = dp.getAddress().getHostAddress();
        //    int port = dp.getPort();
             String text = new String(dp.getData(), 0, dp.getLength());
             System.out.println(""+text.length());
            System.out.println(""+text);
           // byte[] data = dp.getData();

            String[] split = text.split("#");
            System.out.println(split[0]);
            userService.updateUser(split[0],split[1]);
            Thread.sleep(3000L);

        }

    }
}
@SpringBootApplication
@MapperScan("com.example.net.udpservicertest.mapper")
@ComponentScan("com.example.net.udpservicertest")
public class UdpServicerTestApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(UdpServicerTestApplication.class, args);
        new Thread(()->{

            try {
                UDPService udpService = new UDPService();
                udpService.startSocketServer();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();




    }

}

头疼的问题终于解决了!开心

猜你喜欢

转载自blog.csdn.net/qq_29235677/article/details/88538423