Java文件編譯的兩種方式以及在SpringMVC傳參中帶來的問題
一、基本概念
java編譯成.class 有兩種方式。使用javac,默認使用的release方式,而使用的MyEclipse工具,用的是debug模式。值得注意的是使用release模式下對於函數參數會改變。
public class Test{
private static void sayHello(){
System.out.println("Hello world");
}
public static void main(String[] args){
sayHello();
}
}
以上代碼分別用javac命令和MyEclipse IDE編譯,然後使用jd-gui.exe查看Test.class,可以發現區別。
使用MyEclipse編譯(debug)
public class Test
{
private static void sayHello()
{
System.out.println("Hello world");
}
public static void main(String[] args) {
sayHello();
}
}
使用javac編譯(release)
public class Test
{
private static void sayHello()
{
System.out.println("Hello world");
}
public static void main(String[] paramArrayOfString) {
sayHello();
}
}
二、發現問題
以SpringMVC為例
@RequestMapping(/test/{str})
public String test(@PathVariable String str){
System.out.println(str);
return null;
}
實際項目部署使用的是release版本,這樣str經過編譯後就和RequestMapping中的{str}這樣就對應不起來了。而這樣的問題在開發中是不會發現的。這也可以解釋為什麼在spring MVC 中controller的注解初始化參數建議指定名稱。建議寫法如下:
@RequestMapping(/test/{str})
public String test(@PathVariable("str") String str){
System.out.println(str);
return null;
}
原帖地址:https://blog.csdn.net/ping_qc/article/details/7265814
最後更新:2017-04-03 16:49:00