Spring Boot 시작 중에 발생한 예외를 FailureAnalyzer로 잡는다.
10332 단어 spring-bootKotlinspring
경위
여기를 참조
htps : // 코 m / 샤 s s ぇ th / ms / 476436 b7c81 81 d b2
아무튼 지정의 포트가 벌써 사용되고 있었을 경우의 Exception은
원래 Spring Boot에서 사용할 수 있습니다.
덮어 쓸 수 있고 추가 할 수 있다는 이야기
환경
Spring Boot 2.2.2.RELEASE - 2.2.4.RELEASE
Kotlin 1.3.61
Gradle
환경은 다음 기사의 만다
Spring Boot 측이 준비해 주는 FailureAnalyzer
예외를 잡으려면 AbstractFailureAnalyzer
을 상속해야합니다.
검색을 통해 나온 수업이

총 17개
또한 각 클래스를 상속 할 수 있기 때문에 더있을 수 있습니다.
포트가 이미 사용되었을 때 호출되는 Exception은 PortInUseException
그것을 잡는 것이 PortInUseFailureAnalyzer
PortInUseFailureAnalyzer.javaclass PortInUseFailureAnalyzer extends AbstractFailureAnalyzer<PortInUseException> {
@Override
protected FailureAnalysis analyze(Throwable rootFailure, PortInUseException cause) {
return new FailureAnalysis("Web server failed to start. Port " + cause.getPort() + " was already in use.",
"Identify and stop the process that's listening on port " + cause.getPort() + " or configure this "
+ "application to listen on another port.",
cause);
}
}
안의 클래스는 이런 느낌
적당히 뒤에서 포트 8080 사용으로

Boot Run으로 앱을 시작하면 ...

이런 느낌
FailureAnalyzer에 있는 Description과 Action이 로그에 기록되어 있다고 합니다.
Spring Boot에서 제공하는 FailureAnalyzer를 자체로 다시 작성
정직 추천은 할 수 없다
MyPortInUseFailureAnalyzer.ktclass MyPortInUseExceptionFailureAnalyzer : AbstractFailureAnalyzer<PortInUseException>() {
override fun analyze(rootFailure: Throwable, cause: PortInUseException): FailureAnalysis {
return FailureAnalysis(
cause.message,
"Port already in use!!!",
cause
)
}
}
AbstractFailureAnalyzer<PortInUseException>
를 상속받은 클래스를 만드십시오.
resources/META-INF/
폴더 아래에 spring.factories
파일을 만듭니다.
resources/META-INF/spring.factoriesorg.springframework.boot.diagnostics.FailureAnalyzer =\
com.example.spring.boot.web.failure.analyzer.MyPortInUseFailureAnalyzer
그 내용은 이런 느낌
이제 등록 OK
또한 적당히 포트 8080을 뒤에서 사용해 두고 Boot Run하면

덮어쓸 수있었습니다.
추천하지 않는다는 것도 예외시에 출력되는 정보를 변경할 수 있기 때문에 실제로 예외가 발생했을 때 무엇이 있었는지 모르기 때문에
출력되는 내용은 바꾸지 않고 처리를 끼우거나 하는 것이 좋은 것일까···?
FailureAnalyzer의 커스터마이즈의 용도로서는 이하가 실용적일지도 모른다
독자적인 Exception을 FailureAnalyzer로 잡는다
SampleFailureAnalyzer.kt/**
* 独自の例外
*/
class SampleException(message: String?) : RuntimeException(message)
/**
* カスタムFailureAnalyzerのサンプル
* アプリ起動中に発生したSampleExceptionをつかむ
*/
class SampleFailureAnalyzer : AbstractFailureAnalyzer<SampleException>() {
override fun analyze(rootFailure: Throwable, cause: SampleException): FailureAnalysis {
return FailureAnalysis(
cause.message,
"Sample Exception!",
cause
)
}
}
이런 식으로 독자적인 예외를 만들어 두고, 그것을 잡는 FailureAnalyzer를 작성해 둔다
spring.factories
이런 느낌
resources/META-INF/spring.factoriesorg.springframework.boot.diagnostics.FailureAnalyzer =\
com.example.spring.boot.web.failure.analyzer.MyPortInUseFailureAnalyzer ,\
com.example.spring.boot.web.failure.analyzer.SampleFailureAnalyzer
여러 FailureAnalyzer를 등록할 수도 있습니다.
그래서 만든 SampleFailureAnalyzer가 제대로 잡을 수 있는지 확인하고 싶습니다.
다음을 적당히 만들어 둔다
SampleFailureAnalyzer.kt/**
* アプリ起動中に読み込まれるサンプルComponent
*/
@Component
class SampleComponent {
fun run() {
throw SampleException("exception!")
}
}
/**
* リフレッシュ時にイベント発火するListenerクラス
*/
@Component
class SampleApplicationListener : ApplicationListener<ContextRefreshedEvent?> {
@Autowired
private val sampleComponent: SampleComponent? = null
/**
* リフレッシュ時にSampleComponentのrunを実行する(例外を発生させる)
*/
override fun onApplicationEvent(event: ContextRefreshedEvent) {
sampleComponent!!.run()
}
}
Spring Boot 2.2.2.RELEASE - 2.2.4.RELEASE
Kotlin 1.3.61
Gradle
환경은 다음 기사의 만다
Spring Boot 측이 준비해 주는 FailureAnalyzer
예외를 잡으려면 AbstractFailureAnalyzer
을 상속해야합니다.
검색을 통해 나온 수업이

총 17개
또한 각 클래스를 상속 할 수 있기 때문에 더있을 수 있습니다.
포트가 이미 사용되었을 때 호출되는 Exception은 PortInUseException
그것을 잡는 것이 PortInUseFailureAnalyzer
PortInUseFailureAnalyzer.javaclass PortInUseFailureAnalyzer extends AbstractFailureAnalyzer<PortInUseException> {
@Override
protected FailureAnalysis analyze(Throwable rootFailure, PortInUseException cause) {
return new FailureAnalysis("Web server failed to start. Port " + cause.getPort() + " was already in use.",
"Identify and stop the process that's listening on port " + cause.getPort() + " or configure this "
+ "application to listen on another port.",
cause);
}
}
안의 클래스는 이런 느낌
적당히 뒤에서 포트 8080 사용으로

Boot Run으로 앱을 시작하면 ...

이런 느낌
FailureAnalyzer에 있는 Description과 Action이 로그에 기록되어 있다고 합니다.
Spring Boot에서 제공하는 FailureAnalyzer를 자체로 다시 작성
정직 추천은 할 수 없다
MyPortInUseFailureAnalyzer.ktclass MyPortInUseExceptionFailureAnalyzer : AbstractFailureAnalyzer<PortInUseException>() {
override fun analyze(rootFailure: Throwable, cause: PortInUseException): FailureAnalysis {
return FailureAnalysis(
cause.message,
"Port already in use!!!",
cause
)
}
}
AbstractFailureAnalyzer<PortInUseException>
를 상속받은 클래스를 만드십시오.
resources/META-INF/
폴더 아래에 spring.factories
파일을 만듭니다.
resources/META-INF/spring.factoriesorg.springframework.boot.diagnostics.FailureAnalyzer =\
com.example.spring.boot.web.failure.analyzer.MyPortInUseFailureAnalyzer
그 내용은 이런 느낌
이제 등록 OK
또한 적당히 포트 8080을 뒤에서 사용해 두고 Boot Run하면

덮어쓸 수있었습니다.
추천하지 않는다는 것도 예외시에 출력되는 정보를 변경할 수 있기 때문에 실제로 예외가 발생했을 때 무엇이 있었는지 모르기 때문에
출력되는 내용은 바꾸지 않고 처리를 끼우거나 하는 것이 좋은 것일까···?
FailureAnalyzer의 커스터마이즈의 용도로서는 이하가 실용적일지도 모른다
독자적인 Exception을 FailureAnalyzer로 잡는다
SampleFailureAnalyzer.kt/**
* 独自の例外
*/
class SampleException(message: String?) : RuntimeException(message)
/**
* カスタムFailureAnalyzerのサンプル
* アプリ起動中に発生したSampleExceptionをつかむ
*/
class SampleFailureAnalyzer : AbstractFailureAnalyzer<SampleException>() {
override fun analyze(rootFailure: Throwable, cause: SampleException): FailureAnalysis {
return FailureAnalysis(
cause.message,
"Sample Exception!",
cause
)
}
}
이런 식으로 독자적인 예외를 만들어 두고, 그것을 잡는 FailureAnalyzer를 작성해 둔다
spring.factories
이런 느낌
resources/META-INF/spring.factoriesorg.springframework.boot.diagnostics.FailureAnalyzer =\
com.example.spring.boot.web.failure.analyzer.MyPortInUseFailureAnalyzer ,\
com.example.spring.boot.web.failure.analyzer.SampleFailureAnalyzer
여러 FailureAnalyzer를 등록할 수도 있습니다.
그래서 만든 SampleFailureAnalyzer가 제대로 잡을 수 있는지 확인하고 싶습니다.
다음을 적당히 만들어 둔다
SampleFailureAnalyzer.kt/**
* アプリ起動中に読み込まれるサンプルComponent
*/
@Component
class SampleComponent {
fun run() {
throw SampleException("exception!")
}
}
/**
* リフレッシュ時にイベント発火するListenerクラス
*/
@Component
class SampleApplicationListener : ApplicationListener<ContextRefreshedEvent?> {
@Autowired
private val sampleComponent: SampleComponent? = null
/**
* リフレッシュ時にSampleComponentのrunを実行する(例外を発生させる)
*/
override fun onApplicationEvent(event: ContextRefreshedEvent) {
sampleComponent!!.run()
}
}
class PortInUseFailureAnalyzer extends AbstractFailureAnalyzer<PortInUseException> {
@Override
protected FailureAnalysis analyze(Throwable rootFailure, PortInUseException cause) {
return new FailureAnalysis("Web server failed to start. Port " + cause.getPort() + " was already in use.",
"Identify and stop the process that's listening on port " + cause.getPort() + " or configure this "
+ "application to listen on another port.",
cause);
}
}
정직 추천은 할 수 없다
MyPortInUseFailureAnalyzer.kt
class MyPortInUseExceptionFailureAnalyzer : AbstractFailureAnalyzer<PortInUseException>() {
override fun analyze(rootFailure: Throwable, cause: PortInUseException): FailureAnalysis {
return FailureAnalysis(
cause.message,
"Port already in use!!!",
cause
)
}
}
AbstractFailureAnalyzer<PortInUseException>
를 상속받은 클래스를 만드십시오.resources/META-INF/
폴더 아래에 spring.factories
파일을 만듭니다.resources/META-INF/spring.factories
org.springframework.boot.diagnostics.FailureAnalyzer =\
com.example.spring.boot.web.failure.analyzer.MyPortInUseFailureAnalyzer
그 내용은 이런 느낌
이제 등록 OK
또한 적당히 포트 8080을 뒤에서 사용해 두고 Boot Run하면

덮어쓸 수있었습니다.
추천하지 않는다는 것도 예외시에 출력되는 정보를 변경할 수 있기 때문에 실제로 예외가 발생했을 때 무엇이 있었는지 모르기 때문에
출력되는 내용은 바꾸지 않고 처리를 끼우거나 하는 것이 좋은 것일까···?
FailureAnalyzer의 커스터마이즈의 용도로서는 이하가 실용적일지도 모른다
독자적인 Exception을 FailureAnalyzer로 잡는다
SampleFailureAnalyzer.kt/**
* 独自の例外
*/
class SampleException(message: String?) : RuntimeException(message)
/**
* カスタムFailureAnalyzerのサンプル
* アプリ起動中に発生したSampleExceptionをつかむ
*/
class SampleFailureAnalyzer : AbstractFailureAnalyzer<SampleException>() {
override fun analyze(rootFailure: Throwable, cause: SampleException): FailureAnalysis {
return FailureAnalysis(
cause.message,
"Sample Exception!",
cause
)
}
}
이런 식으로 독자적인 예외를 만들어 두고, 그것을 잡는 FailureAnalyzer를 작성해 둔다
spring.factories
이런 느낌
resources/META-INF/spring.factoriesorg.springframework.boot.diagnostics.FailureAnalyzer =\
com.example.spring.boot.web.failure.analyzer.MyPortInUseFailureAnalyzer ,\
com.example.spring.boot.web.failure.analyzer.SampleFailureAnalyzer
여러 FailureAnalyzer를 등록할 수도 있습니다.
그래서 만든 SampleFailureAnalyzer가 제대로 잡을 수 있는지 확인하고 싶습니다.
다음을 적당히 만들어 둔다
SampleFailureAnalyzer.kt/**
* アプリ起動中に読み込まれるサンプルComponent
*/
@Component
class SampleComponent {
fun run() {
throw SampleException("exception!")
}
}
/**
* リフレッシュ時にイベント発火するListenerクラス
*/
@Component
class SampleApplicationListener : ApplicationListener<ContextRefreshedEvent?> {
@Autowired
private val sampleComponent: SampleComponent? = null
/**
* リフレッシュ時にSampleComponentのrunを実行する(例外を発生させる)
*/
override fun onApplicationEvent(event: ContextRefreshedEvent) {
sampleComponent!!.run()
}
}
/**
* 独自の例外
*/
class SampleException(message: String?) : RuntimeException(message)
/**
* カスタムFailureAnalyzerのサンプル
* アプリ起動中に発生したSampleExceptionをつかむ
*/
class SampleFailureAnalyzer : AbstractFailureAnalyzer<SampleException>() {
override fun analyze(rootFailure: Throwable, cause: SampleException): FailureAnalysis {
return FailureAnalysis(
cause.message,
"Sample Exception!",
cause
)
}
}
org.springframework.boot.diagnostics.FailureAnalyzer =\
com.example.spring.boot.web.failure.analyzer.MyPortInUseFailureAnalyzer ,\
com.example.spring.boot.web.failure.analyzer.SampleFailureAnalyzer
/**
* アプリ起動中に読み込まれるサンプルComponent
*/
@Component
class SampleComponent {
fun run() {
throw SampleException("exception!")
}
}
/**
* リフレッシュ時にイベント発火するListenerクラス
*/
@Component
class SampleApplicationListener : ApplicationListener<ContextRefreshedEvent?> {
@Autowired
private val sampleComponent: SampleComponent? = null
/**
* リフレッシュ時にSampleComponentのrunを実行する(例外を発生させる)
*/
override fun onApplicationEvent(event: ContextRefreshedEvent) {
sampleComponent!!.run()
}
}
느낌
이제 앱을 시작할 때 오류가 발생합니다.
실행해 보면

제대로 메시지를 낼 수 있습니다!
spring.factories를 등록하지 않은 경우 스택 추적이 표시됩니다.

요약
스택 트레이스 쪽이 알기 쉬운 경우도 있고, 일괄적으로 모든 Exception를 잡는 편이 좋다고는 말할 수 없는 것일까···?
하지만 파악할 수 있는 Exception인지, 발생 가능한 예외인지의 판단은 할 수 있고, 아무튼 편리하다고는 생각한다
Reference
이 문제에 관하여(Spring Boot 시작 중에 발생한 예외를 FailureAnalyzer로 잡는다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/ShassBeleth/items/113e1a2b5703d86d21b5텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)