I want @Bean to be created if application is running in embedded container. That bean should not be created if application is run on external tomcat. Is there any way we can create @Conditional annotation to create bean only if application is running in embedded tomcat.
By default, Spring Boot provides an embedded Apache Tomcat build. By default, Spring Boot configures everything for you in a way that's most natural from development to production in today's platforms, as well as in the leading platforms-as-a-service.
Yes. It's the same tomcat, but in library form. So you are responsible for configuration and other things that the standalone tomcat provides. Also you don't have to use tomcat.
An embedded Tomcat server consists of a single Java web application along with a full Tomcat server distribution, packaged together and compressed into a single JAR, WAR or ZIP file.
If you'd like to change the embedded web server to Jetty in a new Spring Boot web starter project, you'll have to: Exclude Tomcat from web starter dependency, since it is added by default. Add the Jetty dependency.
Rather than using a custom condition, you could use a Spring profile that's only enabled when you're using an embedded container. When you deploy a Spring Boot application to Tomcat its main method isn't run, making it a good place to enable the profile that you only want to be active in the embedded case.
Something like this:
@SpringBootApplication
public class So34924050Application extends SpringBootServletInitializer {
@Bean
@Profile("embedded")
public EmbeddedOnlyBean embeddedOnlyBean() {
return new EmbeddedOnlyBean();
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(So34924050Application.class);
}
public static void main(String[] args) {
new SpringApplicationBuilder(So34924050Application.class).profiles("embedded").run(args);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With