package org.stagemonitor.web.init;

import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.stagemonitor.core.instrument.StagemonitorByteBuddyTransformer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.returns;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

public class SpringBootWebPluginInitializer extends StagemonitorByteBuddyTransformer {

	@Override
	protected ElementMatcher.Junction<TypeDescription> getTypeMatcher() {
		return named("org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor");
	}

	@Override
	protected ElementMatcher.Junction<MethodDescription.InDefinedShape> getMethodElementMatcher() {
		return named("postProcessBeforeInitialization")
				.and(returns(Object.class))
				.and(takesArguments(Object.class, String.class));
	}

	@Advice.OnMethodExit(inline = false)
	public static void addInitializer(@Advice.Argument(0) Object bean) {
		if (bean instanceof ConfigurableEmbeddedServletContainer) {
			((ConfigurableEmbeddedServletContainer) bean).addInitializers(new StagemonitorServletContextInitializer());
		}
	}

	static class StagemonitorServletContextInitializer implements ServletContextInitializer {
		@Override
		public void onStartup(ServletContext servletContext) throws ServletException {
			ServletContainerInitializerUtil.registerStagemonitorServletContainerInitializers(servletContext);
		}
	}
}

@@ -453,16 +453,62 @@
 sformer;
+%0Aimport org.stagemonitor.core.util.ClassUtils;
 %0A%0Aimport
@@ -1054,16 +1054,157 @@
 %22);%0A%09%7D%0A%0A
+%09@Override%0A%09public boolean isActive() %7B%0A%09%09return ClassUtils.isPresent(%22org.springframework.boot.web.servlet.ServletContextInitializer%22);%0A%09%7D%0A%0A
 %09@Overri

Check if ServletContextInitializer is present in SpringBootWebPluginInitializer