如何在 Java 8 中使用 Spring 方面保留方法参数名称
How to keep Method Parameters Names with Spring Aspect in Java 8
我已经完成了在 Java 8 中获取参数名称所需的操作。
当 MyBean 没有方面时,我得到名称:'first' 和 'second'.
但是当设置方面时,bean 被标记为 MyBean$$EnhancerBySpringCGLIB 而我所拥有的是:'arg0' 'arg1'
此测试是使用 spring 4.1.5 和 aspectj 1.8.5 进行的。
我错过了什么?
package com.test;
public class MyBean {
public void doSomething(String first, int second) {
System.out.println("something");
}
}
package com.test;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class MinimalAspect {
@Around("execution(* com.test.MyBean.*(..))")
public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Around before is running!");
joinPoint.proceed();
System.out.println("Around after is running!");
}
}
package com.test;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) throws Exception {
ApplicationContext appContext = new ClassPathXmlApplicationContext("spring-config.xml");
MyBean myBean = (MyBean) appContext.getBean("myBean");
for (Method method : myBean.getClass().getMethods()) {
System.out.print(method.getName() + "(");
for (Parameter parameter : method.getParameters()) {
System.out.print(parameter.getType());
System.out.print(' ');
System.out.print(parameter.getName());
System.out.print(' ');
}
System.out.println(")");
}
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd ">
<aop:aspectj-autoproxy />
<bean id="myBean" class="com.test.MyBean" />
<!-- Aspect -->
<bean id="minimalAspect" class="com.test.MinimalAspect" />
</beans>
Spring 由于注释而增强了您的 bean,因此 myBean.getClass()
return 是一个包装器 class 而不是您自己的 class,它具有通用参数名称。这应该return你原来的class
MyBean myBean = (MyBean) appContext.getBean("myBean");
Class clazz = org.springframework.utils.ClassUtils.getUserClass(myBean.getClass());
for (Method method : clazz.getMethods()) {
...
我已经完成了在 Java 8 中获取参数名称所需的操作。 当 MyBean 没有方面时,我得到名称:'first' 和 'second'.
但是当设置方面时,bean 被标记为 MyBean$$EnhancerBySpringCGLIB 而我所拥有的是:'arg0' 'arg1'
此测试是使用 spring 4.1.5 和 aspectj 1.8.5 进行的。
我错过了什么?
package com.test;
public class MyBean {
public void doSomething(String first, int second) {
System.out.println("something");
}
}
package com.test;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class MinimalAspect {
@Around("execution(* com.test.MyBean.*(..))")
public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Around before is running!");
joinPoint.proceed();
System.out.println("Around after is running!");
}
}
package com.test;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) throws Exception {
ApplicationContext appContext = new ClassPathXmlApplicationContext("spring-config.xml");
MyBean myBean = (MyBean) appContext.getBean("myBean");
for (Method method : myBean.getClass().getMethods()) {
System.out.print(method.getName() + "(");
for (Parameter parameter : method.getParameters()) {
System.out.print(parameter.getType());
System.out.print(' ');
System.out.print(parameter.getName());
System.out.print(' ');
}
System.out.println(")");
}
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd ">
<aop:aspectj-autoproxy />
<bean id="myBean" class="com.test.MyBean" />
<!-- Aspect -->
<bean id="minimalAspect" class="com.test.MinimalAspect" />
</beans>
Spring 由于注释而增强了您的 bean,因此 myBean.getClass()
return 是一个包装器 class 而不是您自己的 class,它具有通用参数名称。这应该return你原来的class
MyBean myBean = (MyBean) appContext.getBean("myBean");
Class clazz = org.springframework.utils.ClassUtils.getUserClass(myBean.getClass());
for (Method method : clazz.getMethods()) {
...