Spring Boot ServletRegistrationBean and Deployment to External Tomcat - spring

Recently I found the possibility to add my own servlet bean to MVC using the ServletRegistrationBean, which comes with Spring Boot. But the documentation seems to suggest that this is only for embedded servlet containers: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-servlet-containers.html
My simple question is: can the ServletRegistrationBean be used only with embedded containers or also when deploying a war to an external Tomcat?

ServletRegistrationBean will also work with external Tomcat, in fact that's exactly how Boot registers its own DispatcherServlet when you deploy a Boot app to Tomcat (or any other Servlet container).

I had similar problems when deploying spring boot war file to weblogic. The servlet is able to registered with the support of Servlet 3.x web container. but weblogic is trying to create new servlet instance instance of the servlet bean.
#Bean
#ConditionalOnMissingBean
public GraphQLServlet graphQLServlet(GraphQLSchemaProvider schemaProvider, ExecutionStrategyProvider executionStrategyProvider) {
return new SimpleGraphQLServlet(schemaProvider, executionStrategyProvider, objectMapperConfigurer, listeners, instrumentation, errorHandler, contextBuilder, graphQLRootObjectBuilder);
}
#Bean
ServletRegistrationBean graphQLServletRegistrationBean(GraphQLServlet servlet) {
return new ServletRegistrationBean(servlet, graphQLServletProperties.getServletMapping());
}
and the weblogic throws the exceptions like this:
<Oct 2, 2017 4:08:31 PM SGT> <Error> <HTTP> <BEA-101125> <[ServletContext#344074943[app:cpapps-gra3.1]] Error occurred while instantiating servlet: "simpleGraphQLServlet".
java.lang.InstantiationException: graphql.servlet.SimpleGraphQLServlet
at java.lang.Class.newInstance(Class.java:427)
at weblogic.servlet.internal.WebComponentContributor.getNewInstance(WebComponentContributo
at weblogic.servlet.internal.WebComponentContributor.getNewInstance(WebComponentContributo
at weblogic.servlet.internal.WebComponentContributor.createServletInstance(WebComponentCon
at weblogic.servlet.internal.StubSecurityHelper$ServletInitAction.newServletInstanceIfNece
Truncated. see log file for complete stacktrace
Caused By: java.lang.NoSuchMethodException: graphql.servlet.SimpleGraphQLServlet.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
at weblogic.servlet.internal.WebComponentContributor.getNewInstance(WebComponentContributo
at weblogic.servlet.internal.WebComponentContributor.getNewInstance(WebComponentContributo
at weblogic.servlet.internal.WebComponentContributor.createServletInstance(WebComponentCon
Truncated. see log file for complete stacktrace
>
It seems that the web requests directly go to the weblogic rather than spring dispatcher servlet.
But it's tricky to see that the same war file can be deployed and run in tomcat without any error.

Related

Instantiating Spring boot bean from Spring applicationContext

I am trying to autowire a bean created in spring boot jar in my spring application and when i try to run the app that bean throws error as bean not found. Now since we do not have ComponentScan in spring application, is there any way we can create a bean from spring boot app in classic spring app.

Spring Webflux: spring.main.web-application-type= reactive not working

I have a Spring Boot application that has both spring-web and spring-webflux. Now I am trying to set the web application type to reactive which is not working.
The error I get:
Caused by: java.lang.IllegalStateException: The Java/XML config for Spring MVC and Spring WebFlux cannot both be enabled, e.g. via #EnableWebMvc and #EnableWebFlux, in the same application.
at org.springframework.util.Assert.state(Assert.java:76)
at org.springframework.web.reactive.config.WebFluxConfigurationSupport.setApplicationContext(WebFluxConfigurationSupport.java:105)
Instead, if I try to set the web application type as Reactive in the below way, it does not work(the webApplicationType is set to SERVLET), though the application starts.
SpringApplication app = new SpringApplication(MyApplication.class);
app.setWebApplicationType(WebApplicationType.REACTIVE);
app.run(MyApplication.class, args);
What can I do to get this working?
Thanks
Make sure includes spring-boot-webflux-starter in your project dependencies.
Try to use SpringApplicationBuilder in your app main method to assemble your application resource.
new SpringApplicationBuilder(Application.class)
.web(WebApplicationType.REACTIVE)
.run(args)

Jetty inject Spring beans into login module

I have a Jetty server running with a realms.properties like this:
MyRealm {
com.example.MyLoginModule required
debug="false";
};
Then I have MyLoginModule which extends Jetty's AbstractLoginModule. The Jetty server creates its own Spring application context.
How can I inject beans from Jetty's Spring application context into MyLoginModule?
Jetty is version 9.4.35 and Spring is 5.3.4.
Also, this is code that I came across in our system rather than that I actually wrote. I am pretty familiar with Spring but not with Jetty.

Servlet/Filters/Listeners in Spring Boot Application

I've added Servlet/Filters/Listeners in Spring Boot application using ServletRegistrationBean , FilterRegistrationBean etc.. for that we have to declare servlets, filters as spring bean ..which will get added in Spring Application Context..which is working absolutely fine
My Question is ..whenever i will call my application, will the request FIRST be handled by Dispatcher Servlet and then to Filter?
Before spring boot, we use to register it directly in web.xml and then i think Filters used to handler the request first, then dispatcher servlet and so on and so forth..
Has the flow changed in Spring Boot Application?

How to initialize Spring container through apache tomcat

Other than through servlet do we have any other way's to initializes Spring container in Apache tomcat.
Thx,
Prikshit
Just try your favorite search engine for a spring tutorial.
There are plenty and I'm sure most of them start out without a webserver.
For example: Creating a spring context directly within code:
ApplicationContext ApplicationContext ctx = new ClassPathXmlApplicationContext ("META-INF/spring/app-context.xml");
SomeBean bean = ctx.getBean(...);
In a ServletContextListener would appear to me to be the easiest way. As a bonus, in recent versions of tomcat you can use this listener to register extra servlets that are initialised by Spring itself.

Resources