痛点为 AI 基于上游原始证据的初步提炼;未包含额外中国市场检索。
用户在 Spring Boot 大学项目中需要根据配置属性在 MongoDB 和 PostgreSQL 两种存储模式间动态切换,但当前使用 @ConditionalOnProperty 的方式仅在启动时评估一次,运行时修改属性不会重新创建 bean。这导致用户无法在不重启应用的情况下切换存储实现,而重启会中断服务、增加开发迭代时间,尤其在测试不同存储行为时造成反复启动的重复劳动和效率损失。用户尝试了多种手动方案(如条件判断、创建两个 bean)但均未解决运行时依赖注入不更新的问题,说明现有 Spring 条件注解机制无法满足其动态切换需求,迫使用户寻找其他设计模式。
Stack Overflow question
I am working on a Spring Boot university project where the application can use either MongoDB or PostgreSQL depending on the selected storage mode. Right now I have two repository implementations that use @ConditionalOnProperty . MongoDB repository: @Repository @ConditionalOnProperty(name = "app.storage", havingValue = "mongo") public class MongoTreeRepository implements TreeRepository { } PostgreSQL repository: @Repository @ConditionalOnProperty(name = "app.storage", havingValue = "postgres") public class PostgresTreeRepository implements TreeRepository { } In my application.properties file I use either: app.storage=mongo or: app.storage=postgres This works correctly during application startup. The injected repository changes depending on the property value before Spring Boot initializes the application context. My service currently looks like this: @Service public class TreeService { private final TreeRepository repository; public TreeService(TreeRepository repository) { this.repository = repository; } } The problem started when I tried to understand if Spring Boot can re-evaluate those conditional beans while the application is already running. For example, I wanted to test a scenario like this: Start the application using MongoDB Change the storage mode dynamically Switch to PostgreSQL without restarting the backend I am not trying to reload configuration files automatically. My question is specifically about bean behavior and dependency injection after the Spring context has already been initialized. I already tried: changing the property value at runtime creating both repositories as beans selecting implementations manually using if conditions testing different service configurations However, the injected repository does not change unless I restart the application. I understand that Spring creates beans during application startup, but I am trying to understand whether conditional beans can be refreshed or re-evaluated later without rebuilding the application context. I reviewed similar questions about switching repositories and Spring profiles, but most examples focus on startup configuration instead of bean behavior after the context has already been initialized. My questions are: Is @ConditionalOnProperty evaluated only during Spring Boot startup? Are conditional beans re-created automatically if the property changes later? Would another approach or design pattern be more appropriate for runtime switching between repositories?
Question details
- View count
- 26
- Answer count
- 0
- Last activity
- 2026/05/28
源数据· Raw Archive
- source
- Stack Overflow
- upstream_source
- stackoverflow
- upstream_item_id
- 79948123
- daily_ranking_item_id
- eda84969-9fea-45b4-93f8-3cf24b65d072
- rank_date
- 2026-05-29
- rank
- 7
- name
- Are beans created with @ConditionalOnProperty re-evaluated after Spring Boot startup?
- tagline
- java, postgresql, mongodb, spring-boot, dependency-injection
- description
- I am working on a Spring Boot university project where the application can use either MongoDB or PostgreSQL depending on the selected storage mode. Right now I have two repository implementations that use @ConditionalOnProperty . MongoDB repository: @Repository @ConditionalOnProperty(name = "app.storage", havingValue = "mongo") public class MongoTreeRepository implements TreeRepository { } PostgreSQL repository: @Repository @ConditionalOnProperty(name = "app.storage", havingValue = "postgres") public class PostgresTreeRepository implements TreeRepository { } In my application.properties file I use either: app.storage=mongo or: app.storage=postgres This works correctly during application startup. The injected repository changes depending on the property value before Spring Boot initializes the application context. My service currently looks like this: @Service public class TreeService { private final TreeRepository repository; public TreeService(TreeRepository repository) { this.repository = repository; } } The problem started when I tried to understand if Spring Boot can re-evaluate those conditional beans while the application is already running. For example, I wanted to test a scenario like this: Start the application using MongoDB Change the storage mode dynamically Switch to PostgreSQL without restarting the backend I am not trying to reload configuration files automatically. My question is specifically about bean behavior and dependency injection after the Spring context has already been initialized. I already tried: changing the property value at runtime creating both repositories as beans selecting implementations manually using if conditions testing different service configurations However, the injected repository does not change unless I restart the application. I understand that Spring creates beans during application startup, but I am trying to understand whether conditional beans can be refreshed or re-evaluated later without rebuilding the application context. I reviewed similar questions about switching repositories and Spring profiles, but most examples focus on startup configuration instead of bean behavior after the context has already been initialized. My questions are: Is @ConditionalOnProperty evaluated only during Spring Boot startup? Are conditional beans re-created automatically if the property changes later? Would another approach or design pattern be more appropriate for runtime switching between repositories?
- votes_count
- 0
- comments_count
- 0
- created_at_on_source
- 2026-05-28T20:21:13.000Z
{
"stackoverflow": {
"score": 0,
"view_count": 26,
"is_answered": false,
"top_answers": [],
"answer_count": 0,
"accepted_answer_id": null,
"last_activity_date": 1779999673
}
}{
"stats": {
"score": 0,
"view_count": 26,
"is_answered": false,
"answer_count": 0,
"creation_date": 1779999673,
"last_edit_date": null,
"accepted_answer_id": null,
"last_activity_date": 1779999673
},
"api_wrapper": {
"backoff": null,
"has_more": true,
"page_size": 8,
"quota_max": 300,
"quota_remaining": 294
},
"question_id": 79948123,
"answer_fetch": {
"has_more": false,
"answers_fetched": 0,
"answer_page_size": 3
},
"snapshot_version": "stackoverflow_question_v1"
}{
"id": "1cd7675b-69a8-490e-98dd-9a418d88201b",
"daily_ranking_item_id": "eda84969-9fea-45b4-93f8-3cf24b65d072",
"source": "stackoverflow",
"external_id": "79948123",
"fetched_at": "2026-05-28T22:02:15.509Z",
"question_raw": {
"body": "<p>I am working on a Spring Boot university project where the application can use either MongoDB or PostgreSQL depending on the selected storage mode.</p>\n<p>Right now I have two repository implementations that use <code>@ConditionalOnProperty</code>.</p>\n<p>MongoDB repository:</p>\n<pre><code>@Repository\n@ConditionalOnProperty(name = "app.storage", havingValue = "mongo")\npublic class MongoTreeRepository implements TreeRepository {\n\n}\n</code></pre>\n<p>PostgreSQL repository:</p>\n<pre><code>@Repository\n@ConditionalOnProperty(name = "app.storage", havingValue = "postgres")\npublic class PostgresTreeRepository implements TreeRepository {\n\n}\n</code></pre>\n<p>In my <code>application.properties</code> file I use either:</p>\n<pre><code>app.storage=mongo\n</code></pre>\n<p>or:</p>\n<pre><code>app.storage=postgres\n</code></pre>\n<p>This works correctly during application startup.</p>\n<p>The injected repository changes depending on the property value before Spring Boot initializes the application context.</p>\n<p>My service currently looks like this:</p>\n<pre><code>@Service\npublic class TreeService {\n\n private final TreeRepository repository;\n\n public TreeService(TreeRepository repository) {\n this.repository = repository;\n }\n}\n</code></pre>\n<p>The problem started when I tried to understand if Spring Boot can re-evaluate those conditional beans while the application is already running.</p>\n<p>For example, I wanted to test a scenario like this:</p>\n<ol>\n<li><p>Start the application using MongoDB</p>\n</li>\n<li><p>Change the storage mode dynamically</p>\n</li>\n<li><p>Switch to PostgreSQL without restarting the backend</p>\n</li>\n</ol>\n<p>I am not trying to reload configuration files automatically. My question is specifically about bean behavior and dependency injection after the Spring context has already been initialized.</p>\n<p>I already tried:</p>\n<ul>\n<li><p>changing the property value at runtime</p>\n</li>\n<li><p>creating both repositories as beans</p>\n</li>\n<li><p>selecting implementations manually using <code>if</code> conditions</p>\n</li>\n<li><p>testing different service configurations</p>\n</li>\n</ul>\n<p>However, the injected repository does not change unless I restart the application.</p>\n<p>I understand that Spring creates beans during application startup, but I am trying to understand whether conditional beans can be refreshed or re-evaluated later without rebuilding the application context.</p>\n<p>I reviewed similar questions about switching repositories and Spring profiles, but most examples focus on startup configuration instead of bean behavior after the context has already been initialized.</p>\n<p>My questions are:</p>\n<ul>\n<li><p>Is <code>@ConditionalOnProperty</code> evaluated only during Spring Boot startup?</p>\n</li>\n<li><p>Are conditional beans re-created automatically if the property changes later?</p>\n</li>\n<li><p>Would another approach or design pattern be more appropriate for runtime switching between repositories?</p>\n</li>\n</ul>\n",
"link": "https://stackoverflow.com/questions/79948123/are-beans-created-with-conditionalonproperty-re-evaluated-after-spring-boot-sta",
"tags": [
"java",
"postgresql",
"mongodb",
"spring-boot",
"dependency-injection"
],
"owner": {
"link": "https://stackoverflow.com/users/32769263/j-oslee19",
"user_id": 32769263,
"user_type": "registered",
"account_id": 46417094,
"reputation": 1,
"display_name": "J_oslee19",
"profile_image": "https://i.sstatic.net/jtzvqaIF.jpg?s=256"
},
"score": 0,
"title": "Are beans created with @ConditionalOnProperty re-evaluated after Spring Boot startup?",
"view_count": 26,
"is_answered": false,
"question_id": 79948123,
"answer_count": 0,
"creation_date": 1779999673,
"content_license": "CC BY-SA 4.0",
"last_activity_date": 1779999673
},
"answers_raw": [],
"tags_raw": [
"java",
"postgresql",
"mongodb",
"spring-boot",
"dependency-injection"
],
"stats_raw": {
"score": 0,
"view_count": 26,
"is_answered": false,
"answer_count": 0,
"creation_date": 1779999673,
"last_edit_date": null,
"accepted_answer_id": null,
"last_activity_date": 1779999673
},
"selection_meta": {
"site": "stackoverflow",
"api_wrapper": {
"backoff": null,
"has_more": true,
"page_size": 8,
"quota_max": 300,
"quota_remaining": 294
},
"answer_fetch": {
"backoff": null,
"has_more": false,
"answers_fetched": 0,
"quota_remaining": 267,
"answer_page_size": 3
},
"snapshot_version": "stackoverflow_question_v1",
"selection_strategy": "tag_whitelist_unanswered_high_score_recent_active"
},
"created_at": "2026-05-28T22:02:15.735Z",
"updated_at": "2026-05-28T22:02:15.735Z"
}