....
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package kr.lunaticbum.back.lun
|
||||
|
||||
import org.springframework.boot.CommandLineRunner
|
||||
import org.springframework.boot.SpringApplication
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||
import org.springframework.boot.runApplication
|
||||
import org.springframework.context.annotation.Bean
|
||||
import java.util.*
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
class LunApplication
|
||||
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
runApplication<LunApplication>(*args)
|
||||
}
|
||||
//
|
||||
//@SpringBootApplication
|
||||
//class Application {
|
||||
// @Bean
|
||||
// fun commandLineRunner(ctx: ApplicationContext): CommandLineRunner {
|
||||
// return CommandLineRunner { args: Array<String?>? ->
|
||||
// println("Let's inspect the beans provided by Spring Boot:")
|
||||
// val beanNames: Array<String> = ctx.getBeanDefinitionNames()
|
||||
// Arrays.sort(beanNames)
|
||||
// for (beanName in beanNames) {
|
||||
// println(beanName)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// companion object {
|
||||
// @JvmStatic
|
||||
// fun main(args: Array<String>) {
|
||||
// SpringApplication.run(Application::class.java, *args)
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,31 @@
|
||||
package kr.lunaticbum.back.lun.configs
|
||||
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
class AppConfig {
|
||||
// @Bean
|
||||
// fun memberRepository(): MemberRepository {
|
||||
// return MemoryMemberRepository()
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// fun discountPolicy(): DiscountPolicy {
|
||||
// return RateDiscountPolicy()
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// fun memberService(): MemberService {
|
||||
// return MemberServiceImpl(memberRepository())
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// fun orderService(): OrderService {
|
||||
// return OrderServiceImpl(memberRepository(), discountPolicy())
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package kr.lunaticbum.back.lun.configs
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.context.annotation.FilterType
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(excludeFilters = [ComponentScan.Filter(type = FilterType.ANNOTATION, classes = arrayOf(Configuration::class))])
|
||||
class AutoAppConfig {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package kr.lunaticbum.back.lun.configs
|
||||
|
||||
import org.springframework.context.annotation.Configuration
|
||||
|
||||
@Configuration
|
||||
class RootAppContext {
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package kr.lunaticbum.back.lun.configs
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
|
||||
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
|
||||
|
||||
|
||||
// Spring MVC 프로젝트에 관련된 설정을 하는 클래스
|
||||
@Configuration // Controller 어노테이션이 셋팅되어 있는 클래스를 Controller로 등록한다.
|
||||
@EnableWebMvc // 스캔할 패키지를 지정한다.
|
||||
@ComponentScan("kr.lunaticbum.back.lun.controllers")
|
||||
internal class ServletAppContext : WebMvcConfigurer {
|
||||
// Controller의 메서드가 반환하는 jsp의 이름 앞뒤에 경로와 확장자를 붙혀주도록 설정한다.
|
||||
override fun configureViewResolvers(registry: ViewResolverRegistry) {
|
||||
// TODO Auto-generated method stub
|
||||
super.configureViewResolvers(registry)
|
||||
// registry.viewResolver { viewName, locale -> }
|
||||
// registry.jsp("/WEB-INF/views/", ".jsp")
|
||||
}
|
||||
|
||||
// 정적 파일의 경로를 매핑한다.
|
||||
override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
|
||||
// TODO Auto-generated method stub
|
||||
super.addResourceHandlers(registry)
|
||||
registry.addResourceHandler("/**").addResourceLocations("/resources/")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package kr.lunaticbum.back.lun.configs
|
||||
|
||||
import jakarta.servlet.ServletContext
|
||||
import org.springframework.web.WebApplicationInitializer
|
||||
import org.springframework.web.filter.CharacterEncodingFilter
|
||||
|
||||
|
||||
class SpringConfigClass : WebApplicationInitializer {
|
||||
|
||||
override fun onStartup(servletContext: ServletContext) {
|
||||
val filter = servletContext.addFilter("encodingFilter", CharacterEncodingFilter::class.java)
|
||||
filter.setInitParameter("encoding", "UTF-8")
|
||||
filter.addMappingForServletNames(null, false, "dispatcher")
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package kr.lunaticbum.back.lun.configs
|
||||
|
||||
import jakarta.servlet.ServletContext
|
||||
import jakarta.servlet.ServletException
|
||||
import org.springframework.web.WebApplicationInitializer
|
||||
import org.springframework.web.context.ContextLoaderListener
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext
|
||||
import org.springframework.web.servlet.DispatcherServlet
|
||||
|
||||
|
||||
class WebConfig : WebApplicationInitializer {
|
||||
@Throws(ServletException::class)
|
||||
override fun onStartup(servletContext: ServletContext) {
|
||||
// Spring MVC 프로젝트 설정을 위해 작성하는 클래스의 객체를 생성한다.
|
||||
val servletAppContext = AnnotationConfigWebApplicationContext()
|
||||
servletAppContext.register(ServletAppContext::class.java)
|
||||
|
||||
// 요청 발생 시 요청을 처리하는 서블릿을 DispatcherServlet으로 설정해준다.
|
||||
val dispatcherServlet = DispatcherServlet(servletAppContext)
|
||||
val servlet = servletContext.addServlet("dispatcher", dispatcherServlet)
|
||||
|
||||
// 부가 설정
|
||||
servlet.setLoadOnStartup(1)
|
||||
servlet.addMapping("/")
|
||||
|
||||
// Bean을 정의하는 클래스를 지정한다.
|
||||
val rootAppContext = AnnotationConfigWebApplicationContext()
|
||||
rootAppContext.register(RootAppContext::class.java)
|
||||
|
||||
val listener = ContextLoaderListener(rootAppContext)
|
||||
servletContext.addListener(listener)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package kr.lunaticbum.back.lun.controllers
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.ResponseBody
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@RequestMapping("tlbt")
|
||||
class Telegram {
|
||||
@ResponseBody
|
||||
@GetMapping("/hello")
|
||||
fun hello(): String {
|
||||
return "hello1212"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package kr.lunaticbum.back.lun.controllers
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import org.springframework.web.servlet.ModelAndView
|
||||
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
class User(private val viewResolver: ContentNegotiatingViewResolver) {
|
||||
|
||||
@GetMapping("join")
|
||||
fun hello(httpServletRequest: HttpServletRequest): ModelAndView {
|
||||
println("onJoin")
|
||||
val vm = ModelAndView("join")
|
||||
vm.modelMap.put("ddd","asdas")
|
||||
println("${vm.toString()}")
|
||||
return vm
|
||||
}
|
||||
|
||||
|
||||
@ResponseBody
|
||||
@PostMapping("joinUser.api")
|
||||
fun joinUser(httpServletRequest: HttpServletRequest, @RequestBody jsonString: String) : String {
|
||||
println("${httpServletRequest.requestURI}")
|
||||
val reqString = jsonString
|
||||
println(reqString)
|
||||
return "1234"
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@GetMapping("test/{path}")
|
||||
fun test(@PathVariable path : String): String {
|
||||
println("path >>> $path")
|
||||
return "ok"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package kr.lunaticbum.back.lun.utils
|
||||
|
||||
import jakarta.annotation.PostConstruct
|
||||
import jakarta.annotation.PreDestroy
|
||||
import jakarta.servlet.http.HttpServletRequest
|
||||
import lombok.RequiredArgsConstructor
|
||||
import org.springframework.context.annotation.Scope
|
||||
import org.springframework.context.annotation.ScopedProxyMode
|
||||
import org.springframework.stereotype.Component
|
||||
import org.springframework.stereotype.Controller
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.ResponseBody
|
||||
import java.util.*
|
||||
|
||||
|
||||
@Component
|
||||
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||
class Logger {
|
||||
private var uuid: String? = null
|
||||
private var requestURL: String? = null
|
||||
fun setRequestURL(requestURL: String?) {
|
||||
this.requestURL = requestURL
|
||||
}
|
||||
|
||||
fun log(message: String) {
|
||||
println("[$uuid] [$requestURL] $message")
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
fun init() {
|
||||
uuid = UUID.randomUUID().toString()
|
||||
println("[$uuid] request scope bean create:$this")
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
fun close() {
|
||||
println("[$uuid] request scope bean close:$this")
|
||||
}
|
||||
}
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
class LogService {
|
||||
private val myLogger: Logger? = null
|
||||
fun logic(id: String) {
|
||||
myLogger?.log("service id = $id")
|
||||
}
|
||||
}
|
||||
|
||||
@Controller
|
||||
@RequiredArgsConstructor
|
||||
class LogController {
|
||||
private val logService: LogService? = null
|
||||
private val myLogger: Logger? = null
|
||||
|
||||
@RequestMapping("log-demo")
|
||||
@ResponseBody
|
||||
fun logDemo(request: HttpServletRequest): String {
|
||||
val requestURL = request.requestURL.toString()
|
||||
myLogger?.setRequestURL(requestURL)
|
||||
myLogger?.log("controller test")
|
||||
logService!!.logic("testId")
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user