mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix bug
This commit is contained in:
parent
79620d83ac
commit
1727e5c566
@ -12,25 +12,61 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.extra.aop.engine.spring;
|
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.Aspect;
|
||||||
import org.dromara.hutool.extra.aop.engine.ProxyEngine;
|
import org.dromara.hutool.extra.aop.engine.ProxyEngine;
|
||||||
import org.springframework.cglib.proxy.Enhancer;
|
import org.springframework.cglib.proxy.Enhancer;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 基于Spring-cglib的切面代理工厂
|
* 基于Spring-cglib的切面代理工厂
|
||||||
*
|
*
|
||||||
* @author looly
|
* @author looly
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class SpringCglibProxyEngine implements ProxyEngine {
|
public class SpringCglibProxyEngine implements ProxyEngine {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public <T> T proxy(final T target, final Aspect aspect) {
|
public <T> T proxy(final T target, final Aspect aspect) {
|
||||||
|
final Class<?> targetClass = target.getClass();
|
||||||
|
|
||||||
final Enhancer enhancer = new Enhancer();
|
final Enhancer enhancer = new Enhancer();
|
||||||
enhancer.setSuperclass(target.getClass());
|
enhancer.setSuperclass(targetClass);
|
||||||
enhancer.setCallback(new SpringCglibInterceptor(target, aspect));
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.extra.aop;
|
package org.dromara.hutool.extra.aop;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
import org.dromara.hutool.core.lang.Console;
|
import org.dromara.hutool.core.lang.Console;
|
||||||
import org.dromara.hutool.extra.aop.aspects.SimpleAspect;
|
import org.dromara.hutool.extra.aop.aspects.SimpleAspect;
|
||||||
import org.dromara.hutool.extra.aop.engine.ProxyEngine;
|
import org.dromara.hutool.extra.aop.engine.ProxyEngine;
|
||||||
@ -19,55 +20,32 @@ import org.dromara.hutool.extra.aop.engine.jdk.JdkProxyEngine;
|
|||||||
import org.dromara.hutool.extra.aop.engine.spring.SpringCglibProxyEngine;
|
import org.dromara.hutool.extra.aop.engine.spring.SpringCglibProxyEngine;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
public class IssueI74EX7Test {
|
public class IssueI74EX7Test {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void proxyTest() {
|
void proxyTest() {
|
||||||
final SmsBlend smsBlend = new SmsBlendImpl(1);
|
final SmsBlend smsBlend = new SmsBlendImpl(1);
|
||||||
final ProxyEngine engine = new JdkProxyEngine();
|
final ProxyEngine engine = new JdkProxyEngine();
|
||||||
engine.proxy(smsBlend, new SimpleAspect(){
|
engine.proxy(smsBlend, new SimpleAspect());
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean before(final Object target, final Method method, final Object[] args) {
|
|
||||||
Console.log("切面进入");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean after(final Object target, final Method method, final Object[] args, final Object returnVal) {
|
|
||||||
Console.log("代理Object:"+target.toString());
|
|
||||||
Console.log("代理方法:"+method.getName());
|
|
||||||
Console.log("代理参数:"+ Arrays.toString(args));
|
|
||||||
return super.after(target, method, args, returnVal);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://gitee.com/dromara/hutool/issues/I74EX7<br>
|
||||||
|
* Enhancer.create()默认调用无参构造,有参构造或者多个构造没有很好的兼容。
|
||||||
|
*
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
void cglibProxyTest() {
|
void cglibProxyTest() {
|
||||||
final SmsBlend smsBlend = new SmsBlendImpl(1);
|
final SmsBlend smsBlend = new SmsBlendImpl(1);
|
||||||
final ProxyEngine engine = new SpringCglibProxyEngine();
|
final ProxyEngine engine = new SpringCglibProxyEngine();
|
||||||
engine.proxy(smsBlend, new SimpleAspect(){
|
engine.proxy(smsBlend, new SimpleAspect());
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean before(final Object target, final Method method, final Object[] args) {
|
|
||||||
Console.log("切面进入");
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Test
|
||||||
public boolean after(final Object target, final Method method, final Object[] args, final Object returnVal) {
|
void cglibProxyWithoutConstructorTest() {
|
||||||
Console.log("代理Object:"+target.toString());
|
final SmsBlend smsBlend = new SmsBlendImplWithoutConstructor();
|
||||||
Console.log("代理方法:"+method.getName());
|
final ProxyEngine engine = new SpringCglibProxyEngine();
|
||||||
Console.log("代理参数:"+ Arrays.toString(args));
|
engine.proxy(smsBlend, new SimpleAspect());
|
||||||
return super.after(target, method, args, returnVal);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface SmsBlend{
|
public interface SmsBlend{
|
||||||
@ -76,7 +54,7 @@ public class IssueI74EX7Test {
|
|||||||
|
|
||||||
public static class SmsBlendImpl implements SmsBlend{
|
public static class SmsBlendImpl implements SmsBlend{
|
||||||
|
|
||||||
private int status;
|
private final int status;
|
||||||
|
|
||||||
public SmsBlendImpl(final int status) {
|
public SmsBlendImpl(final int status) {
|
||||||
this.status = status;
|
this.status = status;
|
||||||
@ -87,4 +65,15 @@ public class IssueI74EX7Test {
|
|||||||
Console.log("sms send." + status);
|
Console.log("sms send." + status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class SmsBlendImplWithoutConstructor implements SmsBlend{
|
||||||
|
|
||||||
|
private int status;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void send() {
|
||||||
|
Console.log("sms send." + status);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user