kotlin에서 registerBean 컴파일 에러 해결법
864
작성한 질문수 8
kotlin으로 toby님의 강의를 따라다가다 안되는 부분이 발생하여 다른분들도 해결하시면 좋을 것 같아 공유차원에서 글 남깁니다!
org.springframwork.context.support.registerBean을 impot하여 해결할 수 있습니다.
관련 docs:
https://docs.spring.io/spring-framework/docs/5.0.0.RELEASE/spring-framework-reference/kotlin.html
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory
import org.springframework.boot.web.server.WebServer
import org.springframework.boot.web.servlet.ServletContextInitializer
import org.springframework.context.support.GenericApplicationContext
import org.springframework.context.support.registerBean
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
class HellobootApplication
fun main(args: Array<String>) {
val myApplicationContext = ApplicationContext()
myApplicationContext.registerSpringBean()
val serverFactory: TomcatServletWebServerFactory = TomcatServletWebServerFactory()
val webServer: WebServer = serverFactory.getWebServer(
ServletContextInitializer {
it.addServlet("frontController", FrontController::class.java)
.addMapping("/*")
}
)
webServer.start()
}
class ApplicationContext{
fun getContext() = springApplicationContext
fun registerSpringBean(){
springApplicationContext.apply{
registerBean<HelloController>()
refresh()
}
}
companion object{
val springApplicationContext: GenericApplicationContext = GenericApplicationContext()
}
}
class FrontController: HttpServlet() {
override fun service(request: HttpServletRequest?, response: HttpServletResponse?) {
requireNotNull(request)
requireNotNull(response)
val springApplicationContext = ApplicationContext().getContext()
val helloController = springApplicationContext.getBean(HelloController::class.java)
//인증, 보안, 다국어, 공통 기능 처리
duplicateCode()
if(request.requestURI == "/hello"
&& request.method == HttpMethod.GET.name
){
val name = request.getParameter("name")
response.status = HttpStatus.OK.value()
response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
response.writer.println(helloController.hello(name))
}else {
response.status = HttpStatus.NOT_FOUND.value()
}
}
}
private fun duplicateCode(){}
11강에서 cmd에서 spring shell에 $ init 하면 Fail 메세지
0
75
2
TestRestTemplate 을 통해 테스트 실행시 웹 요청 정보가 콘솔에 표시되지 않습니다.
0
83
1
섹션7. 자동구성 정보파일분리 강의 질문(@MyAutoConfiguration 붙힌 이유)
0
200
2
WebApplicationContext를 DispatcherServlet에 this로 넘기는 것
0
278
2
인프라 빈 구성 정보의 분리에서 EnableMyAutoConfiguration 질문드립니다.
0
209
2
질문드립니다.
0
232
2
spring boot 3.3.7로 학습중입니다.
0
369
2
Serverproperties 객체 생성 후 @Impor 어노테이션 사용 이유 용도
0
162
2
spring start io 에서 이제더이상 2.x버전은 지원하지 않는 것 같습니다.
1
295
2
Springboot 3.2 이상에서 파라미터 추론관련
0
913
4
binding error
0
220
3
Arrays.copyOf 메서드의 타입 세이프
1
155
2
MyOnClassCondition에 있는 matches method의 Invoke 횟수
1
231
3
인용구의 출처가 궁금합니다.
0
259
1
프로퍼티 빈의 후처리기 도입 AnnotationUtils의 사용
0
236
2
SimpleCacheConfiguration과 빈 등록
0
168
2
MyAutoConfigImportSelector 에서 생성자로 ClassLoader를 주입받을 수 있는 점
0
243
1
IntelliJ project jenerator spring initailizr
0
150
1
강의 자료 레퍼지토리에 업로드
0
215
1
강의자료
0
388
1
Hikari 라이브러리가 없으면 오류가 나는거 아닌가요
0
314
2
Tomcat 포트 프로퍼티 미설정시 랜덤 포트 설정 문의
0
476
5
@Import 로 Bean을 등록해야하는 기준이 뭔지 궁금합니다.
0
339
2
application.properties파일내 프로퍼티 이름
0
210
1





