mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-15 08:17:24 +00:00
feat[boot]: 支持spring boot的自动化配置
This commit is contained in:
1 parent
47be79785e
commit
44e4782641
37 files changed
+657
-219
No files matched your search
@@ -12,6 +12,7 @@
|
||||
|
||||
<properties>
|
||||
<!-- 本项目的其它module版本号 -->
|
||||
<zfoo.boot.version>3.0</zfoo.boot.version>
|
||||
<zfoo.event.version>3.0</zfoo.event.version>
|
||||
<zfoo.hotswap.version>3.0</zfoo.hotswap.version>
|
||||
<zfoo.monitor.version>3.0</zfoo.monitor.version>
|
||||
|
||||
@@ -98,9 +98,9 @@ public class OrmManager implements IOrmManager {
|
||||
.codecRegistry(pojoCodecRegistry);
|
||||
|
||||
// 设置数据库地址
|
||||
var hostConfig = ormConfig.getHostConfig();
|
||||
if (CollectionUtils.isNotEmpty(hostConfig.getAddressMap())) {
|
||||
var hostList = HostAndPort.toHostAndPortList(hostConfig.getAddressMap().values())
|
||||
var hostConfig = ormConfig.getHost();
|
||||
if (CollectionUtils.isNotEmpty(hostConfig.getAddress())) {
|
||||
var hostList = HostAndPort.toHostAndPortList(hostConfig.getAddress().values())
|
||||
.stream()
|
||||
.map(it -> new ServerAddress(it.getHost(), it.getPort()))
|
||||
.collect(Collectors.toList());
|
||||
@@ -293,8 +293,8 @@ public class OrmManager implements IOrmManager {
|
||||
public EntityDef parserEntityDef(Class<? extends IEntity<?>> clazz) {
|
||||
analyze(clazz);
|
||||
|
||||
var cacheStrategies = ormConfig.getCachesConfig().getCacheStrategies();
|
||||
var persisterStrategies = ormConfig.getPersistersConfig().getPersisterStrategies();
|
||||
var cacheStrategies = ormConfig.getCaches();
|
||||
var persisterStrategies = ormConfig.getPersisters();
|
||||
|
||||
var cache = clazz.getAnnotation(EntityCache.class);
|
||||
var cacheStrategyOptional = cacheStrategies.stream().filter(it -> it.getStrategy().equals(cache.cacheStrategy())).findFirst();
|
||||
|
||||
@@ -23,12 +23,14 @@ public class CacheStrategy {
|
||||
private int size;
|
||||
private long expireMillisecond;
|
||||
|
||||
public CacheStrategy(String strategy, int cacheSize, long expireMillisecond) {
|
||||
this.strategy = strategy;
|
||||
this.size = cacheSize;
|
||||
this.expireMillisecond = expireMillisecond;
|
||||
public CacheStrategy() {
|
||||
}
|
||||
|
||||
public CacheStrategy(String strategy, int size, long expireMillisecond) {
|
||||
this.strategy = strategy;
|
||||
this.size = size;
|
||||
this.expireMillisecond = expireMillisecond;
|
||||
}
|
||||
|
||||
public String getStrategy() {
|
||||
return strategy;
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.orm.model.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class CachesConfig {
|
||||
|
||||
private List<CacheStrategy> cacheStrategies;
|
||||
|
||||
public List<CacheStrategy> getCacheStrategies() {
|
||||
return cacheStrategies;
|
||||
}
|
||||
|
||||
public void setCacheStrategies(List<CacheStrategy> cacheStrategies) {
|
||||
this.cacheStrategies = cacheStrategies;
|
||||
}
|
||||
}
|
||||
@@ -24,14 +24,14 @@ public class HostConfig {
|
||||
private String database;
|
||||
private String user;
|
||||
private String password;
|
||||
private Map<String, String> addressMap;
|
||||
private Map<String, String> address;
|
||||
|
||||
public static HostConfig valueOf(String database, String user, String password, Map<String, String> addressMap) {
|
||||
HostConfig config = new HostConfig();
|
||||
config.database = database;
|
||||
config.user = user;
|
||||
config.password = password;
|
||||
config.addressMap = addressMap;
|
||||
config.address = addressMap;
|
||||
return config;
|
||||
}
|
||||
|
||||
@@ -59,11 +59,11 @@ public class HostConfig {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public Map<String, String> getAddressMap() {
|
||||
return addressMap;
|
||||
public Map<String, String> getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddressMap(Map<String, String> addressMap) {
|
||||
this.addressMap = addressMap;
|
||||
public void setAddress(Map<String, String> address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,8 @@
|
||||
|
||||
package com.zfoo.orm.model.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
@@ -23,11 +25,11 @@ public class OrmConfig {
|
||||
|
||||
private String entityPackage;
|
||||
|
||||
private HostConfig hostConfig;
|
||||
private HostConfig host;
|
||||
|
||||
private CachesConfig cachesConfig;
|
||||
private List<CacheStrategy> caches;
|
||||
|
||||
private PersistersConfig persistersConfig;
|
||||
private List<PersisterStrategy> persisters;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
@@ -45,27 +47,27 @@ public class OrmConfig {
|
||||
this.entityPackage = entityPackage;
|
||||
}
|
||||
|
||||
public HostConfig getHostConfig() {
|
||||
return hostConfig;
|
||||
public HostConfig getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHostConfig(HostConfig hostConfig) {
|
||||
this.hostConfig = hostConfig;
|
||||
public void setHost(HostConfig host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public CachesConfig getCachesConfig() {
|
||||
return cachesConfig;
|
||||
public List<CacheStrategy> getCaches() {
|
||||
return caches;
|
||||
}
|
||||
|
||||
public void setCachesConfig(CachesConfig cachesConfig) {
|
||||
this.cachesConfig = cachesConfig;
|
||||
public void setCaches(List<CacheStrategy> caches) {
|
||||
this.caches = caches;
|
||||
}
|
||||
|
||||
public PersistersConfig getPersistersConfig() {
|
||||
return persistersConfig;
|
||||
public List<PersisterStrategy> getPersisters() {
|
||||
return persisters;
|
||||
}
|
||||
|
||||
public void setPersistersConfig(PersistersConfig persistersConfig) {
|
||||
this.persistersConfig = persistersConfig;
|
||||
public void setPersisters(List<PersisterStrategy> persisters) {
|
||||
this.persisters = persisters;
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,9 @@ public class PersisterStrategy {
|
||||
|
||||
private String config;
|
||||
|
||||
public PersisterStrategy() {
|
||||
}
|
||||
|
||||
public PersisterStrategy(String strategy, String type, String config) {
|
||||
this.strategy = strategy;
|
||||
this.config = config;
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.orm.model.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class PersistersConfig {
|
||||
|
||||
private List<PersisterStrategy> persisterStrategies;
|
||||
|
||||
public List<PersisterStrategy> getPersisterStrategies() {
|
||||
return persisterStrategies;
|
||||
}
|
||||
|
||||
public void setPersisterStrategies(List<PersisterStrategy> persisterStrategies) {
|
||||
this.persisterStrategies = persisterStrategies;
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,10 @@ package com.zfoo.orm.schema;
|
||||
import com.zfoo.orm.OrmContext;
|
||||
import com.zfoo.orm.manager.OrmManager;
|
||||
import com.zfoo.orm.model.accessor.MongodbAccessor;
|
||||
import com.zfoo.orm.model.config.*;
|
||||
import com.zfoo.orm.model.config.CacheStrategy;
|
||||
import com.zfoo.orm.model.config.HostConfig;
|
||||
import com.zfoo.orm.model.config.OrmConfig;
|
||||
import com.zfoo.orm.model.config.PersisterStrategy;
|
||||
import com.zfoo.orm.model.query.MongodbQuery;
|
||||
import com.zfoo.protocol.util.DomUtils;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
@@ -42,7 +45,7 @@ public class OrmDefinitionParser implements BeanDefinitionParser {
|
||||
String name;
|
||||
BeanDefinitionBuilder builder;
|
||||
|
||||
// 注册NetConfig
|
||||
// 注册OrmConfig
|
||||
parseOrmConfig(element, parserContext);
|
||||
|
||||
// 注册OrmSpringContext
|
||||
@@ -81,13 +84,15 @@ public class OrmDefinitionParser implements BeanDefinitionParser {
|
||||
resolvePlaceholder("entity-package", "entityPackage", builder, element, parserContext);
|
||||
|
||||
parseHostConfig(DomUtils.getFirstChildElementByTagName(element, "host"), parserContext);
|
||||
builder.addPropertyReference("hostConfig", HostConfig.class.getCanonicalName());
|
||||
builder.addPropertyReference("host", HostConfig.class.getCanonicalName());
|
||||
|
||||
parseCaches(DomUtils.getFirstChildElementByTagName(element, "caches"), parserContext);
|
||||
builder.addPropertyReference("cachesConfig", CachesConfig.class.getCanonicalName());
|
||||
// 解析caches标签
|
||||
var caches = parseCacheStrategies(DomUtils.getFirstChildElementByTagName(element, "caches"), parserContext);
|
||||
builder.addPropertyValue("caches", caches);
|
||||
|
||||
parsePersisters(DomUtils.getFirstChildElementByTagName(element, "persisters"), parserContext);
|
||||
builder.addPropertyReference("persistersConfig", PersistersConfig.class.getCanonicalName());
|
||||
// 解析persisters标签
|
||||
var persisters = parsePersisterStrategies(DomUtils.getFirstChildElementByTagName(element, "persisters"), parserContext);
|
||||
builder.addPropertyValue("persisters", persisters);
|
||||
|
||||
parserContext.getRegistry().registerBeanDefinition(clazz.getCanonicalName(), builder.getBeanDefinition());
|
||||
}
|
||||
@@ -103,34 +108,11 @@ public class OrmDefinitionParser implements BeanDefinitionParser {
|
||||
resolvePlaceholder("password", "password", builder, element, parserContext);
|
||||
|
||||
var addressMap = parseAddress(element, parserContext);
|
||||
builder.addPropertyValue("addressMap", addressMap);
|
||||
builder.addPropertyValue("address", addressMap);
|
||||
|
||||
parserContext.getRegistry().registerBeanDefinition(clazz.getCanonicalName(), builder.getBeanDefinition());
|
||||
}
|
||||
|
||||
|
||||
private void parseCaches(Element element, ParserContext parserContext) {
|
||||
// 解析caches标签
|
||||
var clazz = CachesConfig.class;
|
||||
var builder = BeanDefinitionBuilder.rootBeanDefinition(clazz);
|
||||
|
||||
var cacheStrategies = parseCacheStrategies(element, parserContext);
|
||||
builder.addPropertyValue("cacheStrategies", cacheStrategies);
|
||||
parserContext.getRegistry().registerBeanDefinition(clazz.getCanonicalName(), builder.getBeanDefinition());
|
||||
}
|
||||
|
||||
|
||||
private void parsePersisters(Element element, ParserContext parserContext) {
|
||||
// 解析persisters标签
|
||||
var clazz = PersistersConfig.class;
|
||||
var builder = BeanDefinitionBuilder.rootBeanDefinition(clazz);
|
||||
|
||||
var persisterStrategies = parsePersisterStrategies(element, parserContext);
|
||||
builder.addPropertyValue("persisterStrategies", persisterStrategies);
|
||||
parserContext.getRegistry().registerBeanDefinition(clazz.getCanonicalName(), builder.getBeanDefinition());
|
||||
}
|
||||
|
||||
|
||||
private ManagedList<BeanDefinitionHolder> parseCacheStrategies(Element element, ParserContext parserContext) {
|
||||
var cacheStrategiesElementList = DomUtils.getChildElementsByTagName(element, "cache");
|
||||
var cacheStrategies = new ManagedList<BeanDefinitionHolder>();
|
||||
|
||||
Reference in new issue
Block a user