Java文件编译的两种方式以及在SpringMVC传参中带来的问题

来源:互联网 发布:软件开发技术发展方向 编辑:程序博客网 时间:2024/05/18 01:42

一、基本概念

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; 
}

 

原帖地址:http://blog.csdn.net/ping_qc/article/details/7265814


原创粉丝点击