update 条件化 SmsAutoConfiguration 的 SmsDao/异常处理器 Bean

This commit is contained in:
Shenlijun 2026-06-30 20:47:19 +08:00
parent 9d9fd42aef
commit 7865f5210e
2 changed files with 29 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package org.dromara.test;
import org.dromara.common.sms.config.SmsAutoConfiguration;
import org.dromara.common.sms.config.properties.SmsProperties;
import org.dromara.common.sms.handler.SmsExceptionHandler;
import org.dromara.sms4j.api.dao.SmsDao;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
@ -43,4 +45,28 @@ public class SmsPropertiesTest {
assertThat(context.getBean(SmsProperties.class).getEnabled()).isEqualTo(true);
});
}
@Tag("dev")
@DisplayName("sms.enabled=false 时不创建 PlusSmsDao / SmsExceptionHandler")
@Test
public void shouldNotCreateSmsBeansWhenDisabled() {
contextRunner
.withPropertyValues("sms.enabled=false")
.run(context -> {
assertThat(context).doesNotHaveBean(SmsDao.class);
assertThat(context).doesNotHaveBean(SmsExceptionHandler.class);
});
}
@Tag("dev")
@DisplayName("sms.enabled=true 时创建 PlusSmsDao / SmsExceptionHandler")
@Test
public void shouldCreateSmsBeansWhenEnabled() {
contextRunner
.withPropertyValues("sms.enabled=true")
.run(context -> {
assertThat(context).hasSingleBean(SmsDao.class);
assertThat(context).hasSingleBean(SmsExceptionHandler.class);
});
}
}

View File

@ -5,6 +5,7 @@ import org.dromara.common.sms.core.dao.PlusSmsDao;
import org.dromara.common.sms.handler.SmsExceptionHandler;
import org.dromara.sms4j.api.dao.SmsDao;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@ -21,6 +22,7 @@ public class SmsAutoConfiguration {
@Primary
@Bean
@ConditionalOnProperty(prefix = "sms", name = "enabled", havingValue = "true")
public SmsDao smsDao() {
return new PlusSmsDao();
}
@ -29,6 +31,7 @@ public class SmsAutoConfiguration {
* 异常处理器
*/
@Bean
@ConditionalOnProperty(prefix = "sms", name = "enabled", havingValue = "true")
public SmsExceptionHandler smsExceptionHandler() {
return new SmsExceptionHandler();
}