Kotlin替换java写J2EE

Kotlin为Intellij IDEA公司出品的一种JVM语言,与Scala类似。有趣的程序员们可以去Kotlin的官网。

http://kotlinlang.org/

这次带来的是Spring+SpringMVC+Kotlin语言的一个J2EE的小demo,希望可以展现Kotlin集OO和OP于一身的优势。有不足之处,还望海涵!
项目的目录结构图如下:
Kotlin替换java写J2EE_第1张图片
POM.xml的配置如下:


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.kotlin.puregroupId>
    <artifactId>PureKotlinWebartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>warpackaging>

    <properties>
        <spring.version>4.2.5.RELEASEspring.version>
        <jackson.version>2.7.3jackson.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-coreartifactId>
            <version>${jackson.version}version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>${jackson.version}version>
        dependency>
    dependencies>
project>

Spring的配置文件如下:


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.kotlin.web">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    context:component-scan>
beans>

SpringMVC的配置文件如下所示:


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <context:component-scan base-package="com.kotlin.web.controller" />
    <mvc:default-servlet-handler />
    <mvc:annotation-driven>
        
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/jsonvalue>
                        <value>text/jsonvalue>
                    list>
                property>
            bean>
        mvc:message-converters>
    mvc:annotation-driven>

beans>

KotlinController的核心代码如下所示:

@RestController
class KotlinController {
    @Autowired
    private var kotlinService: KotlinService? = null

    @Autowired
    private var person: Person? = null

    @RequestMapping("/kotlin")
    fun home() = mapOf("kotlin" to kotlinService?.kotlin(), "map" to kotlinService?.map(), "person" to person)
}

KotlinService的核心代码如下:

@Service
class KotlinService {
    fun kotlin() = "kotlin"

    fun map() = mapOf("one" to 1, "two" to 2, "three" to 3)
}

Person的核心代码如下:

@Component
data class Person(var name: String? = "xuwenhui", var id: Int? = 1)

你可能感兴趣的:(java)