访问应用程序中的对象

时间:2016-09-28 00:24:46

标签: java spring spring-boot

在Spring启动应用程序中,我使用serialPuntit librairie读取串口。

线程通过onApplicationEvent启动。

工作正常。

@Component
public class ScanRead implements Runnable {

    Thread thrd;
    boolean suspended;
    boolean stopped;

    String port = "/dev/ttyUSB0";

    final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    @Override
    public void run() {

        try {

            while (true) {

                StringBuilder sb = new StringBuilder();
                SerialComManager scm = new SerialComManager();
                long handle = scm.openComPort(port, true, false, true);
                byte[] dataRead = {};

                scm.configureComPortData(handle, DATABITS.DB_8, STOPBITS.SB_1, PARITY.P_NONE, BAUDRATE.B9600, 0);
                scm.configureComPortControl(handle, FLOWCONTROL.NONE, 'x', 'x', false, false);

                long context = scm.createBlockingIOContext();

                for (int x = 0; x < 8; x++) {
                    dataRead = scm.readBytesBlocking(handle, 1, context);
                    if (dataRead != null) {
                        sb.append(bytesToHex(dataRead));
                    }
                }

                System.out.println(sb.toString());

                scm.unblockBlockingIOOperation(context);
                scm.destroyBlockingIOContext(context);
                scm.closeComPort(handle);

                Thread.sleep(50);

                synchronized (this) {
                    while (suspended) {
                        wait();
                    }
                    if (stopped) {
                        break;
                    }
                }

            }
        } catch (InterruptedException exc) {
            System.out.println(thrd.getName() + " interrupted.");
        } catch (IOException ex) {
            Logger.getLogger(ScanRead.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    synchronized void stop() {
        stopped = true;
        suspended = false;
        notify();
    }

    synchronized void suspend() {
        suspended = true;
    }

    synchronized void resume() {
        suspended = false;
        notify();
    }

}


@Component
public class ScanApplicationStartup implements ApplicationListener<ApplicationReadyEvent> {
  @Override
  public void onApplicationEvent(final ApplicationReadyEvent event) {

    ScanRead sc = new ScanRead();

    sc.run();
  }

}

只有缺点,不能在应用程序的另一个地方使用另一个SerialComManager来读取同一个端口。

我搜索一个初始化SerialComManager的方法,并在应用程序的任何地方使用它。

任何建议。

1 个答案:

答案 0 :(得分:0)

在app配置类中定义一个bean:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public SerialComManager getSerialComManager() {
        return new SerialComManager();
    }
}

之后,您将能够Autowire个实例进入所需的类:

import org.springframework.beans.factory.annotation.Autowired;

@Component
public class ScanRead {
    @Autowired
    private SerialComManager scm;

    @Override
    public void run() {
        // ...
        long handle = scm.openComPort(port, true, false, true);
        // ...
    }
}

请注意,如果将自动装配与静态上下文混合使用会发生奇怪的事情,所以如果可以,我会避免这种情况。

相关问题