You can attach your ElasticApmAttacher.attach() in the Spring Application main class
For a SpringBootApplication packaged as a war file, and deployed to Tomcat server, this can be added to the configure method
Below code might help:
package com.test.main
import co.elastic.apm.attach.ElasticApmAttacher
import org.springframework.boot.Banner
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.builder.SpringApplicationBuilder
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer
@SpringBootApplication(scanBasePackages = [ "com.test" ])
class Application extends SpringBootServletInitializer{
static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class)
app.setBannerMode(Banner.Mode.OFF)
ElasticApmAttacher.attach();
app.run(args)
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
ElasticApmAttacher.attach();
return application.sources(Application.class)
}
}