mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
Merge branch 'v6-dev' of gitee.com:dromara/hutool into v6-dev
This commit is contained in:
commit
8b1f28913e
@ -12,25 +12,63 @@
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建代理对象<br>
|
||||
* https://gitee.com/dromara/hutool/issues/I74EX7<br>
|
||||
* 某些对象存在非空参数构造,则需遍历查找需要的构造完成代理对象构建。
|
||||
*
|
||||
* @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;
|
||||
|
||||
import lombok.Setter;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.extra.aop.aspects.SimpleAspect;
|
||||
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.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class IssueI74EX7Test {
|
||||
|
||||
@Test
|
||||
void proxyTest() {
|
||||
final SmsBlend smsBlend = new SmsBlendImpl(1);
|
||||
final ProxyEngine engine = new JdkProxyEngine();
|
||||
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);
|
||||
}
|
||||
});
|
||||
engine.proxy(smsBlend, new SimpleAspect());
|
||||
}
|
||||
|
||||
/**
|
||||
* https://gitee.com/dromara/hutool/issues/I74EX7<br>
|
||||
* Enhancer.create()默认调用无参构造,有参构造或者多个构造没有很好的兼容。
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
void cglibProxyTest() {
|
||||
final SmsBlend smsBlend = new SmsBlendImpl(1);
|
||||
final ProxyEngine engine = new SpringCglibProxyEngine();
|
||||
engine.proxy(smsBlend, new SimpleAspect(){
|
||||
private static final long serialVersionUID = 1L;
|
||||
engine.proxy(smsBlend, new SimpleAspect());
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
});
|
||||
@Test
|
||||
void cglibProxyWithoutConstructorTest() {
|
||||
final SmsBlend smsBlend = new SmsBlendImplWithoutConstructor();
|
||||
final ProxyEngine engine = new SpringCglibProxyEngine();
|
||||
engine.proxy(smsBlend, new SimpleAspect());
|
||||
}
|
||||
|
||||
public interface SmsBlend{
|
||||
@ -76,7 +54,7 @@ public class IssueI74EX7Test {
|
||||
|
||||
public static class SmsBlendImpl implements SmsBlend{
|
||||
|
||||
private int status;
|
||||
private final int status;
|
||||
|
||||
public SmsBlendImpl(final int status) {
|
||||
this.status = status;
|
||||
@ -87,4 +65,15 @@ public class IssueI74EX7Test {
|
||||
Console.log("sms send." + status);
|
||||
}
|
||||
}
|
||||
|
||||
@Setter
|
||||
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