Spring AI Spring Boot Auto Configuration modules providing automatic setup for AI models, vector stores, MCP, and retry capabilities
Advanced custom Spring Boot conditions used in Spring AI autoconfiguration.
Some autoconfiguration modules use custom conditions for fine-grained control over bean creation.
Checks if Google GenAI CachedContentService can be created.
/**
* Custom condition for Google GenAI cached content service
*
* Location: model-google-genai module
* Used in: GoogleGenAiCachedContentServiceAutoConfiguration
*
* Evaluation Logic:
* 1. Checks if GoogleGenAiChatModel bean exists
* 2. Verifies chat model supports cached content API
* 3. Confirms cached content service is properly configured
*
* @return true if cached content service can be safely created
*/
class CachedContentServiceCondition extends SpringBootCondition {
@Override
public ConditionOutcome getMatchOutcome(
ConditionContext context,
AnnotatedTypeMetadata metadata) {
// Implementation checks chat model capabilities
return ConditionOutcome.match("Cached content service available");
}
}Usage:
@Bean
@Conditional(CachedContentServiceCondition.class)
@ConditionalOnProperty(prefix = "spring.ai.google.genai.chat",
name = "enable-cached-content",
havingValue = "true", matchIfMissing = true)
GoogleGenAiCachedContentService googleGenAiCachedContentService(
GoogleGenAiChatModel chatModel
);Composite condition for enabling non-stdio MCP server transports.
/**
* Composite condition that checks if MCP server stdio should be disabled
*
* Location: mcp-server-common module
* Type: AllNestedConditions (both conditions must be true)
*
* Nested Conditions:
* 1. McpServerEnabledCondition: Checks spring.ai.mcp.server.enabled=true
* 2. StdioDisabledCondition: Checks transport is NOT STDIO
*
* Use Case: Activate non-stdio transports (SSE, STREAMABLE_HTTP)
* when server is enabled
*/
class McpServerStdioDisabledCondition extends AllNestedConditions {
public McpServerStdioDisabledCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnProperty(
prefix = "spring.ai.mcp.server",
name = "enabled",
havingValue = "true"
)
static class McpServerEnabledCondition {}
@ConditionalOnProperty(
prefix = "spring.ai.mcp.server",
name = "transport",
havingValue = "STDIO",
matchIfMissing = false
)
static class StdioDisabledCondition {}
}Example: When spring.ai.mcp.server.transport=SSE, this condition is true, enabling SSE-specific beans.
Distinguishes between AWS and non-AWS OpenSearch.
/**
* Condition that distinguishes between AWS and non-AWS OpenSearch
*
* Location: vector-store-opensearch module
* Used in: OpenSearch vector store autoconfiguration
*
* Detection Logic:
* 1. Checks for AWS-specific configuration properties
* 2. Examines OpenSearch endpoint URL patterns
* 3. Detects AWS SDK classes on classpath
*
* Use Case: Different client configurations for:
* - AWS OpenSearch Service (uses AWS SDK, IAM auth)
* - Self-hosted OpenSearch (uses basic auth, API keys)
*
* @return true for non-AWS OpenSearch, false for AWS OpenSearch
*/
class OpenSearchNonAwsCondition extends SpringBootCondition {
@Override
public ConditionOutcome getMatchOutcome(
ConditionContext context,
AnnotatedTypeMetadata metadata) {
String endpoint = context.getEnvironment()
.getProperty("spring.ai.vectorstore.opensearch.uris");
boolean isAws = endpoint != null &&
(endpoint.contains(".es.amazonaws.com") ||
endpoint.contains(".aoss.amazonaws.com"));
return isAws ?
ConditionOutcome.noMatch("AWS OpenSearch detected") :
ConditionOutcome.match("Non-AWS OpenSearch detected");
}
}Example of creating your own custom condition:
public class CustomModelCondition extends SpringBootCondition {
@Override
public ConditionOutcome getMatchOutcome(
ConditionContext context,
AnnotatedTypeMetadata metadata) {
// Check environment for custom criteria
String modelType = context.getEnvironment()
.getProperty("app.ai.model-type");
if ("custom".equals(modelType)) {
return ConditionOutcome.match("Custom model enabled");
}
return ConditionOutcome.noMatch("Custom model not enabled");
}
}
// Usage
@Bean
@Conditional(CustomModelCondition.class)
public ChatModel customChatModel() {
return new CustomChatModel();
}Custom conditions enable: