SpringBoot - MDC filter for sl4j - spring-boot

Spring Boot - MDC filter for logging props like correlation-id, http-method with slf4j logs is not working for the been I have created manually whereas it prints the correlation id, http-method in the log that comes out of the class with spring annotation like Component, Service, etc., I really don't understand how that I get the MDC filter to recognise the #Bean instance as well. Thanks and I will appreciate your help!.

Related

How configure Spring Boot to show a custom #Endpoint in the base path '/actuator' but alphabetically?

In Spring Boot 2.4.1. For a custom #Endpoint I have the following:
#Component
#Endpoint(id="springhost")
class SpringHostEndpoint {
#ReadOperation()
HostInfo report() {
...
}
}
It works fine, the custom #Endpoint appears under the .../actuator base path how /springhost as follows:
From above it is the second link or item.
The situation is that it is not located in the right place such as all the rest of endpoints, it according alphabetically
How configure Spring Boot to accomplish that automatically?
Spring Boot does not sort the links returned by the /actuator endpoint. While in such cases it is preferable that sorting is done on the client side, I've raised an issue to see if this is something we want to consider doing in a future release.

Implementation provided for BatchConfigurer is not connsidered when using #EnableBatchProcessing(modular=true)

I am developing a sample application that Spring Batch with Spring Boot. My requirement is:
Have my own implementation of BasicBatchConfigurer so that I can configure AsyncTaskExecutor and my own dataSource as I am using SAP HANA as DB for which databaseType is not supported.
I want to use #EnableBatchProcessing(modular=true) so that I can register multiple jobs and launch them with separate Child Context
I have added all the required configurations. Without setting modular=true the Job is Launched and works as expected. It initializes the beans defined from my implementation of BasicBatchConfigurer.
However, once modular=true is set, the beans from my implementation are not initialized.
The code is hosted here: https://github.com/VKJEY/spring-framework-evaluation
I debugged further into the issue:
Looks like, When we set modular=true, BatchConfigurationSelector uses ModularBatchConfiguration
In ModularBatchConfiguration, there's a field Collection<BatchConfigurer> configurers. This has been annotated as #autowired.
I assume that this field is auto initialized if I provided a implementation
of BatchConfigurer as it has been mentioned in the comments of ModularBatchConfiguration class as well
However, While debugging I realized that the above field is still null beacuse of which, It loads DefaultBatchConfigurer and follows the default flow.
My question is why is that field configurers not being initialized in ModularBatchConfiguration? Am I missing something?
I am using Spring boot 2.1.2.
My question is why is that field configurers not being initialized in ModularBatchConfiguration? Am I missing something?
You are hitting a lifecycle issue between Spring Boot custom auto-configuration that you defined in the META-INF/spring.factories file and Spring Batch configuration.
I debugged your code and here is how to fix the issue:
remove org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.job.data.persistence.config.AsyncBatchConfigurer
from META-INF/spring.factories file. This is not needed as Spring Batch
will detect the AsyncBatchConfigurer when it is declared as a bean.
You can even remove this spring.factories file
remove #ConditionalOnMissingBean(BatchConfigurer.class) from AsyncBatchConfigurer:
Since you declared this class as a #Configuration class, it will also be defined as a bean of type BatchConfigurer and will be detected by ModularBatchConfiguration
With these two changes, the field configurers in ModularBatchConfiguration is correctly autowired with your AsyncBatchConfigurer.
As a side note, you don't need the AsyncBatchConfigurer#configurers method as Spring will do the work of injecting all BatchConfigurer beans in ModularBatchConfiguration.
Hope this helps.

Does a Spring controller returning a ListenableFuture needs #AsynchEnable in configuration?

As of spring 4.1, spring controllers accept return value that can be of type ListenableFuture. is returning a ListenableFuture return value sufficient in making the controller async? Or does it also need #enableAsync annotation somewhere in spring configuration file or/and anything else? I am following this tutorial
I found out that what i was looking for is not #enableAsync but a servlet 3.0 property called async-supported. According to this link, spring-boot defaults async-supported to true.
Hence, there is no need of any further configuration to do if you're using spring-boot.

Spring 4 Rest RequestMappingHandlerAdapter not Saving Configured MessageConverters

I am having a problem with configuring the RequestMappingHandlerAdapter; which is used in a Spring 4.1.4 Restful WebService configuration. When I configure the RequestMappingHandlerAdapter message converters, it doesn't not use the message converters that I've configured. I put break points in the RequestMappingHandlerAdapter.setMessageConverters(List<HttpMessageConverter<?>> messageConverters) method and on application startup I see this method being called three times. The first two times this method is called it has the pre-configured message converters, one of which is the Jaxb2RootElementHttpMessageConverter. On the third time, this method is called with my manually configured message converters via application-context.xml bean configuration. At this point, I am thinking that I have successfully reset the message converters with my own configuration; but that is not so because when I invoke my Restful WebService, Spring is calling the Jaxb2RootElementHttpMessageConverter instead of the MarshallingHttpMessageConverter that I manually configured via application-context.xml.
So I need to know how to:
How to tell Jaxb2RootElementHttpMessageConverter to use my configured JAXB2Marshaller; which is configured to work with JAXBIntroductions,
Unregister the Jaxb2RootElementHttpMessageConverter in Spring 4.1.4,
Tell Spring 4.1.4 when it see XML data to use MarshallingHttpMessageConverter instead of the Jaxb2RootElementHttpMessageConverter,
Create my own custom version of Jaxb2RootElementHttpMessageConverter so I can give it the correct JAXB2 Marshaller; which is configured to work with JAXBIntroductions, or
Get the RequestMappingHandlerAdapter to only used the configuration that I give it.
Any help with any of the five options above would be greatly appreciated.
Thank you.
Tonté
I too faced same issue.
You have to remove from the context file.
Its overriding the converters even if we specified list of converters.
I too faced same issue.
You have to remove mvcannotationDriven from the context file.
Its overriding the converters even if we specified list of converters.

How to configure log4j to log Spring injections

How can I configure log4j to write an entry to the log every time it injects a class?
My use case is this: I have Autowiring enabled. I have some interfaces with multiple implementing classes. I want to be able to see in the log which impl class gets injected to another class.
Any thoughts?
How can I configure log4j to write an entry to the log every time it injects a class? ... Any thoughts?
My thought is that the real problem is to get Spring or your application to generate the log events; e.g. to call the relevant methods on a Logger at the relevant point.

Resources