File tree Expand file tree Collapse file tree 11 files changed +162
-28
lines changed
kotlin/com/example/kotlin
kotlin/com/example/kotlin Expand file tree Collapse file tree 11 files changed +162
-28
lines changed Original file line number Diff line number Diff line change 1
- buildscript {
2
- ext {
3
- springBootVersion = ' 2.1.3.RELEASE'
4
- }
5
- repositories {
6
- mavenCentral()
7
- }
8
- dependencies {
9
- classpath(" org.springframework.boot:spring-boot-gradle-plugin:${ springBootVersion} " )
10
- }
1
+ plugins {
2
+ id ' org.springframework.boot' version ' 2.1.3.RELEASE'
3
+ id ' org.jetbrains.kotlin.jvm' version ' 1.2.71'
4
+ id ' org.jetbrains.kotlin.plugin.spring' version ' 1.2.71'
11
5
}
12
6
13
- apply plugin : ' java'
14
- apply plugin : ' eclipse'
15
- apply plugin : ' org.springframework.boot'
16
7
apply plugin : ' io.spring.dependency-management'
17
8
18
9
group = ' com.example'
19
10
version = ' 0.0.1-SNAPSHOT'
20
- sourceCompatibility = 1.8
11
+ sourceCompatibility = ' 1.8'
21
12
22
13
repositories {
23
14
mavenCentral()
24
15
}
25
16
26
17
dependencies {
27
- compile(' org.springframework.boot:spring-boot-starter-cache' )
28
- testCompile(" org.springframework.boot:spring-boot-starter-test" )
18
+ implementation ' org.springframework.boot:spring-boot-starter-cache'
19
+ implementation ' com.fasterxml.jackson.module:jackson-module-kotlin'
20
+ implementation ' org.jetbrains.kotlin:kotlin-reflect'
21
+ implementation ' org.jetbrains.kotlin:kotlin-stdlib-jdk8'
22
+
23
+ testImplementation ' org.springframework.boot:spring-boot-starter-test'
24
+ }
25
+
26
+ compileKotlin {
27
+ kotlinOptions {
28
+ freeCompilerArgs = [' -Xjsr305=strict' ]
29
+ jvmTarget = ' 1.8'
30
+ }
31
+ }
32
+
33
+ compileTestKotlin {
34
+ kotlinOptions {
35
+ freeCompilerArgs = [' -Xjsr305=strict' ]
36
+ jvmTarget = ' 1.8'
37
+ }
29
38
}
Original file line number Diff line number Diff line change 1
- package com .example ;
1
+ package com .example . java ;
2
2
3
3
import org .springframework .boot .SpringApplication ;
4
4
import org .springframework .boot .autoconfigure .SpringBootApplication ;
Original file line number Diff line number Diff line change 1
- package com .example .component ;
1
+ package com .example .java . component ;
2
2
3
- import com .example .domain .Book ;
3
+ import com .example .java . domain .Book ;
4
4
5
5
public interface BookRepository {
6
6
Original file line number Diff line number Diff line change 1
- package com .example .component ;
1
+ package com .example .java . component ;
2
2
3
- import com .example .domain .Book ;
3
+ import com .example .java . domain .Book ;
4
4
import org .slf4j .Logger ;
5
5
import org .slf4j .LoggerFactory ;
6
6
import org .springframework .cache .annotation .CacheEvict ;
Original file line number Diff line number Diff line change 1
- package com .example .domain ;
1
+ package com .example .java . domain ;
2
2
3
3
public class Book {
4
4
Original file line number Diff line number Diff line change
1
+ package com.example.kotlin
2
+
3
+ import org.springframework.boot.autoconfigure.SpringBootApplication
4
+ import org.springframework.boot.runApplication
5
+ import org.springframework.cache.annotation.EnableCaching
6
+
7
+ @SpringBootApplication
8
+ @EnableCaching
9
+ class SpringBootCacheApplication
10
+
11
+ fun main (args : Array <String >) {
12
+ runApplication<SpringBootCacheApplication >(* args)
13
+ }
Original file line number Diff line number Diff line change
1
+ package com.example.kotlin.component
2
+
3
+ import com.example.kotlin.domain.Book
4
+
5
+ interface BookRepository {
6
+
7
+ fun getByIsbn (isbn : String ): Book
8
+
9
+ fun refresh (isbn : String )
10
+
11
+ }
Original file line number Diff line number Diff line change
1
+ package com.example.kotlin.component
2
+
3
+ import com.example.kotlin.domain.Book
4
+ import org.slf4j.LoggerFactory
5
+ import org.springframework.cache.annotation.CacheEvict
6
+ import org.springframework.cache.annotation.Cacheable
7
+ import org.springframework.stereotype.Component
8
+
9
+ @Component
10
+ class SimpleBookRepository : BookRepository {
11
+
12
+ companion object {
13
+ private val logger = LoggerFactory .getLogger(SimpleBookRepository ::class .java)
14
+ private const val CACHE_BOOK = " book"
15
+ }
16
+
17
+ @Cacheable(value = [CACHE_BOOK ], key = " #isbn" )
18
+ override fun getByIsbn (isbn : String ): Book {
19
+ simulateSlowService()
20
+ return Book (isbn, " Some book" )
21
+ }
22
+
23
+ private fun simulateSlowService () {
24
+ try {
25
+ Thread .sleep(3000L )
26
+ } catch (e: InterruptedException ) {
27
+ throw IllegalStateException (e)
28
+ }
29
+
30
+ }
31
+
32
+ @CacheEvict(value = [CACHE_BOOK ], key = " #isbn" )
33
+ override fun refresh (isbn : String ) {
34
+ logger.info(" cache clear => $isbn " )
35
+ }
36
+ }
37
+
Original file line number Diff line number Diff line change
1
+ package com.example.kotlin.domain
2
+
3
+ data class Book (val isbn : String , val title : String ) {
4
+ constructor () : this (" " , " " )
5
+ }
Original file line number Diff line number Diff line change 1
- package com .example ;
1
+ package com .example . java ;
2
2
3
- import com .example .component .BookRepository ;
3
+ import com .example .java . component .BookRepository ;
4
4
import org .junit .After ;
5
5
import org .junit .Before ;
6
6
import org .junit .Test ;
12
12
import org .springframework .test .context .junit4 .SpringRunner ;
13
13
14
14
@ RunWith (SpringRunner .class )
15
- @ SpringBootTest
15
+ @ SpringBootTest ( classes = SpringBootCacheApplication . class )
16
16
public class SpringBootCacheApplicationTests {
17
17
18
18
@ Autowired
19
19
private BookRepository repository ;
20
20
21
21
private long startTime ;
22
- private long endTime ;
23
22
24
23
private static final Logger logger = LoggerFactory .getLogger (SpringBootCacheApplicationTests .class );
25
24
@@ -30,8 +29,7 @@ public void onBefore() {
30
29
31
30
@ After
32
31
public void onAfter () {
33
- endTime = System .currentTimeMillis ();
34
- logger .info ("소요시간: {}ms" , endTime - startTime );
32
+ logger .info ("소요시간: {}ms" , System .currentTimeMillis () - startTime );
35
33
}
36
34
37
35
@ Test
You can’t perform that action at this time.
0 commit comments