Browse Source

修改可以自动装配多个bean

k 2 years ago
parent
commit
89e2639d34

+ 120 - 8
src/main/java/com/jwk/spring/boot/autoconfigure/ModbusAutoConfiguration.java

@@ -16,27 +16,139 @@ public class ModbusAutoConfiguration {
 
 
     /**
-     * RTU-Master
+     * RTU-Master first
+     * 注入方式@Autowired(required = true) private ModbusRtuMasterTemplate modbusRtuMasterTemplateFirst;
+     * 没有open=true的bean千万不要注入,@Autowired(required = false)也不行.
+     *
      * @param modbusProperties
      * @return
      */
     @Bean
-    @ConditionalOnProperty(prefix = "modbus.rtu-master", name = "open", havingValue = "true")
-    public ModbusRtuMasterTemplate modbusRtuMasterTemplate(ModbusProperties modbusProperties) {
-        ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster();
+    @ConditionalOnProperty(prefix = "modbus.rtu-master.first", name = "open", havingValue = "true")
+    public ModbusRtuMasterTemplate modbusRtuMasterTemplateFirst(ModbusProperties modbusProperties) {
+        ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getFirst();
         return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
                 , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
     }
 
     /**
-     * TCP-Master
+     * RTU-Master second
+     *
      * @param modbusProperties
      * @return
      */
     @Bean
-    @ConditionalOnProperty(prefix = "modbus.tcp-master", name = "open", havingValue = "true")
-    public ModbusTcpMasterTemplate modbusTcpMasterTemplate(ModbusProperties modbusProperties) {
-        ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster();
+    @ConditionalOnProperty(prefix = "modbus.rtu-master.second", name = "open", havingValue = "true")
+    public ModbusRtuMasterTemplate modbusRtuMasterTemplateSecond(ModbusProperties modbusProperties) {
+        ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getSecond();
+        return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
+                , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
+    }
+
+    /**
+     * RTU-Master third
+     *
+     * @param modbusProperties
+     * @return
+     */
+    @Bean
+    @ConditionalOnProperty(prefix = "modbus.rtu-master.third", name = "open", havingValue = "true")
+    public ModbusRtuMasterTemplate modbusRtuMasterTemplateThird(ModbusProperties modbusProperties) {
+        ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getThird();
+        return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
+                , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
+    }
+
+    /**
+     * RTU-Master fourth
+     *
+     * @param modbusProperties
+     * @return
+     */
+    @Bean
+    @ConditionalOnProperty(prefix = "modbus.rtu-master.fourth", name = "open", havingValue = "true")
+    public ModbusRtuMasterTemplate modbusRtuMasterTemplateFourth(ModbusProperties modbusProperties) {
+        ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getFourth();
+        return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
+                , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
+    }
+
+    /**
+     * RTU-Master fifth
+     *
+     * @param modbusProperties
+     * @return
+     */
+    @Bean
+    @ConditionalOnProperty(prefix = "modbus.rtu-master.fifth", name = "open", havingValue = "true")
+    public ModbusRtuMasterTemplate modbusRtuMasterTemplateFifth(ModbusProperties modbusProperties) {
+        ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getFifth();
+        return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
+                , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
+    }
+
+    /**
+     * TCP-Master first
+     *
+     * @param modbusProperties
+     * @return
+     */
+    @Bean
+    @ConditionalOnProperty(prefix = "modbus.tcp-master.first", name = "open", havingValue = "true")
+    public ModbusTcpMasterTemplate modbusTcpMasterTemplateFirst(ModbusProperties modbusProperties) {
+        ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getFirst();
+        return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
+    }
+
+    /**
+     * TCP-Master second
+     *
+     * @param modbusProperties
+     * @return
+     */
+    @Bean
+    @ConditionalOnProperty(prefix = "modbus.tcp-master.second", name = "open", havingValue = "true")
+    public ModbusTcpMasterTemplate modbusTcpMasterTemplateSecond(ModbusProperties modbusProperties) {
+        ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getSecond();
+        return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
+    }
+
+    /**
+     * TCP-Master third
+     *
+     * @param modbusProperties
+     * @return
+     */
+    @Bean
+    @ConditionalOnProperty(prefix = "modbus.tcp-master.third", name = "open", havingValue = "true")
+    public ModbusTcpMasterTemplate modbusTcpMasterTemplateThird(ModbusProperties modbusProperties) {
+        ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getThird();
+        return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
+    }
+
+    /**
+     * TCP-Master fourth
+     *
+     * @param modbusProperties
+     * @return
+     */
+    @Bean
+    @ConditionalOnProperty(prefix = "modbus.tcp-master.fourth", name = "open", havingValue = "true")
+    public ModbusTcpMasterTemplate modbusTcpMasterTemplateFourth(ModbusProperties modbusProperties) {
+        ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getFourth();
+        return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
+    }
+
+    /**
+     * TCP-Master fifth
+     *
+     * @param modbusProperties
+     * @return
+     */
+    @Bean
+    @ConditionalOnProperty(prefix = "modbus.tcp-master.fifth", name = "open", havingValue = "true")
+    public ModbusTcpMasterTemplate modbusTcpMasterTemplateFifth(ModbusProperties modbusProperties) {
+        ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getFifth();
         return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
     }
 }

+ 33 - 7
src/main/java/com/jwk/spring/boot/autoconfigure/ModbusProperties.java

@@ -14,13 +14,39 @@ public class ModbusProperties {
 
     public static final String DEMO_PREFIX = "modbus";
 
-    private RtuMaster rtuMaster;
-    private TcpMaster tcpMaster;
+    /**
+     * 注入方式@Autowired(required = true) private ModbusRtuMasterTemplate modbusRtuMasterTemplateFirst;
+     * 没有open=true的bean千万不要注入,@Autowired(required = false)也不行.
+     */
+    private RtuMasterOrder rtuMaster;
+    /**
+     * 注入方式@Autowired(required = true) private ModbusTcpMasterTemplate modbusTcpMasterTemplateFirst;
+     * 没有open=true的bean千万不要注入,@Autowired(required = false)也不行.
+     */
+    private TcpMasterOrder tcpMaster;
+
+    @Data
+    public static class RtuMasterOrder {
+        private RtuMaster first;
+        private RtuMaster second;
+        private RtuMaster third;
+        private RtuMaster fourth;
+        private RtuMaster fifth;
+    }
+
+    @Data
+    public static class TcpMasterOrder {
+        private TcpMaster first;
+        private TcpMaster second;
+        private TcpMaster third;
+        private TcpMaster fourth;
+        private TcpMaster fifth;
+    }
 
     @Data
     public static class RtuMaster {
         /**
-         * 是否打开串口
+         * 开串口
          */
         private boolean open = false;
         /**
@@ -63,18 +89,18 @@ public class ModbusProperties {
     @Data
     public static class TcpMaster {
         /**
-         * 是否打开Tcp连接
+         * 开启TCP连接
          */
         private boolean open = false;
 
         /**
-         * ip
+         * ip地址
          */
-        private String host = "localhost";
+        private String host = "127.0.0.1";
 
         /**
          * 端口
          */
-        private Integer port;
+        private Integer port = 502;
     }
 }

+ 1 - 1
src/main/java/com/jwk/spring/boot/autoconfigure/ModbusRtuMasterTemplate.java

@@ -34,10 +34,10 @@ public class ModbusRtuMasterTemplate {
         modbusRtuMaster = new ModbusRtuMaster(port, baudRate, dataBits, stopBits, parity
                 , flowControlIn, flowControlOut);
         modbusRtuMaster.connectAsync();
-        modbusMasterUtil = modbusRtuMaster.getModbusMasterUtil();
     }
 
     public ModbusMasterUtil getModbusMasterUtil() {
+        modbusMasterUtil = modbusRtuMaster.getModbusMasterUtil();
         return modbusMasterUtil;
     }