im.aop.loggers.messageinterpolation.DefaultToStringStrategyFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of im-aop-loggers Show documentation
Show all versions of im-aop-loggers Show documentation
A handy and configurable sets of annotation-based loggers for Spring Boot that can
log every execution of a method when entering or exiting normally or abnormally, without you
writing a single line of code using aspect-oriented programming (AOP)
The newest version!
package im.aop.loggers.messageinterpolation;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import jakarta.annotation.PostConstruct;
/**
* Default {@link ToStringStrategyFactory} implementation.
*
* @author Andy Lian
*/
public class DefaultToStringStrategyFactory implements ToStringStrategyFactory {
@Autowired private ObjectToStringStrategy objectToStringStrategy;
@Autowired private ObjectProvider toStringStrategiesProvider;
private List toStringStrategies = new ArrayList<>();
@PostConstruct
void postConstruct() {
toStringStrategies =
toStringStrategiesProvider.stream()
.filter(toStringStrategy -> toStringStrategy instanceof ObjectToStringStrategy == false)
.collect(Collectors.toList());
}
@Override
public ToStringStrategy findOrDefault(Object object) {
if (object == null || toStringStrategies == null || toStringStrategies.isEmpty()) {
return objectToStringStrategy;
}
return toStringStrategies.parallelStream()
.filter(toStringStrategy -> toStringStrategy.supports(object))
.findFirst()
.orElse(objectToStringStrategy);
}
}