默认是没有办法的。我帮你写了个AOP切面 帮你完成设置属性。
[code="java"]
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.core.annotation.Order;
@Aspect
@Order(Integer.MIN_VALUE)
public class SetterAspect {
@After(value="execution(* *.set*(*)) && args(value)", argNames="value")
public void after(JoinPoint jp, Object value) {
Object proxy = jp.getThis();
Object target = jp.getTarget();
if(AopUtils.isAopProxy(proxy)) {//只有代理对象才需要处理
try {
Class<?> proxyClass = proxy.getClass();
Class<?> targetClass = target.getClass();
String methodName = jp.getSignature().getName();
Method m = BeanUtils.findDeclaredMethod(proxyClass, methodName, new Class[]{value.getClass()});
PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(m);
String propName = descriptor.getName();
Field f = targetClass.getClass().getDeclaredField(propName);
if(f != null) {
f.setAccessible(true);
f.set(proxy, value);
}
} catch (Exception e) {
e.printStackTrace();//记录好异常进行处理
}
}
}
}
[/code]