This commit is contained in:
Looly
2023-04-24 11:35:14 +08:00
parent 9e49100f03
commit e67bbdec19
29 changed files with 361 additions and 203 deletions

View File

@@ -1,83 +0,0 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.extra.aop;
import org.dromara.hutool.core.reflect.ConstructorUtil;
import org.dromara.hutool.core.spi.SpiUtil;
import java.io.Serializable;
/**
* 代理工厂<br>
* 根据用户引入代理库的不同,产生不同的代理对象
*
* @author looly
*/
public interface ProxyFactory extends Serializable {
/**
* 根据用户引入Cglib与否自动创建代理对象
*
* @param <T> 切面对象类型
* @param target 目标对象
* @param aspectClass 切面对象类
* @return 代理对象
*/
static <T> T createProxy(final T target, final Class<? extends Aspect> aspectClass) {
return createProxy(target, ConstructorUtil.newInstance(aspectClass));
}
/**
* 根据用户引入Cglib与否自动创建代理对象
*
* @param <T> 切面对象类型
* @param target 被代理对象
* @param aspect 切面实现
* @return 代理对象
*/
static <T> T createProxy(final T target, final Aspect aspect) {
return of().proxy(target, aspect);
}
/**
* 根据用户引入Cglib与否创建代理工厂
*
* @return 代理工厂
*/
static ProxyFactory of() {
return SpiUtil.loadFirstAvailable(ProxyFactory.class);
}
/**
* 创建代理
*
* @param <T> 代理对象类型
* @param target 被代理对象
* @param aspectClass 切面实现类,自动实例化
* @return 代理对象
* @since 5.3.1
*/
default <T> T proxy(final T target, final Class<? extends Aspect> aspectClass) {
return proxy(target, ConstructorUtil.newInstanceIfPossible(aspectClass));
}
/**
* 创建代理
*
* @param <T> 代理对象类型
* @param target 被代理对象
* @param aspect 切面实现
* @return 代理对象
*/
<T> T proxy(T target, Aspect aspect);
}

View File

@@ -13,41 +13,54 @@
package org.dromara.hutool.extra.aop;
import org.dromara.hutool.core.classloader.ClassLoaderUtil;
import org.dromara.hutool.extra.aop.engine.ProxyEngine;
import org.dromara.hutool.extra.aop.engine.ProxyEngineFactory;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
/**
* 代理工具类
* @author Looly
*
* @author Looly
*/
public final class ProxyUtil {
/**
* 使用切面代理对象
* 获取动态代理引擎
*
* @param <T> 切面对象类型
* @param target 目标对象
* @param aspectClass 切面对象类
* @return 代理对象
* @return {@link ProxyEngine}
*/
public static <T> T proxy(final T target, final Class<? extends Aspect> aspectClass){
return ProxyFactory.createProxy(target, aspectClass);
public static ProxyEngine getEngine() {
return ProxyEngineFactory.getEngine();
}
/**
* 使用切面代理对象
*
* @param <T> 被代理对象类型
* @param <T> 切面对象类型
* @param target 目标对象
* @param aspectClass 切面对象类
* @return 代理对象
*/
public static <T> T proxy(final T target, final Class<? extends Aspect> aspectClass) {
return getEngine().proxy(target, aspectClass);
}
/**
* 使用切面代理对象
*
* @param <T> 被代理对象类型
* @param target 被代理对象
* @param aspect 切面对象
* @return 代理对象
*/
public static <T> T proxy(final T target, final Aspect aspect){
return ProxyFactory.createProxy(target, aspect);
public static <T> T proxy(final T target, final Aspect aspect) {
return getEngine().proxy(target, aspect);
}
// region ----- JDK Proxy utils
/**
* 创建动态代理对象<br>
* 动态代理对象的创建原理是:<br>
@@ -58,10 +71,10 @@ public final class ProxyUtil {
* 4、将$Proxy0的实例返回给客户端。 <br>
* 5、当调用代理类的相应方法时相当于调用 {@link InvocationHandler#invoke(Object, java.lang.reflect.Method, Object[])} 方法
*
* @param <T> 被代理对象类型
* @param classloader 被代理类对应的ClassLoader
* @param <T> 被代理对象类型
* @param classloader 被代理类对应的ClassLoader
* @param invocationHandler {@link InvocationHandler} ,被代理类通过实现此接口提供动态代理功能
* @param interfaces 代理类中需要实现的被代理类的接口方法
* @param interfaces 代理类中需要实现的被代理类的接口方法
* @return 代理类
*/
@SuppressWarnings("unchecked")
@@ -72,12 +85,13 @@ public final class ProxyUtil {
/**
* 创建动态代理对象
*
* @param <T> 被代理对象类型
* @param <T> 被代理对象类型
* @param invocationHandler {@link InvocationHandler} ,被代理类通过实现此接口提供动态代理功能
* @param interfaces 代理类中需要实现的被代理类的接口方法
* @param interfaces 代理类中需要实现的被代理类的接口方法
* @return 代理类
*/
public static <T> T newProxyInstance(final InvocationHandler invocationHandler, final Class<?>... interfaces) {
return newProxyInstance(ClassLoaderUtil.getClassLoader(), invocationHandler, interfaces);
}
// endregion
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.extra.aop.engine;
import org.dromara.hutool.core.reflect.ConstructorUtil;
import org.dromara.hutool.extra.aop.Aspect;
/**
* 动态代理引擎接口
*
* @author looly
* @since 6.0.0
*/
public interface ProxyEngine {
/**
* 创建代理
*
* @param <T> 代理对象类型
* @param target 被代理对象
* @param aspect 切面实现
* @return 代理对象
*/
<T> T proxy(T target, Aspect aspect);
/**
* 创建代理
*
* @param <T> 代理对象类型
* @param target 被代理对象
* @param aspectClass 切面实现类,自动实例化
* @return 代理对象
* @since 5.3.1
*/
default <T> T proxy(final T target, final Class<? extends Aspect> aspectClass) {
return proxy(target, ConstructorUtil.newInstanceIfPossible(aspectClass));
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.extra.aop.engine;
import org.dromara.hutool.core.lang.Singleton;
import org.dromara.hutool.core.spi.SpiUtil;
/**
* 代理引擎简单工厂<br>
* 根据用户引入代理库的不同,产生不同的代理引擎对象
*
* @author looly
*/
public class ProxyEngineFactory {
/**
* 获得单例的ProxyEngine
*
* @return 单例的ProxyEngine
*/
public static ProxyEngine getEngine() {
return Singleton.get(ProxyEngine.class.getName(), ProxyEngineFactory::createEngine);
}
/**
* 根据用户引入Cglib与否创建代理工厂
*
* @return 代理工厂
*/
public static ProxyEngine createEngine() {
return SpiUtil.loadFirstAvailable(ProxyEngine.class);
}
}

View File

@@ -12,17 +12,16 @@
package org.dromara.hutool.extra.aop.engine.jdk;
import org.dromara.hutool.extra.aop.ProxyFactory;
import org.dromara.hutool.extra.aop.ProxyUtil;
import org.dromara.hutool.extra.aop.Aspect;
import org.dromara.hutool.extra.aop.ProxyUtil;
import org.dromara.hutool.extra.aop.engine.ProxyEngine;
/**
* JDK实现的切面代理
*
* @author looly
*/
public class JdkProxyFactory implements ProxyFactory {
private static final long serialVersionUID = 1L;
public class JdkProxyEngine implements ProxyEngine {
@Override
public <T> T proxy(final T target, final Aspect aspect) {

View File

@@ -12,8 +12,8 @@
package org.dromara.hutool.extra.aop.engine.spring;
import org.dromara.hutool.extra.aop.ProxyFactory;
import org.dromara.hutool.extra.aop.Aspect;
import org.dromara.hutool.extra.aop.engine.ProxyEngine;
import org.springframework.cglib.proxy.Enhancer;
/**
@@ -22,8 +22,7 @@ import org.springframework.cglib.proxy.Enhancer;
* @author looly
*
*/
public class SpringCglibProxyFactory implements ProxyFactory {
private static final long serialVersionUID = 1L;
public class SpringCglibProxyEngine implements ProxyEngine {
@Override
@SuppressWarnings("unchecked")

View File

@@ -17,6 +17,10 @@
* <li>基于Spring-cglib代理</li>
* </ul>
* 考虑到cglib库不再更新且对JDK9+兼容性问题,不再封装
* <pre>
* createEngine proxy
* ProxyEngineFactory =》 ProxyEngine =》 Proxy
* </pre>
*
* @author looly
*

View File

@@ -13,37 +13,75 @@
package org.dromara.hutool.extra.pinyin;
import org.dromara.hutool.core.exception.ExceptionUtil;
import org.dromara.hutool.core.exception.HutoolException;
import org.dromara.hutool.core.text.StrUtil;
/**
* 模板异常
*
* @author xiaoleilu
* @author looly
*/
public class PinyinException extends RuntimeException {
public class PinyinException extends HutoolException {
private static final long serialVersionUID = 1L;
/**
* 构造
*
* @param e 异常
*/
public PinyinException(final Throwable e) {
super(ExceptionUtil.getMessage(e), e);
super(e);
}
/**
* 构造
*
* @param message 消息
*/
public PinyinException(final String message) {
super(message);
}
/**
* 构造
*
* @param messageTemplate 消息模板
* @param params 参数
*/
public PinyinException(final String messageTemplate, final Object... params) {
super(StrUtil.format(messageTemplate, params));
super(messageTemplate, params);
}
public PinyinException(final String message, final Throwable throwable) {
super(message, throwable);
/**
* 构造
*
* @param message 消息
* @param cause 被包装的子异常
*/
public PinyinException(final String message, final Throwable cause) {
super(message, cause);
}
public PinyinException(final String message, final Throwable throwable, final boolean enableSuppression, final boolean writableStackTrace) {
super(message, throwable, enableSuppression, writableStackTrace);
/**
* 构造
*
* @param message 消息
* @param cause 被包装的子异常
* @param enableSuppression 是否启用抑制
* @param writableStackTrace 堆栈跟踪是否应该是可写的
*/
public PinyinException(final String message, final Throwable cause, final boolean enableSuppression, final boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public PinyinException(final Throwable throwable, final String messageTemplate, final Object... params) {
super(StrUtil.format(messageTemplate, params), throwable);
/**
* 构造
*
* @param cause 被包装的子异常
* @param messageTemplate 消息模板
* @param params 参数
*/
public PinyinException(final Throwable cause, final String messageTemplate, final Object... params) {
super(cause, messageTemplate, params);
}
}

View File

@@ -12,25 +12,26 @@
package org.dromara.hutool.extra.pinyin;
import org.dromara.hutool.core.regex.PatternPool;
import org.dromara.hutool.core.regex.ReUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.pinyin.engine.PinyinFactory;
import org.dromara.hutool.extra.pinyin.engine.PinyinEngine;
import org.dromara.hutool.extra.pinyin.engine.PinyinEngineFactory;
/**
* 拼音工具类,封装了TinyPinyin、JPinyin、Pinyin4j通过SPI自动识别。
* 拼音工具类,用于快速获取拼音
*
* @author looly
*/
public class PinyinUtil {
private static final String CHINESE_REGEX = "[\\u4e00-\\u9fa5]";
/**
* 获得全局单例的拼音引擎
*
* @return 全局单例的拼音引擎
*/
public static PinyinEngine getEngine(){
return PinyinFactory.get();
public static PinyinEngine getEngine() {
return PinyinEngineFactory.getEngine();
}
/**
@@ -92,6 +93,6 @@ public class PinyinUtil {
* @return 是否为中文字符
*/
public static boolean isChinese(final char c) {
return '' == c || String.valueOf(c).matches(CHINESE_REGEX);
return '' == c || ReUtil.isMatch(PatternPool.CHINESE, String.valueOf(c));
}
}

View File

@@ -10,7 +10,7 @@
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.extra.pinyin;
package org.dromara.hutool.extra.pinyin.engine;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.text.StrUtil;

View File

@@ -15,24 +15,24 @@ package org.dromara.hutool.extra.pinyin.engine;
import org.dromara.hutool.core.lang.Singleton;
import org.dromara.hutool.core.spi.SpiUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.pinyin.PinyinEngine;
import org.dromara.hutool.extra.pinyin.PinyinException;
import org.dromara.hutool.log.StaticLog;
/**
* 简单拼音引擎工厂用于根据用户引入的拼音库jar自动创建对应的拼音引擎对象
* 简单拼音引擎工厂用于根据用户引入的拼音库jar自动创建对应的拼音引擎对象<br>
* 使用简单工厂Simple Factory模式
*
* @author looly
*/
public class PinyinFactory {
public class PinyinEngineFactory {
/**
* 获得单例的PinyinEngine
*
* @return 单例的PinyinEngine
*/
public static PinyinEngine get(){
return Singleton.get(PinyinEngine.class.getName(), PinyinFactory::of);
public static PinyinEngine getEngine(){
return Singleton.get(PinyinEngine.class.getName(), PinyinEngineFactory::createEngine);
}
/**
@@ -41,8 +41,8 @@ public class PinyinFactory {
*
* @return {@link PinyinEngine}
*/
public static PinyinEngine of() {
final PinyinEngine engine = doCreate();
public static PinyinEngine createEngine() {
final PinyinEngine engine = doCreateEngine();
StaticLog.debug("Use [{}] Engine As Default.", StrUtil.removeSuffix(engine.getClass().getSimpleName(), "Engine"));
return engine;
}
@@ -53,7 +53,7 @@ public class PinyinFactory {
*
* @return {@link PinyinEngine}
*/
private static PinyinEngine doCreate() {
private static PinyinEngine doCreateEngine() {
final PinyinEngine engine = SpiUtil.loadFirstAvailable(PinyinEngine.class);
if(null != engine){
return engine;

View File

@@ -13,7 +13,7 @@
package org.dromara.hutool.extra.pinyin.engine.bopomofo4j;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.pinyin.PinyinEngine;
import org.dromara.hutool.extra.pinyin.engine.PinyinEngine;
import com.rnkrsoft.bopomofo4j.Bopomofo4j;
import com.rnkrsoft.bopomofo4j.ToneType;
@@ -39,6 +39,9 @@ import com.rnkrsoft.bopomofo4j.ToneType;
*/
public class Bopomofo4jEngine implements PinyinEngine {
/**
* 构造
*/
public Bopomofo4jEngine(){
Bopomofo4j.local();
}

View File

@@ -12,7 +12,7 @@
package org.dromara.hutool.extra.pinyin.engine.houbbpinyin;
import org.dromara.hutool.extra.pinyin.PinyinEngine;
import org.dromara.hutool.extra.pinyin.engine.PinyinEngine;
import com.github.houbb.pinyin.constant.enums.PinyinStyleEnum;
import com.github.houbb.pinyin.util.PinyinHelper;

View File

@@ -13,7 +13,7 @@
package org.dromara.hutool.extra.pinyin.engine.jpinyin;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.extra.pinyin.PinyinEngine;
import org.dromara.hutool.extra.pinyin.engine.PinyinEngine;
import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;

View File

@@ -13,7 +13,7 @@
package org.dromara.hutool.extra.pinyin.engine.pinyin4j;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.extra.pinyin.PinyinEngine;
import org.dromara.hutool.extra.pinyin.engine.PinyinEngine;
import org.dromara.hutool.extra.pinyin.PinyinException;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;

View File

@@ -12,7 +12,7 @@
package org.dromara.hutool.extra.pinyin.engine.tinypinyin;
import org.dromara.hutool.extra.pinyin.PinyinEngine;
import org.dromara.hutool.extra.pinyin.engine.PinyinEngine;
import com.github.promeg.pinyinhelper.Pinyin;
/**

View File

@@ -11,7 +11,11 @@
*/
/**
* 拼音工具封装入口为PinyinUtil
* 拼音相关封装
* <pre>
* createEngine getPinyin
* PinyinEngineFactory =》 PinyinEngine =》 拼音
* </pre>
*
* @author looly
*

View File

@@ -1,19 +0,0 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/**
* Servlet封装包括Servlet参数获取、文件上传、Response写出等入口为ServletUtil
*
* @author looly
*
*/
package org.dromara.hutool.extra.servlet;

View File

@@ -28,6 +28,9 @@ import org.dromara.hutool.extra.template.engine.TemplateEngine;
public class TemplateConfig implements Serializable {
private static final long serialVersionUID = 2933113779920339523L;
/**
* 默认配置
*/
public static final TemplateConfig DEFAULT = new TemplateConfig();
/**
@@ -206,17 +209,17 @@ public class TemplateConfig implements Serializable {
@Override
public boolean equals(final Object o) {
if (this == o){
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()){
if (o == null || getClass() != o.getClass()) {
return false;
}
final TemplateConfig that = (TemplateConfig) o;
return Objects.equals(charset, that.charset) &&
Objects.equals(path, that.path) &&
resourceMode == that.resourceMode &&
Objects.equals(customEngine, that.customEngine);
Objects.equals(path, that.path) &&
resourceMode == that.resourceMode &&
Objects.equals(customEngine, that.customEngine);
}
@Override

View File

@@ -12,38 +12,74 @@
package org.dromara.hutool.extra.template;
import org.dromara.hutool.core.exception.ExceptionUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.exception.HutoolException;
/**
* 模板异常
*
* @author xiaoleilu
*/
public class TemplateException extends RuntimeException {
public class TemplateException extends HutoolException {
private static final long serialVersionUID = 8247610319171014183L;
/**
* 构造
*
* @param e 异常
*/
public TemplateException(final Throwable e) {
super(ExceptionUtil.getMessage(e), e);
super(e);
}
/**
* 构造
*
* @param message 消息
*/
public TemplateException(final String message) {
super(message);
}
/**
* 构造
*
* @param messageTemplate 消息模板
* @param params 参数
*/
public TemplateException(final String messageTemplate, final Object... params) {
super(StrUtil.format(messageTemplate, params));
super(messageTemplate, params);
}
public TemplateException(final String message, final Throwable throwable) {
super(message, throwable);
/**
* 构造
*
* @param message 消息
* @param cause 被包装的子异常
*/
public TemplateException(final String message, final Throwable cause) {
super(message, cause);
}
public TemplateException(final String message, final Throwable throwable, final boolean enableSuppression, final boolean writableStackTrace) {
super(message, throwable, enableSuppression, writableStackTrace);
/**
* 构造
*
* @param message 消息
* @param cause 被包装的子异常
* @param enableSuppression 是否启用抑制
* @param writableStackTrace 堆栈跟踪是否应该是可写的
*/
public TemplateException(final String message, final Throwable cause, final boolean enableSuppression, final boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public TemplateException(final Throwable throwable, final String messageTemplate, final Object... params) {
super(StrUtil.format(messageTemplate, params), throwable);
/**
* 构造
*
* @param cause 被包装的子异常
* @param messageTemplate 消息模板
* @param params 参数
*/
public TemplateException(final Throwable cause, final String messageTemplate, final Object... params) {
super(cause, messageTemplate, params);
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.extra.template;
import org.dromara.hutool.extra.template.engine.TemplateEngine;
import org.dromara.hutool.extra.template.engine.TemplateEngineFactory;
import java.io.Writer;
import java.util.Map;
/**
* 提供模板工具类,用于快捷模板融合
*
* @author looly
*/
public class TemplateUtil {
/**
* 获取单例的模板引擎
*
* @return {@link TemplateEngine}
*/
public static TemplateEngine getEngine() {
return TemplateEngineFactory.getEngine();
}
/**
* 融合模板和参数,返回融合后的内容
*
* @param templateContent 模板内容
* @param bindingMap 参数
* @return 内容
*/
public static String render(final String templateContent, final Map<?, ?> bindingMap) {
return getEngine().getTemplate(templateContent).render(bindingMap);
}
/**
* 融合模板和参数,返回融合后的内容
*
* @param templateContent 模板内容
* @param bindingMap 参数
* @param writer 融合内容输出的位置
*/
public static void render(final String templateContent, final Map<?, ?> bindingMap, final Writer writer) {
getEngine().getTemplate(templateContent).render(bindingMap, writer);
}
}

View File

@@ -10,21 +10,23 @@
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.extra.template;
package org.dromara.hutool.extra.template.engine;
import org.dromara.hutool.core.lang.Singleton;
import org.dromara.hutool.core.reflect.ConstructorUtil;
import org.dromara.hutool.core.spi.SpiUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.template.engine.TemplateEngine;
import org.dromara.hutool.extra.template.TemplateConfig;
import org.dromara.hutool.extra.template.TemplateException;
import org.dromara.hutool.log.StaticLog;
/**
* 简单模板殷勤工厂用于根据用户引入的模板引擎jar自动创建对应的模板引擎对象
* 简单模板引擎工厂用于根据用户引入的模板引擎jar自动创建对应的模板引擎对象<br>
* 使用简单工厂Simple Factory模式
*
* @author looly
*/
public class TemplateFactory {
public class TemplateEngineFactory {
/**
* 根据用户引入的模板引擎jar自动创建对应的模板引擎对象<br>
@@ -33,7 +35,7 @@ public class TemplateFactory {
* @return 单例的TemplateEngine
*/
public static TemplateEngine getEngine() {
return Singleton.get(TemplateEngine.class.getName(), TemplateFactory::createEngine);
return Singleton.get(TemplateEngine.class.getName(), TemplateEngineFactory::createEngine);
}
/**
@@ -55,7 +57,7 @@ public class TemplateFactory {
* @return {@link TemplateEngine}
*/
public static TemplateEngine createEngine(final TemplateConfig config) {
final TemplateEngine engine = doCreate(config);
final TemplateEngine engine = doCreateEngine(config);
StaticLog.debug("Use [{}] Engine As Default.", StrUtil.removeSuffix(engine.getClass().getSimpleName(), "Engine"));
return engine;
}
@@ -67,7 +69,7 @@ public class TemplateFactory {
* @param config 模板配置包括编码模板文件path等信息
* @return {@link TemplateEngine}
*/
private static TemplateEngine doCreate(final TemplateConfig config) {
private static TemplateEngine doCreateEngine(final TemplateConfig config) {
final Class<? extends TemplateEngine> customEngineClass = config.getCustomEngine();
final TemplateEngine engine;
if (null != customEngineClass) {

View File

@@ -11,10 +11,15 @@
*/
/**
* 第三方模板引擎封装,提供统一的接口用于适配第三方模板引擎
* 第三方模板引擎封装,提供统一的接口用于适配第三方模板引擎,提供:
* <ul>
* <li>TemplateEngine模板引擎接口用于不同引擎的实现。</li>
* <li>Template 模板接口,用于不同引擎模板对象包装。</li>
* <li>TemplateConfig模板配置用于提供公共配置项。</li>
* </ul>
* <pre>
*
* TemplateFactory =》 TemplateEngine =》 Template =》 内容
* createEngine getTemplate render
* TemplateEngineFactory =》 TemplateEngine =》 Template =》 内容
* </pre>
*
* @author looly