我正在使用帶有 Java 17 的 Spring Boot 2.7.0(但我也嘗試過 2.6.7 和 2.5),并且我想將執行器添加到專案中。該專案被打包為一個戰爭,因為它仍然使用 JSP,當我們將它打包為 jar 時,我們還沒有開始作業。由于兼容性問題,它使用了一個非常舊版本的 spring-security(4.2.18.RELEASE,仍然使用 XML 配置),但除此之外,依賴項應該是最新的。在其他專案中,我從來沒有遇到過執行器的任何問題。無論如何,我已將此添加到pom.xml檔案中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
具有這些屬性:
management.server.port=8081
management.endpoint.health.enabled=true
management.endpoint.health.show-details=always
management.endpoint.health.status.http-mapping.DOWN=200
management.endpoints.web.exposure.include=*
我們已將此設定添加到我們的 spring security XML 配置中,以允許對執行器的所有呼叫:
<http pattern="/actuator/**" auto-config="true" use-expressions="true" create-session="stateless" disable-url-rewriting="true" security="none"/>
我們有(很多)自定義配置,但我能想到的相關部分是這樣定義的:
@EnableRabbit
@EnableRetry
@SpringBootApplication
@EnableAsync
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ComponentScan({"com.company.something", "com.company.another"})
@ImportResource(locations = {"classpath*:META-INF/spring/applicationContext*.xml", "classpath:spring/webflow-config.xml"})
public class Bootstrap {
public static void main(String[] args) {
SpringApplication.run(Bootstrap.class, args);
}
}
和
@Configuration
public class WebappConfiguration implements WebMvcConfigurer {
@Bean
public TomcatServletWebServerFactory tomcatFactory() {
return new CustomTomcatServletWebServerFactory();
}
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> servletContainerCustomizer() {
return container -> {
container.addContextCustomizers(ctx -> ctx.setReloadable(false));
container.addConnectorCustomizers(con -> con.setMaxPostSize(5000000));
};
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/web-resources/**").addResourceLocations("classpath:/META-INF/web-resources/");
}
@Bean
public DispatcherServletRegistrationBean dispatcherServletRegistrationBean() {
DispatcherServlet servlet = new DispatcherServlet();
servlet.setApplicationContext(new AnnotationConfigWebApplicationContext());
return new DispatcherServletRegistrationBean(servlet, "/");
}
private static class CustomTomcatServletWebServerFactory extends TomcatServletWebServerFactory {
@Override
protected void postProcessContext(Context context) {
((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
context.setResources(new ExtractingRoot());
}
}
}
當我運行應用程式時,我收到此錯誤:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 2 of method servletEndpointRegistrar in org.springframework.boot.actuate.autoconfigure.endpoint.web.ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration required a single bean, but 2 were found:
- dispatcherServletRegistration: a programmatically registered singleton - dispatcherServletRegistrationBean: defined by method 'dispatcherServletRegistrationBean' in class path resource [com/company/WebappConfiguration.class]
這是我覺得很奇怪的第一件事。據我了解,Spring 似乎檢測到了兩個servletEndpointRegistrarbean。如果我嘗試洗掉dispatcherServletRegistrationBean我遇到此錯誤:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method errorPageCustomizer in org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' that could not be found.
如果我改為將@Primary注釋添加到dispatcherServletRegistrationBean:
@Primary
@Bean
public DispatcherServletRegistrationBean dispatcherServletRegistrationBean() {
DispatcherServlet servlet = new DispatcherServlet();
servlet.setApplicationContext(new AnnotationConfigWebApplicationContext());
return new DispatcherServletRegistrationBean(servlet, "/");
}
該應用程式確實啟動了。但是,如果我導航到http://localhost:8081/actuator所有我得到的是:
<Map>
<timestamp>2022-05-24T06:38:58.914 00:00</timestamp>
<status>404</status>
<error>Not Found</error>
<message>No message available</message>
<path>/actuator</path>
</Map>
這可能是什么原因,我該如何解決?
uj5u.com熱心網友回復:
我設法通過創建DispatchServlet一個單獨的 bean來解決這個問題DispatcherServletRegistrationBean:
@Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServlet dispatcherServlet() {
DispatcherServlet servlet = new DispatcherServlet();
servlet.setApplicationContext(new AnnotationConfigWebApplicationContext());
return servlet;
}
@Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet) {
return new DispatcherServletRegistrationBean(dispatcherServlet, "/");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/485913.html
上一篇:我沒有在我的Android應用中收到Firebase云訊息
下一篇:ES存在查詢
