Skip to content

Instantly share code, notes, and snippets.

@bjonnh
Created April 19, 2019 23:59
Show Gist options
  • Save bjonnh/1be4b8b063493e90f83c9dc818ded7ac to your computer and use it in GitHub Desktop.
Save bjonnh/1be4b8b063493e90f83c9dc818ded7ac to your computer and use it in GitHub Desktop.
/**
* This is a MWE on how to reuse the data classes of your Spring Boot application for other scripts
* I had to do that so I could have a script that extract informations from the DB without going through the API
*/
package com.example
import com.support.DataElement
import org.springframework.boot.Banner
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.domain.EntityScan
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Service
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
import java.lang.System.exit
import org.springframework.boot.SpringApplication
import org.springframework.boot.WebApplicationType
@EnableJpaRepositories
class Config
interface RawRepository : CrudRepository<DataElement, Long>
@Service
@EntityScan("com.support")
class MyService(private val rawRepository: RawRepository) {
fun count(): String {
return rawRepository.count().toString()
}
}
@Suppress("SpringJavaInjectionPointsAutowiringInspection")
@SpringBootApplication
class Counter(private var myService: MyService): CommandLineRunner {
@Override
override fun run(vararg p0: String?) {
println(myService.count())
exit(0)
}
}
fun main(args: Array<String>) {
val app = SpringApplication(Counter::class.java)
app.setBannerMode(Banner.Mode.OFF)
app.webApplicationType = WebApplicationType.NONE
app.run(*args)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment