带你读《2022技术人的百宝黑皮书》——一个搞定责任链的注解(1)https://developer.aliyun.com/article/1339677?groupCode=taobaotech
在需要生成pipeline的接口上加上@AutoPipeline 只需为这个接口加上@AutoPipeline
@AutoPipeline public interface ConfigSource { String get(String key); }
3、实现pipeline的handler
public class MapConfigSourceHandler implements ConfigSourceHandler { private final Map<String, String> map; public MapConfigSourceHandler(Map<String, String> map) { this.map = map; } @Override public String get(String key, ConfigSourceHandlerContext context) { String value = map.get(key); if (StringUtils.isNotBlank(value)) { return value; } return context.get(key); } } public class SystemConfigSourceHandler implements ConfigSourceHandler {
19 |
|
public static final SystemConfigSourceHandler INSTANCE = new SystemConfigSourceHandler(); |
20 |
|
|
21 |
|
@Override |
22 |
|
public String get(String key, ConfigSourceHandlerContext context) { |
23 |
|
String value = System.getProperty(key); |
24 |
|
if (StringUtils.isNotBlank(value)) { |
25 |
|
return value; |
26 |
|
} |
27 |
|
return context.get(key); |
28 |
|
} |
29 |
} |
|
- 使用pipeline
Map<String, String> mapConfig = new HashMap<String, String>(); mapConfig.put("hello", "world"); ConfigSourceHandler mapConfigSourceHandler = new MapConfigSourceHandler(mapConfig); ConfigSource pipeline = new ConfigSourcePipeline() .addLast(mapConfigSourceHandler) .addLast(SystemConfigSourceHandler.INSTANCE); pipeline.get("hello"); // get value "world" // from mapConfig / mapConfigSourceHandler pipeline.get("java.specification.version") // get value "1.8" // from system properties / SystemConfigSourceHandler
带你读《2022技术人的百宝黑皮书》——一个搞定责任链的注解(3)https://developer.aliyun.com/article/1339675?groupCode=taobaotech