在 spring 应用程序中打包到 war 时未调用 bean 创建函数
bean creator function not called when packaging into war in spring application
我有一个 spring REST 应用程序,如果 运行 使用带有嵌入式 tomcat 的 mvn exec,它可以正常工作(bean 可以很好地创建,@Autowired 按预期工作,等等) .
但是,如果我使用 mvn 包创建 WAR 并将其部署到现有的 tomcat,则不会调用某些 bean 创建器函数。
比如我有一个bean creator函数
@Bean
@Autowired
public ObjectMapper objectMapper(LocalizedMessages messages) {
// create and configure ObjectMapper with custom serializer
}
使用嵌入式 tomcat 时调用,使用 war 时不调用。我试图将包含 class 添加到 SpringApplicationBuilder 的源代码中,但这没有任何效果。
有人建议我如何让 war 版本正常工作吗?
一些额外的细节
我正在使用 spring-boot 1.2 和 spring-boot-starter-web maven 依赖项,最初我使用的是 App
class main
函数来创建和启动一个新的 SpringApplication(它又启动了一个嵌入式 tomcat),而且 App
class 有适当的 @ComponentScan
和 @EnableAutoConfiguration
注释,以便所有 @Component
和 @Configuration
都得到适当处理。
使用 war 打包,我创建了一个 class 扩展 SpringBootServletInitializer
并且有一个 @SpringBootApplication
注释将上面的 App.class
添加到 SpringApplicationBuilder 的源列表(参见 )。我什至试图在来源中列出 all @Component
s,但仍然没有调用上述 bean creator 函数。
如果有人感兴趣,我的一个同事找到了解决方案:
似乎 App
class 必须扩展 SpringBootServerInitializer
(而不是另一个 class),并在 SpringApplicationBuilder 的源中定义自己。之后,@ComponentScan
就开始神奇地工作了。
希望对大家有所帮助。 :)
我有一个 spring REST 应用程序,如果 运行 使用带有嵌入式 tomcat 的 mvn exec,它可以正常工作(bean 可以很好地创建,@Autowired 按预期工作,等等) .
但是,如果我使用 mvn 包创建 WAR 并将其部署到现有的 tomcat,则不会调用某些 bean 创建器函数。
比如我有一个bean creator函数
@Bean
@Autowired
public ObjectMapper objectMapper(LocalizedMessages messages) {
// create and configure ObjectMapper with custom serializer
}
使用嵌入式 tomcat 时调用,使用 war 时不调用。我试图将包含 class 添加到 SpringApplicationBuilder 的源代码中,但这没有任何效果。
有人建议我如何让 war 版本正常工作吗?
一些额外的细节
我正在使用 spring-boot 1.2 和 spring-boot-starter-web maven 依赖项,最初我使用的是 App
class main
函数来创建和启动一个新的 SpringApplication(它又启动了一个嵌入式 tomcat),而且 App
class 有适当的 @ComponentScan
和 @EnableAutoConfiguration
注释,以便所有 @Component
和 @Configuration
都得到适当处理。
使用 war 打包,我创建了一个 class 扩展 SpringBootServletInitializer
并且有一个 @SpringBootApplication
注释将上面的 App.class
添加到 SpringApplicationBuilder 的源列表(参见 @Component
s,但仍然没有调用上述 bean creator 函数。
如果有人感兴趣,我的一个同事找到了解决方案:
似乎 App
class 必须扩展 SpringBootServerInitializer
(而不是另一个 class),并在 SpringApplicationBuilder 的源中定义自己。之后,@ComponentScan
就开始神奇地工作了。
希望对大家有所帮助。 :)