-
Notifications
You must be signed in to change notification settings - Fork 46
Stop using Spring Boot for pure Spring applications #2172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...ring-analyzer/src/main/kotlin/org/utbot/spring/analyzers/PureSpringApplicationAnalyzer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.utbot.spring.analyzers | ||
|
||
import org.springframework.context.annotation.AnnotationConfigApplicationContext | ||
import org.springframework.core.env.ConfigurableEnvironment | ||
import org.utbot.spring.exception.UtBotSpringShutdownException | ||
|
||
class PureSpringApplicationAnalyzer : SpringApplicationAnalyzer { | ||
override fun analyze(sources: Array<Class<*>>, environment: ConfigurableEnvironment): List<String> { | ||
val applicationContext = AnnotationConfigApplicationContext() | ||
applicationContext.register(*sources) | ||
applicationContext.environment = environment | ||
return UtBotSpringShutdownException.catch { applicationContext.refresh() }.beanQualifiedNames | ||
} | ||
|
||
override fun canAnalyze() = true | ||
} |
36 changes: 5 additions & 31 deletions
36
...t-spring-analyzer/src/main/kotlin/org/utbot/spring/analyzers/SpringApplicationAnalyzer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,8 @@ | ||
package org.utbot.spring.analyzers | ||
|
||
import com.jetbrains.rd.util.getLogger | ||
import com.jetbrains.rd.util.info | ||
import org.springframework.boot.builder.SpringApplicationBuilder | ||
import org.springframework.context.ApplicationContextException | ||
import org.utbot.spring.configurators.ApplicationConfigurator | ||
import org.utbot.spring.api.ApplicationData | ||
import org.utbot.spring.postProcessors.UtBotBeanFactoryPostProcessor | ||
import org.springframework.core.env.ConfigurableEnvironment | ||
|
||
val logger = getLogger<SpringApplicationAnalyzer>() | ||
|
||
class SpringApplicationAnalyzer(private val applicationData: ApplicationData) { | ||
|
||
fun analyze(): List<String> { | ||
logger.info { "Current Java version is: " + System.getProperty("java.version") } | ||
|
||
val applicationBuilder = SpringApplicationBuilder(SpringApplicationAnalyzer::class.java) | ||
val applicationConfigurator = ApplicationConfigurator(applicationBuilder, applicationData) | ||
|
||
applicationConfigurator.configureApplication() | ||
|
||
try { | ||
applicationBuilder.build() | ||
applicationBuilder.run() | ||
} catch (e: ApplicationContextException) { | ||
// UtBotBeanFactoryPostProcessor destroys bean definitions | ||
// to prevent Spring application from actually starting and | ||
// that causes it to throw ApplicationContextException. | ||
logger.info { "Bean analysis finished successfully" } | ||
} | ||
return UtBotBeanFactoryPostProcessor.beanQualifiedNames | ||
} | ||
} | ||
interface SpringApplicationAnalyzer { | ||
EgorkaKulikov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fun analyze(sources: Array<Class<*>>, environment: ConfigurableEnvironment): List<String> | ||
fun canAnalyze(): Boolean | ||
} |
37 changes: 37 additions & 0 deletions
37
...ng-analyzer/src/main/kotlin/org/utbot/spring/analyzers/SpringApplicationAnalyzerFacade.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.utbot.spring.analyzers | ||
|
||
import com.jetbrains.rd.util.error | ||
import com.jetbrains.rd.util.getLogger | ||
import com.jetbrains.rd.util.info | ||
import org.springframework.boot.SpringBootVersion | ||
import org.springframework.core.SpringVersion | ||
import org.utbot.spring.api.ApplicationData | ||
import org.utbot.spring.utils.EnvironmentFactory | ||
import org.utbot.spring.utils.SourceFinder | ||
|
||
private val logger = getLogger<SpringApplicationAnalyzerFacade>() | ||
|
||
class SpringApplicationAnalyzerFacade(private val applicationData: ApplicationData) { | ||
|
||
fun analyze(): List<String> { | ||
logger.info { "Current Java version is: " + System.getProperty("java.version") } | ||
logger.info { "Current Spring version is: " + runCatching { SpringVersion.getVersion() }.getOrNull() } | ||
logger.info { "Current Spring Boot version is: " + runCatching { SpringBootVersion.getVersion() }.getOrNull() } | ||
|
||
val sources = SourceFinder(applicationData).findSources() | ||
val environmentFactory = EnvironmentFactory(applicationData) | ||
|
||
for (analyzer in listOf(SpringBootApplicationAnalyzer(), PureSpringApplicationAnalyzer())) { | ||
if (analyzer.canAnalyze()) { | ||
logger.info { "Analyzing with $analyzer" } | ||
try { | ||
return analyzer.analyze(sources, environmentFactory.createEnvironment()) | ||
} catch (e: Throwable) { | ||
logger.error("Analyzer $analyzer failed", e) | ||
} | ||
} | ||
} | ||
logger.error { "All Spring analyzers failed, using empty bean list" } | ||
return emptyList() | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ring-analyzer/src/main/kotlin/org/utbot/spring/analyzers/SpringBootApplicationAnalyzer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.utbot.spring.analyzers | ||
|
||
import org.springframework.boot.builder.SpringApplicationBuilder | ||
import org.springframework.core.env.ConfigurableEnvironment | ||
import org.utbot.spring.exception.UtBotSpringShutdownException | ||
|
||
class SpringBootApplicationAnalyzer : SpringApplicationAnalyzer { | ||
override fun analyze(sources: Array<Class<*>>, environment: ConfigurableEnvironment): List<String> { | ||
val app = SpringApplicationBuilder(*sources) | ||
.environment(environment) | ||
.build() | ||
return UtBotSpringShutdownException.catch { app.run() }.beanQualifiedNames | ||
} | ||
|
||
override fun canAnalyze(): Boolean = try { | ||
this::class.java.classLoader.loadClass("org.springframework.boot.SpringApplication") | ||
true | ||
} catch (e: ClassNotFoundException) { | ||
false | ||
} | ||
} |
67 changes: 0 additions & 67 deletions
67
...spring-analyzer/src/main/kotlin/org/utbot/spring/configurators/ApplicationConfigurator.kt
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
...pring-analyzer/src/main/kotlin/org/utbot/spring/exception/UtBotSpringShutdownException.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.utbot.spring.exception | ||
|
||
import com.jetbrains.rd.util.getLogger | ||
import com.jetbrains.rd.util.info | ||
|
||
private val logger = getLogger<UtBotSpringShutdownException>() | ||
|
||
/** | ||
* Use this exception to shutdown the application | ||
* when all required analysis actions are completed. | ||
*/ | ||
class UtBotSpringShutdownException( | ||
message: String, | ||
val beanQualifiedNames: List<String> | ||
): RuntimeException(message) { | ||
companion object { | ||
fun catch(block: () -> Unit): UtBotSpringShutdownException { | ||
try { | ||
block() | ||
throw IllegalStateException("UtBotSpringShutdownException has not been thrown") | ||
} catch (e: Throwable) { | ||
// Spring sometimes wraps exceptions in other exceptions, so we go over | ||
// all the causes to determine if UtBotSpringShutdownException was thrown | ||
for(cause in generateSequence(e) { it.cause }) | ||
EgorkaKulikov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (cause is UtBotSpringShutdownException) { | ||
logger.info { "UtBotSpringShutdownException has been successfully caught" } | ||
return cause | ||
} | ||
throw e | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.