Java – use spring boot actuator without spring boot application

The actor Library of spring boot with production information endpoints is very useful for any server application But the problem is that I can't find a way to integrate into traditional spring applications (not spring boot applications)

There must be some way to use the endpoints of the actuator, but I can't connect them

I have a javaconfig class, as shown below

@Configuration
@ComponentScan(basePackages = { "com.company.helper","org.springframework.boot" })
@EnableWebMvc
@Import({ DbConfig.class })

public class AppConfig extends WebMvcConfigurerAdapter {

}

However, this configuration raises errors during deployment

Can no spring boot application complete this connection?

Solution

In this blog post, I added information about how to add spring boot executors to non booted applications

http://givenwhenthen.blogspot.com/2015/09/adding-spring-boot-actuator-to-non.html

In the application's build In gradle, I added the following dependencies

compile('org.springframework.boot:spring-boot-actuator:1.2.5.RELEASE'){
    exclude group: 'org.springframework.boot',module:'spring-boot-starter-logging'}

In the spring Config class of the application, I added the following:

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 an additional dependency, please refer to the blog post

UPDATE

In addition, you need to ensure that a bean is defined for the requestmappinghandleradapter. If you do not have it, the servletdispatcher will not be able to obtain the adapter for your healthmvcendpoint handler

If you don't have it, just add it to your bean configuration file

XML configuration:

<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>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>