First commit
This commit is contained in:
84
src/main/java/link/at17/mid/tushare/interfaces/IConfig.java
Normal file
84
src/main/java/link/at17/mid/tushare/interfaces/IConfig.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package link.at17.mid.tushare.interfaces;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import link.at17.mid.tushare.service.ConfigService;
|
||||
import link.at17.mid.tushare.system.util.SpringContextHolder;
|
||||
|
||||
/**
|
||||
* 配置项必须实现该接口
|
||||
*
|
||||
* @author Doghole
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public interface IConfig<T extends IConfig<T>> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public default boolean saveOrUpdate() {
|
||||
return SpringContextHolder.getBean(ConfigService.class).saveOrUpdate((T)this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存前处理。无论 @ConfigInfo 是否设置 save = true 都会调用
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public default T afterSaving() {
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存后处理。无论 @ConfigInfo 是否设置 save = true 都会调用
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public default T beforeSaving() {
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并到 other,且返回合并后的 other
|
||||
* @return
|
||||
*/
|
||||
public default T mergeTo(T other) {
|
||||
if (!Objects.equals(this, other)) {
|
||||
BeanUtil.copyProperties(this, other,
|
||||
CopyOptions.create().setIgnoreNullValue(true));
|
||||
}
|
||||
return other;
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并其他,并返回本身
|
||||
* @param other
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public default T mergeFrom(T other) {
|
||||
if (!Objects.equals(this, other)) {
|
||||
BeanUtil.copyProperties(other, this,
|
||||
CopyOptions.create().setIgnoreNullValue(true));
|
||||
}
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化完成之后的方法,会在
|
||||
* <code><i>beanFactory</i>.autowireBean(bean)</code>
|
||||
* 和
|
||||
* <code><i>beanFactory</i>.initializeBean(bean, beanName)</code>
|
||||
* 后执行。<br>
|
||||
* @see quant.rich.emoney.config.ConfigServiceFactoryBean
|
||||
*/
|
||||
@PostConstruct
|
||||
public default void afterBeanInit() {}
|
||||
|
||||
public static class Views {
|
||||
public static class Persistence {}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user