Kotlin的Spring之旅(一):使用IDEA搭建Gradle+Kotlin+Spring项目

来源:互联网 发布:淘宝网店代理 编辑:程序博客网 时间:2024/06/07 04:12

首先先扯点关于这几个东西的相关,不想看的小伙伴可以跳过哈

以往写JavaEE的项目基本都是用eclipse或者MyEclipse,但是自从用了一次IDEA之后,发现这实在是太好用了,不管是快捷键还是各种支持什么的,都很不错,而且集成了很多东西在内,感觉对新手是相当的友好,对git之类的支持更是无比的便捷,感觉完全回不去了,可惜就是收费的,不过MyEclipse也是收费的,不是么๑乛◡乛๑(大家懂就行)

kotlin这东西可能不少人还不大清楚,这是一门新语言,它对java是完全支持的,因为他们编译出来的字节码就是一样的,也就是说,java能做的它都能做。而且它就是对java的一个简化和加强,简化了语法,强化了功能,简直就是就是咱们java开发者的福星有木有!想想看搞IOS的用swift几行就能搞定的东西,用java写同样的要几十行,那感觉太痛苦了!不过有了kotlin一切都不是问题了,现在咱们也可以像他们一样几行搞定以前几十行都完成不了的功能了!具体我就不细说了,毕竟这篇不是说kotlin的哈

Gradle这东西搞java的小伙伴应该都不陌生,这可是个好东西,全面支持Maven,使用依赖管理,对项目管理来说还是相当的方便的

Spring我想就更不用说了,搞这行的基本上都离不开它

韶完了这些就进入正题吧

1. 创建项目

首先打开IDEA新建一个项目

这里写图片描述

在左边的侧边栏选择Gradle,右边勾选Kotlin和web,你也可以把java也勾选上,这样可以写一些java代码作为对比,之后就一路next了

这里写图片描述

这里的GroupId和ArtifactId就随便你自己填写了

这里写图片描述

如果需要的话可以吧自动导包什么的勾上也没事,如果你需要用自己本地的Gradle也是可以自己选的

这里写图片描述

给你的项目起个名字,选择下存储地址

2. 配置Tomcat

首先,你会发现我这项目怎么这么寒酸呢,没关系,我们可以自己配置。

这里写图片描述

首先打开Edit Configurations

这里写图片描述
这里写图片描述

点击绿色加号下拉找到Tomcat,选择Local(如果你的电脑里面连tomcat都没有,那赶紧去装一个吧,具体这里就不说了,毕竟这不是这篇文章的重点)

这里写图片描述

然后给你的Tomcat起个名字,如果自动找的路径不对也要配置下,端口如果需要的话你也可以换一个,然后点击Deployment

这里写图片描述
这里写图片描述

然后点击绿色加号选择第一个将本项目加入其中,然后就可以一路确定了

这里写图片描述

最后赶紧点击绿色启动按钮试一试成功没有

这里写图片描述

这里写图片描述

这里写图片描述

如果启动后输出了Connected to server而且浏览器localhost:8080也没有问题,那么Tomcat配置就成功了。如果报了Application Server was not connected before run configuration stop, reason: Unable to ping server at localhost:1099这么个错的话,可以看下你的问题是不是和我的一样

下一步就是生成web.xml了

这里写图片描述
这里写图片描述

打开你的项目的Structure,找到Web Gradle,点击绿色加号,添加web.xml

这里写图片描述

不过很明显它的这个自动生成的路径完全不对,把它改成我们项目下的src\main\webapp\WEB-INF\web.xml

这里写图片描述

然后就是确定生成了

3.加入spring依赖

由于使用的是Gradle,只需要在build.gradle里面加一点东西就行了

compile "org.springframework:spring-context:4.3.9.RELEASE"compile "org.springframework:spring-beans:4.3.9.RELEASE"compile "org.springframework:spring-core:4.3.9.RELEASE"compile "org.springframework:spring-expression:4.3.9.RELEASE"

在dependencies中加入这几行就可以了,当然,版本是可以换的,至于kotlin,IDEA已经贴心的给我们配置好了,完整的build.gradle是这样的

group 'com.gradle.kotlin'version '1.0-SNAPSHOT'buildscript {    ext.kotlin_version = '1.1.2-5'    repositories {        jcenter()    }    dependencies {        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"    }}apply plugin: 'kotlin'apply plugin: 'war'repositories {    jcenter()}dependencies {    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"    compile "org.springframework:spring-context:4.3.9.RELEASE"    compile "org.springframework:spring-beans:4.3.9.RELEASE"    compile "org.springframework:spring-core:4.3.9.RELEASE"    compile "org.springframework:spring-expression:4.3.9.RELEASE"    testCompile group: 'junit', name: 'junit', version: '4.11'}

这里稍微提一下,本来的应该是mavenCentral,但是由于国内的原因,还是jcenter比较快,我就改成jcenter了,两个其实都是可以的

学过spring的小伙伴可能觉得我的包导的少了,这几个只是spring最基本的几个包,后面需要的时候我会把剩下的再一一加进去的

4.创建applicationContext.xml

对于这个官方是建议用这个名字,并把这个文件放在src目录下的

其实这些都无所谓,名字可以自己起,位置也可以自己随便放。名字我是不太在意,目录我还是喜欢把它放在WEB-INF下的,当然你们随意

要提一下的是如果你把它放在src下的话可以用ClassPathXmlApplicationContext(“applicationContext.xml”)来找到这个文件,如果你是放在其他位置就要用FileSystemXmlApplicationContext和项目下的全路径来找到这个文件了,比如我的就是val context = FileSystemXmlApplicationContext(“src/main/webapp/WEB-INF/applicationContext.xml”)

以下是applicationContext.xml的内容

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"></beans>

5.测试代码

配置完以后自然要试一下,在此之前就需要导入两个kotlin的辅助包了all-open和noarg。这就要涉及到kotlin的一些特性了。

由于kotlin默认data数据类是没有无参构造的这就会使spring创建对象时候出错,所以必须要导入noarg包。并且kotlin规定函数和函数参数默认都是final的,所以你必须要手动加上open,这也很烦,于是就有了allopen。

你需要在你的build.gradle里面再加一点东西,于是乎现在的build.gradle就是这样的了

group 'com.gradle.kotlin'version '1.0-SNAPSHOT'buildscript {    ext.kotlin_version = '1.1.2-5'    repositories {        jcenter()    }    dependencies {        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"        classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"    }}apply plugin: 'kotlin'apply plugin: 'war'apply plugin: 'kotlin-noarg'apply plugin: 'kotlin-allopen'noArg{    annotation("com.kotlin.annotations.Bean")}allOpen{    annotation("com.kotlin.annotations.Bean")}repositories {    jcenter()}dependencies {    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"    compile "org.springframework:spring-context:4.3.9.RELEASE"    compile "org.springframework:spring-beans:4.3.9.RELEASE"    compile "org.springframework:spring-core:4.3.9.RELEASE"    compile "org.springframework:spring-expression:4.3.9.RELEASE"    testCompile group: 'junit', name: 'junit', version: '4.11'}

这里有一个annotation的路径,这是需要你自己创建的,我们就来手动做一下吧

这里写图片描述

首先我创建了几个包,名字自然随便你了,但是main下的kotlin目录是一定要有的,然后新建一个KotlinFile,在里面输入annotation class Bean也就是这样:

package com.kotlin.annotationsannotation class Bean

这样就可以了,当然,这个class的名字也是随便你改的。然后把你的这个文件的路径复制到build.gradle里面的

noArg{    annotation("com.kotlin.annotations.Bean")}allOpen{    annotation("com.kotlin.annotations.Bean")}

替换两个路径地址

这里写图片描述

然后就开始测试吧

这里写图片描述

首先我创建了main.kt和一个Bean包,Bean包里面创建了一个Beans.kt,以后我们所有bean都可以写在beans中了。在其中写一个Student类,这个注释就是你刚刚创建的那个类

package com.kotlin.Beanimport com.kotlin.annotations.Bean@Beandata class Student(var name: String)

然后在applicationContext.xml中添加这么一行作为创建对象,当然,如果你的路径或者名字不是这个,自然是要改一下的

 <bean id="student" class="com.kotlin.Bean.Student"/>

最后在main.kt中写入以下代码,使用junit进行测试

package com.kotlinimport com.kotlin.Bean.Studentimport org.springframework.context.support.FileSystemXmlApplicationContextimport org.junit.Testclass main{    @Test    fun test()    {        //加载Spring配置文件,创建对象        val context = FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml")        val student = context.getBean("student") as Student        println(student)    }}

点击开始后,如果有这样的输出,那么就说明没有问题了

这里写图片描述

这串测试代码是spring的一种最基本的运用,如果小伙伴们以前没学过,看不懂的没关系,下一章就会仔细说一下的。本人也是刚开始学习,如果有什么问题的,请大家不吝赐教啊。:.゚ヽ(。◕‿◕。)ノ゚.:。+゚

阅读全文
0 0
原创粉丝点击