更新时间:2022-03-21 18:09:17
我已在此博客文章中添加了有关如何在非启动应用程序中添加弹簧启动执行器的信息
I have added information on how to add spring boot actuator in a non boot application in this blog post
http://givenwhenthen.blogspot.com/2015/09/adding-spring-boot-actuator-to-non.html
在应用程序的构建中.gradle,我添加了以下依赖项
In the application's build.gradle, I added the following dependency
compile('org.springframework.boot:spring-boot-actuator:1.2.5.RELEASE'){
exclude group: 'org.springframework.boot', module:'spring-boot-starter-logging'}
在应用程序的Spring Config类中,我添加了以下内容:
In the application's Spring Config class, I added the following things:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
import org.springframework.boot.actuate.endpoint.BeansEndpoint;
import org.springframework.boot.actuate.endpoint.HealthEndpoint;
import org.springframework.boot.actuate.endpoint.InfoEndpoint;
import org.springframework.boot.actuate.endpoint.RequestMappingEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping;
import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;
import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
@Configuration
@Import(EndpointAutoConfiguration.class)
public class MyAppSpringConfig {
@Bean
@Autowired
//Define the HandlerMapping similar to RequestHandlerMapping to expose the endpoint
public EndpointHandlerMapping endpointHandlerMapping(
Collection<? extends MvcEndpoint> endpoints
){
return new EndpointHandlerMapping(endpoints);
}
@Bean
@Autowired
//define the HealthPoint endpoint
public HealthMvcEndpoint healthMvcEndpoint(HealthEndpoint delegate){
return new HealthMvcEndpoint(delegate, false);
}
@Bean
@Autowired
//define the Info endpoint
public EndpointMvcAdapter infoMvcEndPoint(InfoEndpoint delegate){
return new EndpointMvcAdapter(delegate);
}
@Bean
@Autowired
//define the beans endpoint
public EndpointMvcAdapter beansEndPoint(BeansEndpoint delegate){
return new EndpointMvcAdapter(delegate);
}
@Bean
@Autowired
//define the mappings endpoint
public EndpointMvcAdapter requestMappingEndPoint(
RequestMappingEndpoint delegate
){
return new EndpointMvcAdapter(delegate);
}
}
如果你想摆脱一个额外的依赖关系然后请参阅博文。
If you want to get rid of one additional dependency then please refer to the blogpost.
更新
此外,您还需要确保你有一个为RequestMappingHandlerAdapter定义的bean,如果你没有它,ServletDispatcher将无法为你的HealthMvcEndpoint的处理程序获取适配器。
Also you need to make sure you have a bean defined for RequestMappingHandlerAdapter, if you do not have it the ServletDispatcher will not be able to fetch the adapter for the handler of your HealthMvcEndpoint.
如果你没有它只是将它添加到你的bean配置文件中
if you dont have it just add it to your bean configuration file
xml配置:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter"/>
</list>
</property>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
<property name="prettyPrint" value="true" />
</bean>