The main idea is to override one method of the class from
I have an AwsSpringWebRuntimeInitializer class supplied by aws-serverless-java-container-springboot3. It implements ApplicationContextInitializer to register a specific bean (AwsSpringWebCustomRuntimeEventLoop) on application startup. This class is registered via the spring.factories file.
public class AwsSpringWebRuntimeInitializer implements ApplicationContextInitializer {
private static Log logger = LogFactory.getLog(AwsSpringWebRuntimeInitializer.class);
@Override
public void initialize(GenericApplicationContext context) {
logger.info("AWS Environment: " + System.getenv());
Environment environment = context.getEnvironment();
if (logger.isDebugEnabled()) {
logger.debug("AWS Environment: " + System.getenv());
}
if (context instanceof ServletWebServerApplicationContext && isCustomRuntime(environment)) {
if (context.getBeanFactory().getBeanNamesForType(AwsSpringWebCustomRuntimeEventLoop.class, false, false).length == 0) {
context.registerBean(StringUtils.uncapitalize(AwsSpringWebCustomRuntimeEventLoop.class.getSimpleName()),
SmartLifecycle.class, () -> new AwsSpringWebCustomRuntimeEventLoop((ServletWebServerApplicationContext) context));
}
}
}
private boolean isCustomRuntime(Environment environment) {
String handler = environment.getProperty("_HANDLER");
if (StringUtils.hasText(handler)) {
handler = handler.split(":")[0];
logger.info("AWS Handler: " + handler);
try {
Thread.currentThread().getContextClassLoader().loadClass(handler);
}
catch (Exception e) {
logger.debug("Will execute Lambda in Custom Runtime");
return true;
}
}
return false;
}
}
What I need to do is to change one method in AwsSpringWebCustomRuntimeEventLoop. But this class also implements SmartLifecycle, which means it will be invoked on startup.
public final class AwsSpringWebCustomRuntimeEventLoop implements SmartLifecycle
I’ve tried to exclude the whole AwsSpringWebCustomRuntimeEventLoop and replace it with my counterpart. I also tried to replace this bean by registering a new bean with the same name (using custom ApplicationContextInitializer), but it’s invoked before the AwsSpringWebRuntimeInitializer, so that bean was overridden by one created by AwsSpringWebRuntimeInitializer. However, the main problem is that the AwsSpringWebCustomRuntimeEventLoop class starts on the start of the application, so changing context in the main method after the app was up is useless.
Is there any way to replace AwsSpringWebCustomRuntimeEventLoop with my custom one, or even replace AwsSpringWebRuntimeInitializer to create the necessary bean? I would not like to copy the whole lib so I hope there is a more adequate solution.