Jetty inject Spring beans into login module - spring

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.

Related

spring boot servlet context vs application context

I Come from years with Spring MVC, I'm trying to understand some key differences with Spring boot.
With Spring MVC I used to make a very clear distinction between application context and servlet context(s).
With Spring boot it looks like the servlet context has been pretty much deprecated and all the beans created by means of autoconfig mechanism live in the app context.
you can still create your servlet context of course, you have just to keep in mind the autoconfig is using application context.
so for example one implication of this is that #RestControllers and #Controllers live in the application context and the Spring Boot autoconfig servlet dispatchers will use any #RestController or #Controller annotated beans in the app context.
Can you help me confirm on this or make me understand what I'm missing here ?
In spring-springMVC system, there are two containers as your mentioned. For springboot-springMVC, debug in your controller and service with implementing ApplicationContextAware
they use the same global applicationContext
org.springframework.boot.web.servlet.contextAnnotationConfigServletWebServerApplicationContext

Spring Boot ServletRegistrationBean and Deployment to External Tomcat

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.

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 disable Spring Boot's autoconfiguration for Apache Velocity?

I'm experimenting with Spring Boot (1.1.9.RELEASE) and Apache Velocity (1.7) with the intention of using Velocity as a templating tool for generating emails. I'm using Thymeleaf (2.1.3.RELEASE) for web templates.
Spring Boot's Autoconfiguration detects Velocity on the classpath during start up and adds it as a web view resolver. While this is brilliant, it's not what I want so I tried
#EnableAutoConfiguration(exclude = {VelocityAutoConfiguration.class})
public class Application {
but I still ended up with a velocityViewResolver bean once the application had started up.
Any idea how I might go about disabling this automatic configuration?
Thanks in advance for any replies.
With Spring Boot 1.2.5, disabling the autoconfiguration on the main application class seems to be enough:
#SpringBootApplication
#EnableAutoConfiguration(exclude = { VelocityAutoConfiguration.class })
Edit
I don't exactly know since when that works, but now (Spring Boot 1.3.2) you can also set :
spring.velocity.enabled=false
in application.properties.

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