[转帖]在Java项目中整合Scala_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3026 | 回复: 0   主题: [转帖]在Java项目中整合Scala        下一篇 
jie.liang
注册用户
等级:少校
经验:1003
发帖:77
精华:0
注册:2013-10-11
状态:离线
发送短消息息给jie.liang 加好友    发送短消息息给jie.liang 发消息
发表于: IP:您无权察看 2013-10-22 8:31:16 | [全部帖] [楼主帖] 楼主

Scala是一个运行在Java JVM上的面向对象的语言。它支持函数编程,在语法上比Java更加灵活,同时通过Akka库,Scala支持强大的基于Actor的多线程编程。具有这些优势,使得我最近很想在一个新的项目中使用Scala,但是在新项目中,抛弃我们常用的Java和C#,而直接使用一门新的语言是很困难的。这不仅包括学习新语言这个过程,未来,更为项目的长期发展和日后的开发和支持增加了很多变数。毕竟一门新的语言是不可能在很短的时间内在行业中达到Java和C#的流行度的。

那么,我们就不能在新项目中应用和实践Scala么?通过我的实践,我发现其实我们可以通过简单的Maven配置把Scala集成到我们现有的Java项目中。这样我们可以很简单得在Java项目中集成和使用Scala。在这篇blog里,我给出一个用Scala实现的Hello World Servlet。项目的代码可以在https://github.com/mcai4gl2/scala-integration中找到。

在开发之前,我们首先要配置Scala环境。我在Java开发中使用IntelliJ,首先,在IntelliJ中安装Scala插件。插件安装好后,我们重启IntelliJ,这样我们的运行环境就配置好了。

我们用IntelliJ新建一个Maven项目,添加如下Maven Dependency:



  1. <dependency>  
  2.             <groupId>org.scala-lang</groupId>  
  3.             <artifactId>scala-library</artifactId>  
  4.             <version>2.10.1</version>  
  5.         </dependency>  


同时添加如下plugin:



  1. <plugin>  
  2.                 <groupId>org.apache.maven.plugins</groupId>  
  3.                 <artifactId>maven-surefire-plugin</artifactId>  
  4.                 <version>2.8.1</version>  
  5.                 <configuration>  
  6.                     <includes>  
  7.                         <include>**/*.java</include>  
  8.                         <include>**/*.scala</include>  
  9.                     </includes>  
  10.                 </configuration>  
  11.             </plugin>  
  12.             <plugin>  
  13.                 <groupId>org.scala-tools</groupId>  
  14.                 <artifactId>maven-scala-plugin</artifactId>  
  15.                 <version>2.15.2</version>  
  16.                 <executions>  
  17.                     <execution>  
  18.                         <id>scala-compile-first</id>  
  19.                         <phase>process-resources</phase>  
  20.                         <goals>  
  21.                             <goal>compile</goal>  
  22.                         </goals>  
  23.                     </execution>  
  24.                     <execution>  
  25.                         <id>scala-test-compile</id>  
  26.                         <phase>process-test-resources</phase>  
  27.                         <goals>  
  28.                             <goal>testCompile</goal>  
  29.                         </goals>  
  30.                     </execution>  
  31.                 </executions>  
  32.             </plugin>  


这样就完成了对我们的Java项目添加Scala的步骤。

在下面的Scala代码中,我们实现了一个简单的Servlet返回Hello World:



  1. package weblog.examples.scala 
  2. import org.springframework.stereotype.Controller 
  3. import org.springframework.web.bind.annotation.{RequestMapping, RequestMethod} 
  4. import javax.servlet.http.{HttpServletRequest, HttpServletResponse} 
  5. import java.io.OutputStream 
  6. import org.apache.log4j.Logger 
  7. import org.apache.commons.io.IOUtils 
  8. import HelloWorldServlet._ 
  9. @Controller 
  10. class HelloWorldServlet { 
  11.        @RequestMapping(value = Array("/"), method = Array(RequestMethod.GET)) 
  12.        def helloworld(request: HttpServletRequest, response: HttpServletResponse, outputStream: OutputStream) { 
  13.              log.info("helloworld is called") 
  14.              response.setStatus(HttpServletResponse.SC_OK) 
  15.              IOUtils.write("HELLO WORLD!", outputStream) 
  16.              outputStream.flush 
  17.              outputStream.close 
  18.        } 
  19. object HelloWorldServlet { 
  20.        private var log: Logger = Logger.getLogger(classOf[HelloWorldServlet]) 


当这段代码通过编译之后,就会生成和Java一样的class文件。我们可以看到,用Scala编写的Servlet代码更加简洁,这可以大大提高我们的编程效率。

由于Scala语言普及度的局限,在项目中普及使用还是很有风险的。但是,在我们编写Unit Test的过程中,我们可以很好的使用Scala来提高我们的编程效率。下面是一个用Scala写的对我们的HelloWorldServlet进行测试的单元测试的例子:



  1. package weblog.examples.scala 
  2. import org.springframework.web.servlet.DispatcherServlet 
  3. import org.springframework.mock.web.{MockServletConfig, MockHttpServletResponse, MockHttpServletRequest} 
  4. import org.junit.{Assert, Test, After, Before} 
  5. class HelloWorldServletTest { 
  6.        private var dispatcherServlet : DispatcherServlet = _ 
  7.        private var httpRequest : MockHttpServletRequest = _ 
  8.        private var httpResponse : MockHttpServletResponse = _ 
  9.       
  10.        @Before 
  11.        def before() { 
  12.              val config = new MockServletConfig 
  13.              config.addInitParameter("contextConfigLocation", "classpath:servlet-context.xml") 
  14.              dispatcherServlet = new DispatcherServlet 
  15.              dispatcherServlet.init(config) 
  16.             
  17.              httpRequest = new MockHttpServletRequest 
  18.              httpResponse = new MockHttpServletResponse 
  19.        } 
  20.       
  21.        @After 
  22.        def after() { 
  23.              dispatcherServlet = null 
  24.              httpRequest = null 
  25.              httpResponse = null 
  26.        } 
  27.       
  28.        @Test 
  29.        def testHelloWord() { 
  30.              httpRequest.setMethod("GET") 
  31.              httpRequest.setRequestURI("/") 
  32.             
  33.              dispatcherServlet.service(httpRequest, httpResponse) 
  34.             
  35.              val response = httpResponse.getContentAsString 
  36.             
  37.              Assert.assertEquals("HELLO WORLD!", response) 
  38.        } 


这段代码,与Java相比较要简洁很多,这可以大大提高我们的编程效率。

与完全基于Scala开发相比,这种Java与Scala的混合开发方式有以下几个优势:

  1. 项目本身还是基于Java的,可以很好的使用现有的Java工具,包括CI继承,等
  2. 混合了Java和Scala,可以使程序员根据自己的需要在不同的情况下选择更合适的语言
  3. 在项目未来的持续维护上,我们不需要使用Scala的专门程序员,即使是完全没有Scala经验的Java程序员,也可以进行代码维护


同时我们还可以用这种混合开发方式,对现有的Java项目进行修改,而不是完全的重写。希望这种混合开发方式可以帮助希望使用Scala而又在工作中没有机会的朋友。




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论