我正在尝试将 spring、非启动应用程序迁移到启动应用程序。当前构建了一个 war 文件。正在关注these说明,我正在逐步完成迁移步骤。
我发现 @SpringBootApplication 注释会强制很多事情失败。例如,当我确实需要现有的 xml 安全配置保持原样时,它会尝试自动配置安全性。我发现我可以覆盖 @EnableAutoConfiguration 并排除配置类(即 SecurityAutoConfiguration.class)。但我发现它对我的类路径上已有的项目做了很多工作。我决定最好删除 @SpringBootApplication 并将其替换为 @Configuration、@ComponentScan 和 @ImportResource 以加载我的原始上下文 xml。该类扩展了 SpringBootServletInitializer,以便我可以注册自定义 servlet 和过滤器。
我发现,它现在不再知道加载 application.yml 或 bootstrap.yml。是什么触发了这些文件的自动配置?我是否会退回到使用传统属性占位符配置程序进行加载?我想避免这种情况,因为下一步是将其连接到 spring cloud config 以集中管理应用程序配置。
最佳答案
@SpringBootApplication
是 @Configuration
、@EnableAutoConfiguration
和 @ComponentScan
的替代方案。
您可能想使用@Configuration
+ @ComponentScan
。如果你想加载xml配置,你可以使用:@ImportResource
注释。
如果你想使用自动配置,但你可以禁用一些自动配置,例如:
@EnableAutoConfiguration(排除= {DataSourceAutoConfiguration.class})
详细信息:
- http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html
- http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-configuration-classes.html
- http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html
关于没有 @SpringBootApplication 的 spring-boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33290683/