Commit a0211dd6ff1ac7818208ff346e1c15d0c399e550

Authored by 杜云山
1 parent 40f3a57b

feat: 抽取properties及configuration

src/main/java/com/example/mina/config/AbstractVirtualBoxConfiguration.java 0 → 100644
... ... @@ -0,0 +1,56 @@
  1 +package com.example.mina.config;
  2 +
  3 +import com.example.mina.base.AbstractVirtualBoxHandler;
  4 +import com.example.mina.property.AbstractVirtualBoxProperties;
  5 +import lombok.extern.slf4j.Slf4j;
  6 +import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
  7 +import org.springframework.boot.ApplicationArguments;
  8 +import org.springframework.boot.ApplicationRunner;
  9 +
  10 +import java.net.InetSocketAddress;
  11 +
  12 +/**
  13 + * @author 杜云山
  14 + * @date 21/03/05
  15 + */
  16 +@Slf4j
  17 +public class AbstractVirtualBoxConfiguration implements ApplicationRunner {
  18 +
  19 + private final AbstractVirtualBoxProperties abstractVirtualBoxProperties;
  20 +
  21 + private final AbstractVirtualBoxHandler abstractVirtualBoxHandler;
  22 +
  23 + public AbstractVirtualBoxConfiguration(AbstractVirtualBoxProperties abstractVirtualBoxProperties,
  24 + AbstractVirtualBoxHandler abstractVirtualBoxHandler) {
  25 + this.abstractVirtualBoxProperties = abstractVirtualBoxProperties;
  26 + this.abstractVirtualBoxHandler = abstractVirtualBoxHandler;
  27 + }
  28 +
  29 + @Override
  30 + public void run(ApplicationArguments args) {
  31 +
  32 + String name = abstractVirtualBoxProperties.getName();
  33 + Integer port = abstractVirtualBoxProperties.getPort();
  34 +
  35 + if (!abstractVirtualBoxProperties.getEnable()) {
  36 + log.info("{}服务端 配置未开启", name);
  37 + return;
  38 + }
  39 +
  40 + if (port == null) {
  41 + log.info("{}服务端 端口未配置", name);
  42 + return;
  43 + }
  44 +
  45 + try {
  46 + NioSocketAcceptor acceptor = new NioSocketAcceptor();
  47 + acceptor.setHandler(abstractVirtualBoxHandler);
  48 + acceptor.setReuseAddress(true);
  49 + acceptor.bind(new InetSocketAddress(port));
  50 + log.info("{}服务端已经启动,监听端口: {}", name, port);
  51 + } catch (Exception e) {
  52 + log.error("无法启动{}服务端, {}", name, e.getMessage(), e);
  53 + }
  54 + }
  55 +
  56 +}
... ...
src/main/java/com/example/mina/config/AeroflexVirtualBoxConfiguration.java
... ... @@ -3,48 +3,18 @@ package com.example.mina.config;
3 3 import com.example.mina.box1.AeroflexVirtualBoxHandler;
4 4 import com.example.mina.property.AeroflexVirtualBoxProperties;
5 5 import lombok.extern.slf4j.Slf4j;
6   -import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
7 6 import org.springframework.context.annotation.Configuration;
8 7  
9   -import javax.annotation.PostConstruct;
10   -import java.net.InetSocketAddress;
11   -
12 8 /**
13 9 * @author 杜云山
14 10 * @date 21/03/05
15 11 */
16 12 @Slf4j
17 13 @Configuration(proxyBeanMethods = false)
18   -public class AeroflexVirtualBoxConfiguration {
19   -
20   - private final AeroflexVirtualBoxProperties aeroflexVirtualBoxProperties;
  14 +public class AeroflexVirtualBoxConfiguration extends AbstractVirtualBoxConfiguration {
21 15  
22 16 public AeroflexVirtualBoxConfiguration(AeroflexVirtualBoxProperties aeroflexVirtualBoxProperties) {
23   - this.aeroflexVirtualBoxProperties = aeroflexVirtualBoxProperties;
24   - }
25   -
26   - @PostConstruct
27   - public void init() {
28   -
29   - if (!aeroflexVirtualBoxProperties.getEnable()) {
30   - log.info("AeroflexVirtual服务端 配置未开启");
31   - return;
32   - }
33   -
34   - if (aeroflexVirtualBoxProperties.getPort() == null) {
35   - log.info("AeroflexVirtual服务端 端口未配置");
36   - return;
37   - }
38   -
39   - try {
40   - NioSocketAcceptor acceptor = new NioSocketAcceptor();
41   - acceptor.setHandler(new AeroflexVirtualBoxHandler());
42   - acceptor.setReuseAddress(true);
43   - acceptor.bind(new InetSocketAddress(aeroflexVirtualBoxProperties.getPort()));
44   - log.info("AeroflexVirtual服务端已经启动,监听端口: {}", aeroflexVirtualBoxProperties.getPort());
45   - } catch (Exception e) {
46   - log.error("无法启动AeroflexVirtual服务端, {}", e.getMessage(), e);
47   - }
  17 + super(aeroflexVirtualBoxProperties, new AeroflexVirtualBoxHandler());
48 18 }
49 19  
50 20 }
... ...
src/main/java/com/example/mina/config/Rbm3000VirtualBoxConfiguration.java
... ... @@ -3,49 +3,18 @@ package com.example.mina.config;
3 3 import com.example.mina.box1.Rbm3000VirtualBoxHander;
4 4 import com.example.mina.property.Rbm3000VitualBoxProperties;
5 5 import lombok.extern.slf4j.Slf4j;
6   -import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
7 6 import org.springframework.context.annotation.Configuration;
8 7  
9   -import javax.annotation.PostConstruct;
10   -import java.net.InetSocketAddress;
11   -
12 8 /**
13 9 * @author 杜云山
14 10 * @date 21/03/05
15 11 */
16 12 @Slf4j
17 13 @Configuration(proxyBeanMethods = false)
18   -public class Rbm3000VirtualBoxConfiguration {
19   -
20   - private final Rbm3000VitualBoxProperties rbm3000VitualBoxProperties;
  14 +public class Rbm3000VirtualBoxConfiguration extends AbstractVirtualBoxConfiguration {
21 15  
22 16 public Rbm3000VirtualBoxConfiguration(Rbm3000VitualBoxProperties rbm3000VitualBoxProperties) {
23   - this.rbm3000VitualBoxProperties = rbm3000VitualBoxProperties;
24   - }
25   -
26   -
27   - @PostConstruct
28   - public void init() {
29   -
30   - if (!rbm3000VitualBoxProperties.getEnable()) {
31   - log.info("Rbm3000Virtual服务端 配置未开启");
32   - return;
33   - }
34   -
35   - if (rbm3000VitualBoxProperties.getPort() == null) {
36   - log.info("Rbm3000Virtual服务端 端口未配置");
37   - return;
38   - }
39   -
40   - try {
41   - NioSocketAcceptor acceptor = new NioSocketAcceptor();
42   - acceptor.setHandler(new Rbm3000VirtualBoxHander());
43   - acceptor.setReuseAddress(true);
44   - acceptor.bind(new InetSocketAddress(rbm3000VitualBoxProperties.getPort()));
45   - log.info("Rbm3000Virtual服务端已经启动,监听端口: {}", rbm3000VitualBoxProperties.getPort());
46   - } catch (Exception e) {
47   - log.error("无法启动Rbm3000Virtual服务端, {}", e.getMessage(), e);
48   - }
  17 + super(rbm3000VitualBoxProperties, new Rbm3000VirtualBoxHander());
49 18 }
50 19  
51 20 }
... ...
src/main/java/com/example/mina/property/AbstractVirtualBoxProperties.java 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +package com.example.mina.property;
  2 +
  3 +import lombok.Getter;
  4 +import lombok.Setter;
  5 +
  6 +/**
  7 + * @author 杜云山
  8 + * @date 2021/03/05
  9 + */
  10 +@Getter
  11 +@Setter
  12 +public abstract class AbstractVirtualBoxProperties {
  13 +
  14 + protected String name = "";
  15 +
  16 + protected Boolean enable = false;
  17 +
  18 + protected Integer port = null;
  19 +
  20 +}
0 21 \ No newline at end of file
... ...
src/main/java/com/example/mina/property/AeroflexVirtualBoxProperties.java
... ... @@ -17,10 +17,6 @@ import org.springframework.context.annotation.Configuration;
17 17 @ConfigurationProperties(prefix = "aeroflex-virtual")
18 18 @Configuration
19 19 @EnableConfigurationProperties(AeroflexVirtualBoxProperties.class)
20   -public class AeroflexVirtualBoxProperties {
21   -
22   - private Boolean enable = false;
23   -
24   - private Integer port = null;
  20 +public class AeroflexVirtualBoxProperties extends AbstractVirtualBoxProperties {
25 21  
26 22 }
27 23 \ No newline at end of file
... ...
src/main/java/com/example/mina/property/Rbm3000VitualBoxProperties.java
... ... @@ -17,10 +17,6 @@ import org.springframework.context.annotation.Configuration;
17 17 @ConfigurationProperties(prefix = "rbm3000-vitualbox-virtual")
18 18 @Configuration
19 19 @EnableConfigurationProperties(Rbm3000VitualBoxProperties.class)
20   -public class Rbm3000VitualBoxProperties {
21   -
22   - private Boolean enable = false;
23   -
24   - private Integer port = null;
  20 +public class Rbm3000VitualBoxProperties extends AbstractVirtualBoxProperties {
25 21  
26 22 }
27 23 \ No newline at end of file
... ...
src/main/resources/application.yml
1 1 aeroflex-virtual:
  2 + name: aeroflex-virtual
2 3 enable: true
3 4 port: 9100
4 5 rbm3000-vitualbox-virtual:
  6 + name: rbm3000-vitualbox-virtual
5 7 enable: true
6 8 port: 9101
... ...