springBoot swing 界面实现配置和日志打印
废话少说,直接贴代码,
代码全在一个包下面不需要展示目录结构了。
ConfigFrame
package com.adao.simulater.swing; import org.apache.log4j.Logger; import javax.swing.*; import java.awt.*; public class ConfigFrame { private final static Logger logger = Logger.getLogger(ConfigFrame.class); public static void show() { JPanel panel = new JPanel(); panel.setLayout(null); panel = new ConfigDB().buildJpanel(panel); panel = new ConfigSocket().buildJpanel(panel); panel = new ConfigEvent().buildJpanel(panel); panel = new ConfigTask().buildJpanel(panel); panel = new ConfigPrint().buildJpanel(panel); buildFrame(panel); } protected static JButton buildJButton(String name, int x, int y, int width, int height) { JButton button = new JButton(name); button.setBounds(x, y, width, height); return button; } // 文本框 protected JTextField buildJTextField(JTextField jtf, String value, String name, int columns, int x, int y, int width, int height) { jtf = new JTextField(columns); jtf.setText(value); jtf.setName(name); jtf.setBounds(x, y, width, height); logger.info(name + "======" + value); return jtf; } protected static JLabel buildJLabel(String name, int x, int y, int width, int height) { JLabel label = new JLabel(name); label.setBounds(x, y, width, height); return label; } protected JLabel buildJBorder(String name, int x, int y, int width, int height) { JLabel label = new JLabel(); label.setBounds(x, y, width, height); label.setBorder(BorderFactory.createTitledBorder(name)); return label; } protected static void saveResult(String msg) { if ("true".equals(msg)) { JOptionPane.showMessageDialog(null, "保存成功!", "提示", JOptionPane.PLAIN_MESSAGE); } else { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "保存异常!", "提示", JOptionPane.ERROR_MESSAGE); } } private static void buildFrame(JComponent component) { JFrame frame = new JFrame("Simulater Terminal"); component.setBounds(0, 0, 1152, 864); frame.add(component); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().setLayout(null); frame.getContentPane().add(BorderLayout.CENTER, component); // 设置窗口最小尺寸 frame.setMinimumSize(new Dimension(1152, 864)); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setResizable(true); } }
ConfigDB
package com.adao.simulater.swing; import com.linyang.simulater.common.Constant; import com.linyang.simulater.common.PropertiesUtil; import com.linyang.simulater.util.ComMethod; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashMap; import java.util.Map; /** * @Classname ConfigDB * @Date 2020/7/17 * @Created adao */ public class ConfigDB { private final static Logger logger = Logger.getLogger(ConfigDB.class); @Autowired PropertiesUtil propertiesUtil; private JTextField urlTextField; private JTextField usernameTextField; private JTextField passwordTextField; public ConfigFrame cf = new ConfigFrame(); public JPanel buildJpanel(JPanel panel) { String url = propertiesUtil.getConUrl(); String username = propertiesUtil.getConUserName(); String password = propertiesUtil.getConPassWord(); int y0 = 0; panel.add(cf.buildJBorder("数据库配置", 5, y0 + 10, 490, 180)); // URL panel.add(cf.buildJLabel("URL:", 15, y0 + 40, 80, 25)); urlTextField = cf.buildJTextField(urlTextField, url, "dbUrl", 20, 100, y0 + 40, 350, 25); panel.add(urlTextField); // 用户名 panel.add(cf.buildJLabel("用户名:", 15, y0 + 70, 80, 25)); usernameTextField = cf.buildJTextField(usernameTextField, username, "dbUserName", 20, 100, y0 + 70, 165, 25); panel.add(usernameTextField); // 密码 panel.add(cf.buildJLabel("密码:", 15, y0 + 100, 80, 25)); passwordTextField = cf.buildJTextField(passwordTextField, password, "dbPassWord", 20, 100, y0 + 100, 165, 25); panel.add(passwordTextField); // 添加按钮,绑定事件监听 JButton saveButton = cf.buildJButton("保存", 100, y0 + 140, 80, 25); addActionListener(saveButton); panel.add(saveButton); return panel; } // 为按钮绑定监听 private void addActionListener(JButton saveButton) { saveButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { activeEvent(); } }); } // save event private void activeEvent() { String url = propertiesUtil.getConUrl(); String username = propertiesUtil.getConUserName(); String password = propertiesUtil.getConPassWord(); String textUrl = urlTextField.getText().toString(); String textUsername = usernameTextField.getText().toString(); String textPassword = passwordTextField.getText().toString(); if (ComMethod.isEmpty(textUrl)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "请输入数据库URL!", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (ComMethod.isEmpty(textUsername)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "请输入用户名!", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (ComMethod.isEmpty(textPassword)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "请输入密码!", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (url.equals(textUrl) && textUsername.equals(username) && textPassword.equals(password) ) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "配置参数未更改!", "提示", JOptionPane.INFORMATION_MESSAGE); } else { propertiesUtil.setConUrl(textUrl); propertiesUtil.setConUserName(textUsername); propertiesUtil.setConPassWord(textPassword); } } }
ConfigEvent
package com.adao.simulater.swing; import com.adao.simulater.common.Constant; import com.adao.simulater.common.PropertiesUtil; import com.adao.simulater.util.ComMethod; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashMap; import java.util.Map; /** * @Classname ConfigTask * @Date 2020/7/20 * @Created adao */ public class ConfigEvent { private final static Logger logger = Logger.getLogger(ConfigEvent.class); @Autowired PropertiesUtil propertiesUtil; private JTextField eventPeriodUnitField; private JTextField eventPeriodMinField; private JTextField eventPeriodMaxField; private ConfigFrame cf = new ConfigFrame(); public JPanel buildJpanel(JPanel panel) { ConfigFrame cf = new ConfigFrame(); String eventPeriodUnit = String.valueOf(propertiesUtil.getConEventPeriodUnit()); String eventPeriodMin = String.valueOf(propertiesUtil.getConEventPeriodMin()); String eventPeriodMax = String.valueOf(propertiesUtil.getConEventPeriodMax()); int y0 = 360; //添加服务器配置区 panel.add(cf.buildJBorder("事件配置", 5, y0, 490, 180)); //事件周期基数unit panel.add(cf.buildJLabel("周期基数:", 15, y0 + 30, 80, 25)); eventPeriodUnitField = cf.buildJTextField(eventPeriodUnitField, eventPeriodUnit, "serverAddr", 20, 100, y0 + 30, 165, 25); panel.add(eventPeriodUnitField); //事件上报周期范围最小数 panel.add(cf.buildJLabel("周期最小数:", 15, y0 + 60, 80, 25)); eventPeriodMinField = cf.buildJTextField(eventPeriodMinField, eventPeriodMin, "serverPort", 20, 100, y0 + 60, 165, 25); panel.add(eventPeriodMinField); //事件上报周期范围最大数 panel.add(cf.buildJLabel("周期最大数:", 15, y0 + 90, 80, 25)); eventPeriodMaxField = cf.buildJTextField(eventPeriodMaxField, eventPeriodMax, "serverPort", 20, 100, y0 + 90, 165, 25); panel.add(eventPeriodMaxField); // 添加按钮,绑定事件监听 JButton saveButton = cf.buildJButton("保存", 100, y0 + 130, 80, 25); addActionListener(saveButton); panel.add(saveButton); return panel; } // 为按钮绑定监听 private void addActionListener(JButton button) { button.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { save(); } }); } // save event private void save() { String eventPeriodUnit = String.valueOf(propertiesUtil.getConEventPeriodUnit()); String eventPeriodMin = String.valueOf(propertiesUtil.getConEventPeriodMin()); String eventPeriodMax = String.valueOf(propertiesUtil.getConEventPeriodMax()); String eventPeriodUnitText = eventPeriodUnitField.getText().toString(); String eventPeriodMinText = eventPeriodMinField.getText().toString(); String eventPeriodMaxText = eventPeriodMaxField.getText().toString(); if (ComMethod.isEmpty(eventPeriodUnitText)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "请输入事件上报周期基数(毫秒)!", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (ComMethod.isEmpty(eventPeriodMinText)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "请输入事件上报周期随机最小数!", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (ComMethod.isEmpty(eventPeriodMaxText)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "请输入事件上报周期随机最大数!", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (Integer.valueOf(eventPeriodMinText) >= Integer.valueOf(eventPeriodMaxText)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "最小数要小于最大数,请修改!", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (eventPeriodUnit.equals(eventPeriodUnitText) && eventPeriodMin.equals(eventPeriodMinText) && eventPeriodMax.equals(eventPeriodMaxText) ) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "配置参数未更改!", "提示", JOptionPane.INFORMATION_MESSAGE); } else { propertiesUtil.setConEventPeriodUnit(Long.valueOf(eventPeriodUnitText)); propertiesUtil.setConEventPeriodMin(Integer.valueOf(eventPeriodMinText)); propertiesUtil.setConEventPeriodMax(Integer.valueOf(eventPeriodMaxText)); } } }
ConfigPrint
package com.adao.simulater.swing; import org.apache.log4j.Appender; import org.apache.log4j.Logger; import org.apache.log4j.WriterAppender; import javax.swing.*; import java.io.PipedReader; import java.io.PipedWriter; import java.io.Writer; public class ConfigPrint { JTextArea logTextArea; public ConfigFrame cf = new ConfigFrame(); public JPanel buildJpanel(JPanel panel) { int y0 = 0; panel.add(cf.buildJBorder("日志", 500, y0 + 10, 630, 720)); logTextArea = new JTextArea(); panel.add(logTextArea); JScrollPane logScrollPane = new JScrollPane(); logScrollPane.setBounds(510, y0 + 40, 610, 680); logScrollPane.getViewport().add(logTextArea); panel.add(logScrollPane); Logger root = Logger.getRootLogger(); try { Appender appender = root.getAppender("WriterAppender"); PipedReader reader = new PipedReader(); Writer writer = new PipedWriter(reader); ((WriterAppender) appender).setWriter(writer); Thread t = new Appendered(reader, logTextArea, logScrollPane); t.start(); } catch (Exception e) { } return panel; } }
Appendered
package com.adao.simulater.swing; import javax.swing.*; import java.io.PipedReader; import java.util.Scanner; public class Appendered extends Thread { PipedReader reader; JTextArea textArea; JScrollPane scroll; public Appendered(PipedReader reader,JTextArea textArea, JScrollPane scroll) { this.reader = reader; this.textArea=textArea; this.scroll=scroll; } public void run() { Scanner scanner = new Scanner(reader); while (scanner.hasNext()) { // System.out.println(scanner.nextLine()); textArea.append(scanner.nextLine()); textArea.append("\n"); //使垂直滚动条自动向下滚动 scroll.getVerticalScrollBar().setValue(scroll.getVerticalScrollBar().getMaximum()); } } }
ConfigSocket
package com.adao.simulater.swing; import com.adao.simulater.common.Constant; import com.adao.simulater.common.PropertiesUtil; import com.adao.simulater.util.ComMethod; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.util.HashMap; import java.util.Map; import java.util.regex.Pattern; /** * @Classname ConfigSocket * @Date 2020/7/20 * @Created YinTao */ public class ConfigSocket { private final static Logger logger = Logger.getLogger(ConfigDB.class); private JTextField serverAddrField; private JTextField serverPortField; @Autowired PropertiesUtil propertiesUtil; private ConfigFrame cf = new ConfigFrame(); public JPanel buildJpanel(JPanel panel) { ConfigFrame cf = new ConfigFrame(); String serverAddr = propertiesUtil.getConServerAddress(); String serverPort = String.valueOf(propertiesUtil.getConServerPort()); int y0 = 200; /** * 服务器配置 */ //添加服务器配置区 panel.add(cf.buildJBorder("Socket服务配置", 5, y0, 490,150)); //添加服务器地址标签 panel.add(cf.buildJLabel("服务器地址:", 15, y0 + 30, 80, 25)); serverAddrField = cf.buildJTextField(serverAddrField, serverAddr, "serverAddr", 20, 100, y0 + 30, 165, 25); panel.add(serverAddrField); //添加服务器端口 panel.add(cf.buildJLabel("服务器端口:", 15, y0 + 60, 80, 25)); serverPortField = cf.buildJTextField(serverPortField, serverPort, "serverPort", 20, 100, y0 + 60, 165, 25); panel.add(serverPortField); //添加保存按钮 JButton linkOrBreak = cf.buildJButton("保存", 100, y0 + 100, 80, 25); addActionListener(linkOrBreak); panel.add(linkOrBreak); // 添加测试按钮 JButton saveButton = cf.buildJButton("测试", 200, y0 + 100, 80, 25); addActionListener(saveButton); panel.add(saveButton); return panel; } // 为按钮绑定监听 private void addActionListener(JButton saveButton) { saveButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(saveButton.getText()=="保存") { activeEvent(); } else if(saveButton.getText()=="测试") { testLink(); } } }); } // save event private void activeEvent() { String serverAddr = propertiesUtil.getConServerAddress(); String serverPort = String.valueOf(propertiesUtil.getConServerPort()); String serverAddrText = serverAddrField.getText().toString(); String serverPortText = serverPortField.getText().toString(); if (ComMethod.isEmpty(serverAddrText)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "请输入Socker服务地址!", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (!ComMethod.isIp(serverAddrText)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "Socker 服务地址格式错误!", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (ComMethod.isEmpty(serverPortText)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "请输入Socker服务端口!", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (Integer.valueOf(serverPortText) < 1 || Integer.valueOf(serverPortText) > 65535) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "端口配置错误,或超出范围[1,65535]", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (!Pattern.matches("^\\d{1,5}$", serverPortText)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "Socker端口配置错误,请输入数字5位数字", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (serverAddr.equals(serverAddrText) && serverPort.equals(serverPortText)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "配置参数未更改!", "提示", JOptionPane.INFORMATION_MESSAGE); } else { propertiesUtil.setConServerAddress(serverAddrText); propertiesUtil.setConServerPort(Integer.valueOf(serverPortText)); } } // 测试连接 private void testLink() { String serverAddrText = serverAddrField.getText().toString(); String serverPortText = serverPortField.getText().toString(); if (ComMethod.isEmpty(serverAddrText)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "请输入Socker服务地址!", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (!ComMethod.isIp(serverAddrText)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "Socker服务地址格式错误!", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (ComMethod.isEmpty(serverPortText)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "请输入Socker服务端口!", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (Integer.valueOf(serverPortText) < 1 || Integer.valueOf(serverPortText) > 65535) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "端口配置超出范围[1,65535]", "提示", JOptionPane.INFORMATION_MESSAGE); } else if (!Pattern.matches("^\\d{1,5}$", serverPortText)) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "Socker端口配置错误,请输入数字5位数字", "提示", JOptionPane.INFORMATION_MESSAGE); } else { try { SocketAddress address = new InetSocketAddress(serverAddrText, Integer.parseInt(serverPortText)); Socket socket = new Socket(); socket.connect(address, 2 * 1000); socket.close(); JOptionPane.showMessageDialog(null, "测试连接正常", "提示", JOptionPane.INFORMATION_MESSAGE); } catch (Exception e) { JOptionPane.showMessageDialog(null, "测试连接异常", "提示", JOptionPane.INFORMATION_MESSAGE); } } } }
taskStart
package com.adao.simulater.swing; import com.adao.simulater.common.AppContextUtil; import com.adao.simulater.common.PropertiesUtil; import com.adao.simulater.pojo.TaskInfoBean; import com.adao.simulater.service.FreezeDayService; import com.adao.simulater.service.TaskService; import com.adao.simulater.service.impl.TaskServiceImpl; import com.adao.simulater.task.TaskManager; import com.adao.simulater.util.ComMethod; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; /** * @Classname ConfigTask * @Description TODO * @Date 2020/7/21 * @Created adao */ public class ConfigTask { private final static Logger logger = Logger.getLogger(ConfigEvent.class); @Autowired PropertiesUtil propertiesUtil; private JButton reportEnd; private JButton reportStart; private JComboBox codeTypeBox; private ConfigFrame cf = new ConfigFrame(); private static String defaultTest = "-- 选择要执行的任务名称 --"; private static String choseTask = null; private AppContextUtil appContextUtil = new AppContextUtil(); private TaskService taskService = appContextUtil.getContext().getBean(TaskService.class); public JPanel buildJpanel(JPanel panel) { ConfigFrame cf = new ConfigFrame(); int y0 = 550; List<TaskInfoBean> tasks = taskService.getAllTaskInfo(); panel.add(cf.buildJBorder("任务配置", 5, y0, 490, 180)); panel.add(cf.buildJLabel("选择任务:", 15, y0 + 30, 80, 25)); codeTypeBox = buildJComboBox( tasks, 100, y0 + 30, 350, 25); addItemListener(codeTypeBox); panel.add(codeTypeBox); //开始上报按钮 reportStart = cf.buildJButton("开始上报", 100, y0 + 120, 100, 25); addActionListener(reportStart); panel.add(reportStart); //添加停止上报按钮 reportEnd = cf.buildJButton("停止上报", 300, y0 + 120, 100, 25); reportEnd.setEnabled(false); addActionListener(reportEnd); panel.add(reportEnd); return panel; } // 为按钮绑定监听 private void addActionListener(JButton button) { button.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (button.getText() == "开始上报") { taskStart(); } else if (button.getText() == "停止上报") { taskStop(); } } }); } // 为下拉事件监听器 private void addItemListener(JComboBox comboBox) { comboBox.addItemListener( new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { // 选择的下拉框选项 choseTask = e.getItem().toString(); } } }); } private static JComboBox buildJComboBox(List<TaskInfoBean> elements, int x, int y, int width, int height) { DefaultComboBoxModel codeTypeModel = new DefaultComboBoxModel(); // elements 下拉框中的选项 codeTypeModel.addElement(defaultTest); for (int i = 0; i < elements.size(); i++) { TaskInfoBean taskInfo = elements.get(i); String taskName = taskInfo.getTaskName(); codeTypeModel.addElement(taskName); } JComboBox codeTypeBox = new JComboBox(codeTypeModel); codeTypeBox.setBounds(x, y, width, height); return codeTypeBox; } // 上报开始 private void taskStart() { // 开始执行任务 if (ComMethod.isEmpty(choseTask) || choseTask.equals(defaultTest)){ JOptionPane.showMessageDialog(null, "选择要执行的任务名称!", "提示", JOptionPane.INFORMATION_MESSAGE); } else { propertiesUtil.setConTaskEnable(true); TaskManager taskManager = new TaskManager(); taskManager.init(choseTask); reportStart.setEnabled(false); reportEnd.setEnabled(true); logger.info("客户端连接成功,上报开始"); } } // 上报停止 private void taskStop() { propertiesUtil.setConTaskEnable(false); reportStart.setEnabled(true); reportEnd.setEnabled(false); logger.info("上报即将停止,需等待本次全部完成"); } }
其他文件:
PropertiesUtil
package com.adao.simulater.common; import com.adao.simulater.util.ComMethod; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Map; import java.util.Properties; /** * 配置 * @author adao */ @Component public class PropertiesUtil { private final static Logger logger = Logger.getLogger(PropertiesUtil.class); public static final String propertiesPath = "src/main/resources/constant.properties"; private static Constant constant; @Autowired public void setConfig(Constant constant) { PropertiesUtil.constant = constant; } public static boolean getConTaskEnable() { return constant.isTaskEnable(); } public static void setConTaskEnable(boolean taskEnable) { constant.setTaskEnable(taskEnable); } public static String getConServerAddress() { return constant.getServerAddress(); } public static void setConServerAddress(String serverAddress) { constant.setServerAddress(serverAddress); } public static Integer getConServerPort() { return constant.getServerPort(); } public static void setConServerPort(Integer serverPort) { constant.setServerPort(serverPort); } public static Long getConEventPeriodUnit() { return constant.getEventPeriodUnit(); } public static void setConEventPeriodUnit(Long eventPeriodUnit) { constant.setEventPeriodUnit(eventPeriodUnit); } public static Integer getConEventPeriodMax() { return constant.getEventPeriodMax(); } public static void setConEventPeriodMax(int eventPeriodMax) { constant.setEventPeriodMax(eventPeriodMax); } public static Integer getConEventPeriodMin() { return constant.getEventPeriodMin(); } public static void setConEventPeriodMin(Integer eventPeriodMin) { constant.setEventPeriodMin(eventPeriodMin); } public static String getConUrl() { return constant.getUrl(); } public static void setConUrl(String url) { constant.setUrl(url); } public static String getConUserName() { return constant.getUserName(); } public static void setConUserName(String userName) { constant.setUserName(userName); } public static String getConPassWord() { return constant.getPassWord(); } public static void setConPassWord(String passWord) { constant.setPassWord(passWord); } public static boolean getConHeartEnable() { return constant.isHeartEnable(); } }
Constant
package com.adao.simulater.common; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; /** **/ @Configuration @ConfigurationProperties(prefix = "constant") @PropertySource(value = "classpath:constant.properties") public class Constant { private boolean taskEnable; private Long taskDelay; private boolean heartEnable; private Long clientHeart; private String serverAddress; private Integer serverPort; private Integer connectTimeout; private Long eventPeriodUnit; private Integer eventPeriodMax; private Integer eventPeriodMin; private String url; private String userName; private String passWord; public boolean isTaskEnable() { return taskEnable; } public void setTaskEnable(boolean taskEnable) { this.taskEnable = taskEnable; } public Long getTaskDelay() { return taskDelay; } public void setTaskDelay(Long taskDelay) { this.taskDelay = taskDelay; } public boolean isHeartEnable() { return heartEnable; } public void setHeartEnable(boolean heartEnable) { this.heartEnable = heartEnable; } public Long getClientHeart() { return clientHeart; } public void setClientHeart(Long clientHeart) { this.clientHeart = clientHeart; } public String getServerAddress() { return serverAddress; } public void setServerAddress(String serverAddress) { this.serverAddress = serverAddress; } public Integer getServerPort() { return serverPort; } public void setServerPort(Integer serverPort) { this.serverPort = serverPort; } public Integer getConnectTimeout() { return connectTimeout; } public void setConnectTimeout(Integer connectTimeout) { this.connectTimeout = connectTimeout; } public Long getEventPeriodUnit() { return eventPeriodUnit; } public void setEventPeriodUnit(Long eventPeriodUnit) { this.eventPeriodUnit = eventPeriodUnit; } public Integer getEventPeriodMax() { return eventPeriodMax; } public void setEventPeriodMax(Integer eventPeriodMax) { this.eventPeriodMax = eventPeriodMax; } public Integer getEventPeriodMin() { return eventPeriodMin; } public void setEventPeriodMin(Integer eventPeriodMin) { this.eventPeriodMin = eventPeriodMin; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } }
proterties
constant.taskEnable=true constant.taskDelay=1000 constant.heartEnable=true constant.clientHeart=60000 constant.serverAddress=127.0.0.1 constant.serverPort=8511 constant.connectTimeout=3000 constant.eventPeriodUnit=60000 constant.eventPeriodMax=10 constant.eventPeriodMin=1 constant.url=jdbc:oracle:thin:@192.168.127.150:1521:ORCL constant.userName=coll_platform constant.passWord=coll_platform
log.properties
log4j.rootLogger=DEBUG,stdout,file,WriterAppender log4j.additivity.org.apache=true log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.threshold=INFO log4j.appender.stdout.layout=org.apache.log4j.PatternLayout #log4j.appender.stdout.layout.ConversionPattern=%-5p %c{1}:%L - %m%n log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.file=org.apache.log4j.DailyRollingFileAppender log4j.appender.file.layout=org.apache.log4j.PatternLayout #log4j.appender.file.DatePattern='.'yyyy-MM-dd-HH-mm #log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %d %p [%c] - %m%n log4j.appender.file.Threshold=INFO log4j.appender.file.append=true #log4j.appender.file.File=/workspaces/logs/simulater/simulater.log log4j.appender.file.File=./logs/simulator.log log4j.appender.WriterAppender=org.apache.log4j.WriterAppender log4j.appender.WriterAppender.Threshold=debug log4j.appender.WriterAppender.layout=org.apache.log4j.PatternLayout log4j.appender.WriterAppender.layout.ConversionPattern= - %m%n
SimulaterApplication
@SpringBootApplication // 扫描 mybatis 通用 mapper 所在的包 @MapperScan(basePackages = "com.adao.simulater.mapper") // 扫描所有包以及相关组件包 @ComponentScan(basePackages = {"com.adao.simulater"}) @EnableConfigurationProperties(Constant.class) public class SimulaterApplication { private final static Logger logger = Logger.getLogger(SimulaterApplication.class); public static void main(String[] args) { SpringApplication.run(SimulaterApplication.class, args); logger.info("程序开始启动..."); // 可视化界面模型 ConfigFrame cf=new ConfigFrame(); cf.show(); } }
执行结果