Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 最终方法的弹簧法替换_Java_Spring - Fatal编程技术网

Java 最终方法的弹簧法替换

Java 最终方法的弹簧法替换,java,spring,Java,Spring,我遇到了Method Replacer,想知道我们是否可以将Method Replacer用于最终方法 代码如下: public class FriendlyImpl implements Friendly { public void sayHello(String name) { System.out.println("Hello "+ name); } } 以下是方法替换程序的代码: import java.lang.reflect.Method; import org.s

我遇到了Method Replacer,想知道我们是否可以将Method Replacer用于最终方法

代码如下:

public class FriendlyImpl implements Friendly {
  public void sayHello(String name) {
    System.out.println("Hello "+ name);
  }
}
以下是方法替换程序的代码:

import java.lang.reflect.Method;
import org.springframework.beans.factory.support.MethodReplacer;

public class FriendlyReplacer implements MethodReplacer {
  public Object reimplement(Object o, Method m, Object[] a)
      throws Throwable {

    System.out.println("Hola "+a[0]);

    return null;
  }
}
下面是bean.xml

<beans>
  <bean id="friendly"
  class="com.habuma.wiring.FriendlyImpl">
    <replaced-method name="sayHello" replacer="friendlyReplacer">
    <arg-type>String</arg-type>
</replaced-method>
  </bean>

 <bean id="friendlyReplacer"
  class="com.habuma.wiring.FriendlyReplacer"/>
</beans>

一串

最终方法不能被重写为注入/JSR-330。

您写的是Spring使用反射api查找私有方法,但是在方法替换概念中,spring创建了我们目标类的一个子类,即我们要替换的方法所在的类,并在spring生成的子类中用我们提供的替换方法覆盖这个方法。
因此,如果该方法是final,则if不能重写voilating java规则的方法

Spring使用反射来查找方法(甚至私有方法)。它还应该能够访问这些最终方法。仍在寻求答案有人对此有何评论?