mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix bug
This commit is contained in:
@@ -12,25 +12,61 @@
|
||||
|
||||
package org.dromara.hutool.extra.aop.engine.spring;
|
||||
|
||||
import org.dromara.hutool.core.reflect.ClassUtil;
|
||||
import org.dromara.hutool.core.reflect.ConstructorUtil;
|
||||
import org.dromara.hutool.extra.aop.Aspect;
|
||||
import org.dromara.hutool.extra.aop.engine.ProxyEngine;
|
||||
import org.springframework.cglib.proxy.Enhancer;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
/**
|
||||
* 基于Spring-cglib的切面代理工厂
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class SpringCglibProxyEngine implements ProxyEngine {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T proxy(final T target, final Aspect aspect) {
|
||||
final Class<?> targetClass = target.getClass();
|
||||
|
||||
final Enhancer enhancer = new Enhancer();
|
||||
enhancer.setSuperclass(target.getClass());
|
||||
enhancer.setSuperclass(targetClass);
|
||||
enhancer.setCallback(new SpringCglibInterceptor(target, aspect));
|
||||
return (T) enhancer.create();
|
||||
|
||||
return create(enhancer, targetClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建代理对象
|
||||
*
|
||||
* @param <T> 代理对象类型
|
||||
* @param enhancer {@link Enhancer}
|
||||
* @param targetClass 目标类型
|
||||
* @return 代理对象
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> T create(final Enhancer enhancer, final Class<?> targetClass) {
|
||||
final Constructor<?>[] constructors = ConstructorUtil.getConstructors(targetClass);
|
||||
Class<?>[] parameterTypes;
|
||||
Object[] values;
|
||||
IllegalArgumentException finalException = null;
|
||||
for (final Constructor<?> constructor : constructors) {
|
||||
parameterTypes = constructor.getParameterTypes();
|
||||
values = ClassUtil.getDefaultValues(parameterTypes);
|
||||
|
||||
try {
|
||||
return (T) enhancer.create(parameterTypes, values);
|
||||
} catch (final IllegalArgumentException e) {
|
||||
//ignore
|
||||
finalException = e;
|
||||
}
|
||||
}
|
||||
if (null != finalException) {
|
||||
throw finalException;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("No constructor provided");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user