gpt4 book ai didi

java - 禁用@EnableAutoConfiguration

转载 作者:行者123 更新时间:2023-11-30 03:15:16 25 4
gpt4 key购买 nike

我想知道如何摆脱@EnableAutoConfiguration

spring boot documentation提到了

If you need to find out what auto-configuration is currently being applied, and why, starting your application with the --debug switch

但我找不到 shell 中应用了哪些自动配置。

它看起来怎么样?如何为我的应用程序导入/启用必要的配置?我特别感兴趣的是加载 spring 数据自动配置以快速运行集成测试。

==更新==

添加

@RunWith(SpringJUnit4ClassRunner.class)
@EntityScan("persistence.entities")
@EnableJpaRepositories("persistence.repositories")
@ActiveProfiles("dev")
@Configuration
@Import({ AopAutoConfiguration.class, AopAutoConfiguration.JdkDynamicAutoProxyConfiguration.class,
AuditAutoConfiguration.class, DataSourceAutoConfiguration.class,
DataSourceAutoConfiguration.class, DataSourceAutoConfiguration.class,
DataSourcePoolMetadataProvidersConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
JpaBaseConfiguration.class, JpaRepositoriesAutoConfiguration.class,
JtaAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class,
PersistenceExceptionTranslationAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, ServerPropertiesAutoConfiguration.class,
ServerPropertiesAutoConfiguration.class, SpringDataWebAutoConfiguration.class })
@EnableIntegration
@SpringApplicationConfiguration(classes = QueryTests.class)
public class QueryTests {...

我的 JUnit 测试现在抛出一个 java.lang.IllegalStateException:无法检索 @EnableAutoConfiguration 基础包(见下文)。

Caused by: java.lang.IllegalStateException: Unable to retrieve @EnableAutoConfiguration base packages
at org.springframework.boot.autoconfigure.AutoConfigurationPackages.get(AutoConfigurationPackages.java:77) ~[spring-boot-autoconfigure-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport.getBasePackages(AbstractRepositoryConfigurationSourceSupport.java:77) ~[spring-boot-autoconfigure-1.2.5.RELEASE.jar:1.2.5.RELEASE] ...

最佳答案

要在日志中查看 Spring Boot 内容,当我从 IDEA 运行 Boot 应用程序时,我将 --debug 添加到 Program Arguments 中。从命令行来看,它看起来像:

D:\Java\jdk8\bin\java -Didea.launcher.port=7533 "-Didea.launcher.bin.path=D:\IntelliJ IDEA\bin" -Dfile.encoding=windows-1252 -classpath "[SOME CP]" com.intellij.rt.execution.application.AppMain org.springframework.integration.samples.dsl.cafe.lambda.Application --debug

这样我就可以在控制台中看到类似这样的内容:

=========================
AUTO-CONFIGURATION REPORT
=========================


Positive matches:
-----------------

AopAutoConfiguration
- @ConditionalOnClass classes found: org.springframework.context.annotation.EnableAspectJAutoProxy,org.aspectj.lang.annotation.Aspect,org.aspectj.lang.reflect.Advice (OnClassCondition)
- matched (OnPropertyCondition)

AopAutoConfiguration.JdkDynamicAutoProxyConfiguration
- matched (OnPropertyCondition)

GenericCacheConfiguration
- Automatic cache type (CacheCondition)

等等。

对于你的第二个问题,有两种方法:

  • 使用@SpringBootApplication(exclude)禁用不需要的自动配置
  • 不依赖Boot,只需导入所需的配置。例如:

    @Configuration
    @Import({PropertyPlaceholderAutoConfiguration.class, ServerPropertiesAutoConfiguration.class,
    EmbeddedServletContainerAutoConfiguration.class, DispatcherServletAutoConfiguration.class})
    @EnableIntegration
    public static class ContextConfiguration {

关于java - 禁用@EnableAutoConfiguration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32783701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com