首先先扯點關於這幾個東西的相關,不想看的小夥伴可以跳過哈
以往寫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.annotations
annotation 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.Bean
import com.kotlin.annotations.Bean
@Bean
data class Student(var name: String)
然後在applicationContext.xml中添加這麼一行作爲創建對象,當然,如果你的路徑或者名字不是這個,自然是要改一下的
<bean id="student" class="com.kotlin.Bean.Student"/>
最後在main.kt中寫入以下代碼,使用junit進行測試
package com.kotlin
import com.kotlin.Bean.Student
import org.springframework.context.support.FileSystemXmlApplicationContext
import org.junit.Test
class main
{
@Test
fun test()
{
//加載Spring配置文件,創建對象
val context = FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml")
val student = context.getBean("student") as Student
println(student)
}
}