|
1 | 1 | package com.codingapi.tx.config; |
2 | 2 |
|
| 3 | +import com.codingapi.tx.config.service.TxManagerTxUrlService; |
3 | 4 | import com.lorne.core.framework.utils.config.ConfigUtils; |
4 | 5 | import org.slf4j.Logger; |
5 | 6 | import org.slf4j.LoggerFactory; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.context.ApplicationContext; |
6 | 9 | import org.springframework.stereotype.Component; |
7 | 10 |
|
8 | 11 | /** |
|
11 | 14 | @Component |
12 | 15 | public class ConfigReader { |
13 | 16 |
|
14 | | - private String txUrl; |
15 | | - |
16 | | - private String configName = "tx.properties"; |
17 | | - |
18 | | - private String configKey = "url"; |
19 | 17 |
|
20 | 18 | private Logger logger = LoggerFactory.getLogger(ConfigReader.class); |
21 | 19 |
|
22 | 20 |
|
23 | | - public ConfigReader() { |
24 | | - loadConfig(); |
25 | | - } |
| 21 | + private TxManagerTxUrlService txManagerTxUrlService; |
26 | 22 |
|
27 | | - private void loadConfig(){ |
28 | | - try { |
29 | | - txUrl = ConfigUtils.getString(configName, configKey); |
| 23 | + @Autowired |
| 24 | + private ApplicationContext spring; |
30 | 25 |
|
31 | | - //兼容3.0的配置地址 |
32 | | - if (txUrl.contains("/tx/manager/getServer")) { |
33 | | - txUrl = txUrl.replace("getServer", ""); |
34 | | - } |
35 | 26 |
|
36 | | - //添加后缀/ |
37 | | - if (!txUrl.endsWith("/")){ |
38 | | - txUrl+="/"; |
39 | | - } |
| 27 | + public String getTxUrl() { |
40 | 28 |
|
| 29 | + try { |
| 30 | + txManagerTxUrlService = spring.getBean(TxManagerTxUrlService.class); |
41 | 31 | }catch (Exception e){ |
42 | 32 | logger.error(e.getLocalizedMessage()); |
43 | 33 | } |
44 | | - } |
45 | 34 |
|
46 | | - /** |
47 | | - * 重新设置配置文件名称 |
48 | | - * @param configName 配置文件名称 |
49 | | - * @param key 配置文件key值 |
50 | | - */ |
51 | | - public void setConfigName(String configName,String key) { |
52 | | - this.configName = configName; |
53 | | - this.configKey = key; |
54 | | - loadConfig(); |
55 | | - } |
| 35 | + if(txManagerTxUrlService == null){ |
| 36 | + txManagerTxUrlService = new TxManagerTxUrlService() { |
56 | 37 |
|
57 | | - /** |
58 | | - * 设置TxManager服务地址 格式如 http://127.0.0.1:8899/tx/manager/ |
59 | | - * @param txUrl |
60 | | - */ |
61 | | - public void setTxUrl(String txUrl) { |
62 | | - this.txUrl = txUrl; |
63 | | - } |
| 38 | + private final String configName = "tx.properties"; |
64 | 39 |
|
| 40 | + private final String configKey = "url"; |
65 | 41 |
|
66 | | - public String getTxUrl() { |
67 | | - return txUrl; |
| 42 | + @Override |
| 43 | + public String getTxUrl() { |
| 44 | + return ConfigUtils.getString(configName,configKey); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + logger.info("load default txManagerTxUrlService"); |
| 49 | + }else{ |
| 50 | + logger.info("load txManagerTxUrlService"); |
| 51 | + } |
| 52 | + |
| 53 | + return txManagerTxUrlService.getTxUrl(); |
68 | 54 | } |
69 | 55 |
|
70 | 56 |
|
|
0 commit comments