事情中少不了要拟定各类百般的设置文件,这里和各人分享一下事情中我是如何拟定设置文件的,这是小我私家习惯,团结强大的spring,结果很不错。
=============================需求==========================
如我们此刻有一个这样的设置需求,顶层是Server,有port和shutdown2个属性,包括一个service荟萃,service工具有name一个属性,并包括一个connector荟萃,connector工具有port和protocol2个属性。
我一上来不会去思量是用xml照旧json照旧数据库设置,我会第一步写好对应的设置bean。如上面的需求,就写3个bean。bean和bean之间的包括干系要浮现出来。(利用了lombok)
@Data public class ServerCfg { private int port = 8005; private String shutDown = "SHUTDOWN"; private List<ServiceCfg> services; } @Data public class ServiceCfg { private String name; private List<ConnectorCfg> connectors; } @Data public class ConnectorCfg { private int port = 8080; private String protocol = "HTTP/1.1"; }
然后找一个处所先用代码发生这个bean:
@Configuration public class Configs { @Bean public ServerCfg createTestBean() { ServerCfg server = new ServerCfg(); // List<ServiceCfg> services = new ArrayList<ServiceCfg>(); server.setServices(services); // ServiceCfg service = new ServiceCfg(); services.add(service); service.setName("Kitty"); // List<ConnectorCfg> connectors = new ArrayList<ConnectorCfg>(); service.setConnectors(connectors); // ConnectorCfg connectorhttp11 = new ConnectorCfg(); connectorhttp11.setPort(8088); connectorhttp11.setProtocol("HTTP/1.1"); connectors.add(connectorhttp11); // ConnectorCfg connectorAJP = new ConnectorCfg(); connectorAJP.setPort(8089); connectorAJP.setProtocol("AJP"); connectors.add(connectorAJP); return server; } }
然后先测试,看看是否ok。为了演示,我就直接在controller内里挪用一下
@Autowired ServerCfg cfg; @GetMapping(value = "/configTest") @ResponseBody public ResultBean<ServerCfg> configTest() { return new ResultBean<ServerCfg>(cfg); }
测试一下,事情正常
然后举办业务代码编写,比及所有成果测试完毕,就是【开拓后期】,再来界说设置文件。半途虽然少不了修改名目,字段等各类修改,对付我们来说只是修改bean界说,so easy。
都ok了,再抉择利用哪种设置文件。假如是json,图纸加密,我们这样:
==============================JSON===========================
把上面接口挪用的json复制下来,报存到设置文件。
json内容
{ "port": 8005, "shutDown": "SHUTDOWN", "services": [ { "name": "Kitty", "connectors": [ { "port": 8088, "protocol": "HTTP/1.1", "executor": null }, { "port": 8089, "protocol": "AJP", "executor": null } ] } ] }
然后修改config的bean生成的代码为:
import com.fasterxml.jackson.databind.ObjectMapper; @Configuration public class Configs { @Value("classpath:config/tomcat.json") File serverConfigJson; @Bean public ServerCfg readServerConfig() throws IOException { return new ObjectMapper().readValue(serverConfigJson, ServerCfg.class); } }
代码太简捷了,软件开发,有没有?!
==============================XML===========================
假如利用XML,贫苦一点,劳务派遣管理系统,我这里利用XStream序列化和反序列化xml。
首先在bean上增加XStream相存眷解
@Data @XStreamAlias("Server") public class ServerCfg { @XStreamAsAttribute private int port = 8005; @XStreamAsAttribute private String shutDown = "SHUTDOWN"; private List<ServiceCfg> services; } @Data @XStreamAlias("Service") public class ServiceCfg { @XStreamAsAttribute private String name; private List<ConnectorCfg> connectors; } @Data @XStreamAlias("Connector") public class ConnectorCfg { @XStreamAsAttribute private int port = 8080; @XStreamAsAttribute private String protocol = "HTTP/1.1"; }