ContextLoader.getCurrentWebApplicationContext() always returns null - spring

i'm running a web-app with spring-boot and have to use external articfacts which are using ContextLoader.getCurrentWebApplicationContext() to get some beans from.
But it always returns null.
According to the spring-bootdocumentation the parent application context is not a WebApplicationContext. Is there any other way to access the WebApplicationContext via ContextLoader.getCurrentWebApplicationContext()?
Thanks in advance!

ContextLoader isn't really applicable to Spring Boot applications (or any with Servlet 3.0 dynamic initialization). Spring Boot does set up the ServletContext so that WebApplicationContextUtils can locate the web context. Your external artifacts really ought not to be using ContextLoader static methods since it makes assumptions about the way the app is deployed. Can you change them? Or work out another way to set their state?

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

How many containers in spring MVC?

I am new to spring. I wanted to know two things :
Does the Dispatcher Servlet and ApplicatonContext exists in two different containers (in terms of spring).
Also, if they are part of same container, then how is it possible that spring is creating two beans if I scan the classes in both of their config files?
Will be very thankful for any help.
SpringMVC has one Container i.e WebApplicationContext(Interface)
A Context represents your environment. It represents the state surrounding where you are in your system. For example, in web programming in Java, you have a Request, and a Response. These are passed to the service method of a Servlet. A property of the Servlet is the ServletConfig, and within that is a ServletContext. The ServletContext is used to tell the servlet about the Container that the Servlet is within. So, the ServletContext represents the servlets environment within its container. Similarly, in Java EE, you have EBJContexts that elements (like session beans) can access to work with their containers.(copy/paste from qoura)

using/importing/injecting spring-managed class into non-spring class

Is there a way to inject/import spring-managed class into a legacy code or non-spring class?
I am working on a spring project, but we have this legacy code that needs access to a spring managed class (specifically a jparepsository implementation class). Is this possible?
Sorry, I am a newbie in the Spring framework. Any help/advice will be greatly appreciated. Thanks :)
You can always load an application context up in your code. It's a simple matter of doing something like this:
ApplicationContext context = new ClassPathXmlApplicationContext("path/to/applicationContext.xml");
You'll have access to all the beans created there via the getBean() function. This may be sufficient for you. However, you won't be accessing the same context as other parts of your application, you'll have a copy. So if you have stateful beans in your application context the "unmanaged" code won't see the same state.
if you're talking about a web application, then you can use the WebApplicationContextUtils to access the same context as the rest of the web applicatiom.
WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
Since you have access to the legacy code, what prevents you from rewriting it to be spring managed, or at leas to be ApplicationContextAware?

How can I access Spring bean from Message-driven bean in JBoss AS 7

I want to make a call to a Spring bean (a #Component) from my message-driven bean (MDB) but have problems getting a reference to it. I've tried with a class implementing org.springframework.context.ApplicationContextAware which stores the Spring ApplicationContext in a static field in a class MyAppContext. The static field in MyAppContext is then accessed from the MDB. But MyAppContext is loaded from different classloaders. The Spring application context is correctly set in the web module classloader context, but in the MDB's classloader context, it's null.
Can I somehow instruct JBoss to use the same classloader for the web app and the MDB?
Or is there a better way than storing the Spring application context in a static field?
Thanks for any advice!
A static holder for the context is not really a good idea. To make your beans available to other applications in a Java EE environment, you should consider making use of JNDI.
Unfortunately, there is no plain JNDI exporter available out of the box, but it's fairly easy to write one yourself, as shown in this blog post: http://maestro-lab.blogspot.ro/2009/01/how-to-export-spring-managed-bean-to.html
There is however a JndiRmiServiceExporter that you may want to look at.
Once your beans are bound to names in JNDI, they can be referenced using standard CDI in your message bean without worrying about class loading issues.
Why not use "ClassPathXmlApplicationContext" to load and look up for the Spring bean you require in your MBean?

Application context and spring context is same?

I am new to spring MVC,and just started working on it.I would like to know about the application context(web-applicationcontext) and the context provided by the spring i.e spring-servlet.xml whether it is same or different.
Hopefully somebody could give me the greater idea to come over this confusion.
The difference between the application context and servlet context is that whatever is specified in the application context can be referenced in the servlet context, but not vice-versa.
That's to say that you can have components that are reused through your servlets specified at the application context level, but certain things that are only specific to a certain servlet can be specified there to isolate them from the application and other servlets.
That's there if you have a need for fine-grained control.
You can treat them as being the same file. Look at this answer for more details.
The application-context provided by *-servlet.xml is the WebApplicationContext see here for more info on this. The root application is created by the contextLoader listener .
Quoting from the Spring reference,
"In the web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext.
The WebApplicationContext is an extension of the plain ApplicationContext that has some extra features necessary for web applications. It differs from a normal ApplicationContext in that it is capable of resolving themes (see Section 15.7, “Using themes”), and that it knows which servlet it is associated with (by having a link to the ServletContext). The WebApplicationContext is bound in the ServletContext, and by using static methods on the RequestContextUtils class you can always look up the WebApplicationContext if you need access to it."
You can find the details of the root Application context here

Resources