在Java项目中整合Scala

来源:互联网 发布:如何做采购数据分析 编辑:程序博客网 时间:2024/06/11 02:22

http://www.2cto.com/kf/201310/249427.html

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:
[html] 
<dependency>  
            <groupId>org.scala-lang</groupId>  
            <artifactId>scala-library</artifactId>  
            <version>2.10.1</version>  
        </dependency>  
同时添加如下plugin:
[html] 
<plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-surefire-plugin</artifactId>  
                <version>2.8.1</version>  
                <configuration>  
                    <includes>  
                        <include>**/*.java</include>  
                        <include>**/*.scala</include>  
                    </includes>  
                </configuration>  
            </plugin>  
            <plugin>  
                <groupId>org.scala-tools</groupId>  
                <artifactId>maven-scala-plugin</artifactId>  
                <version>2.15.2</version>  
                <executions>  
                    <execution>  
                        <id>scala-compile-first</id>  
                        <phase>process-resources</phase>  
                        <goals>  
                            <goal>compile</goal>  
                        </goals>  
                    </execution>  
                    <execution>  
                        <id>scala-test-compile</id>  
                        <phase>process-test-resources</phase>  
                        <goals>  
                            <goal>testCompile</goal>  
                        </goals>  
                    </execution>  
                </executions>  
            </plugin>  
 
这样就完成了对我们的Java项目添加Scala的步骤。
在下面的Scala代码中,我们实现了一个简单的Servlet返回Hello World:
[plain]  
package weblog.examples.scala  
  
import org.springframework.stereotype.Controller  
import org.springframework.web.bind.annotation.{RequestMapping, RequestMethod}  
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}  
import java.io.OutputStream  
import org.apache.log4j.Logger  
import org.apache.commons.io.IOUtils  
import HelloWorldServlet._  
  
@Controller  
class HelloWorldServlet {  
  @RequestMapping(value = Array("/"), method = Array(RequestMethod.GET))  
  def helloworld(request: HttpServletRequest, response: HttpServletResponse, outputStream: OutputStream) {  
    log.info("helloworld is called")  
    response.setStatus(HttpServletResponse.SC_OK)  
    IOUtils.write("HELLO WORLD!", outputStream)  
    outputStream.flush  
    outputStream.close  
  }  
}  
  
object HelloWorldServlet {  
  private var log: Logger = Logger.getLogger(classOf[HelloWorldServlet])  
}  
 
当这段代码通过编译之后,就会生成和Java一样的class文件。我们可以看到,用Scala编写的Servlet代码更加简洁,这可以大大提高我们的编程效率。
由于Scala语言普及度的局限,在项目中普及使用还是很有风险的。但是,在我们编写Unit Test的过程中,我们可以很好的使用Scala来提高我们的编程效率。下面是一个用Scala写的对我们的HelloWorldServlet进行测试的单元测试的例子:
[plain] 
package weblog.examples.scala  
  
import org.springframework.web.servlet.DispatcherServlet  
import org.springframework.mock.web.{MockServletConfig, MockHttpServletResponse, MockHttpServletRequest}  
import org.junit.{Assert, Test, After, Before}  
  
class HelloWorldServletTest {  
  private var dispatcherServlet : DispatcherServlet = _  
  private var httpRequest : MockHttpServletRequest = _  
  private var httpResponse : MockHttpServletResponse = _  
  
  @Before  
  def before() {  
    val config = new MockServletConfig  
    config.addInitParameter("contextConfigLocation", "classpath:servlet-context.xml")  
    dispatcherServlet = new DispatcherServlet  
    dispatcherServlet.init(config)  
  
    httpRequest = new MockHttpServletRequest  
    httpResponse = new MockHttpServletResponse  
  }  
  
  @After  
  def after() {  
    dispatcherServlet = null  
    httpRequest = null  
    httpResponse = null  
  }  
  
  @Test  
  def testHelloWord() {  
    httpRequest.setMethod("GET")  
    httpRequest.setRequestURI("/")  
  
    dispatcherServlet.service(httpRequest, httpResponse)  
  
    val response = httpResponse.getContentAsString  
  
    Assert.assertEquals("HELLO WORLD!", response)  
  }  
}  
 
这段代码,与Java相比较要简洁很多,这可以大大提高我们的编程效率。
与完全基于Scala开发相比,这种Java与Scala的混合开发方式有以下几个优势:
项目本身还是基于Java的,可以很好的使用现有的Java工具,包括CI继承,等
混合了Java和Scala,可以使程序员根据自己的需要在不同的情况下选择更合适的语言
在项目未来的持续维护上,我们不需要使用Scala的专门程序员,即使是完全没有Scala经验的Java程序员,也可以进行代码维护
同时我们还可以用这种混合开发方式,对现有的Java项目进行修改,而不是完全的重写。希望这种混合开发方式可以帮助希望使用Scala而又在工作中没有机会的朋友。

0 0