Resteasy and Spring integration without Spring ContextLoadListener - spring

I am using Resteasy and Spring for my project. As Resteasy document said: http://docs.jboss.org/resteasy/docs/3.0.1.Final/userguide/html_single/index.html#RESTEasy_Spring_Integration.
I need to add a listener in the web.xml file:
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
However, in my project we also used a cms called magnolia, and magnolia also has an implementation for SpringContextLoaderListener. If I put both context listener. The magnolia won't be started when I run the app.
So according to the Resteasy document said:
If you do not use a Spring ContextLoaderListener to create your bean factories, then you can manually register the RESTeasy BeanFactoryPostProcessor by allocating an instance of org.jboss.resteasy.plugins.spring.SpringBeanProcessor. You can obtain instances of a ResteasyProviderFactory and Registry from the ServletContext attributes org.jboss.resteasy.spi.ResteasyProviderFactory and org.jboss.resteasy.spi.Registry. (Really the string FQN of these classes). There is also a org.jboss.resteasy.plugins.spring.SpringBeanProcessorServletAware, that will automatically inject references to the Registry and ResteasyProviderFactory from the Servlet Context. (that is, if you have used RestasyBootstrap to bootstrap Resteasy).
Does anyone knows how can I achieve that without using Resteasy ContextLoaderListener? What do I need to put in my spring applicationContext xml file?

I've also had problem with RestEasy SpringContextListener (properties placeholders ${...} weren't processed - RESTEASY-787, Spring java config didn't work etc.).
So it's enough to drop RestEasy SpringContextListener and use default org.springframework.web.context.ContextLoaderListener or whatever listener you need to. You just have to define Spring bean in your Spring XML configuration like this:
<bean class="org.jboss.resteasy.plugins.spring.SpringBeanProcessorServletAware"/>
Than RestEasy should work even without their special SpringContextListener. It works for me.

Related

Is it possible to use Spring boot with web.xml and annotation based configurataion

I want to use Spring Boot with web.xml and servlet 3.1 configuration.Is there any example?
I want to define my context(Dispatcher servlet/SpringBootServletInitializer) in Web.xml mean time define all other configuration using annotation based.Ex want to load application.properties/yml values using pojos.
Need this type of configuration to deploy the app in Liberty profile as Liberty expecting application context in web.xml when using liberety global sharelib.

what is the difference between xml files Spring and Spring MVC framework

I start to learn spring recently.
My goal is to use spring MVC to do restful api
I know spring MVC is web framework in spring
I know that in spring,there is beans.xml
And in spring MVC , there is servletname-servlet.xml
I want to know where is difference??
Is it means if I use spring MVC,I don't need to use beans.xml??
Please give me some way or give me example project link with spring and spring MVC together
The servletname-servlet.xml defines the beans for one servlet's app context. There can be number of servlets in a webapp and for every servlet we have servletname-servlet.xml (e.g. spring1-servlet.xml for servlet1, spring2-servlet.xml for servlet2).
Beans defined in servletname-servlet.xml can reference beans in beans.xml, but not vice versa.
All Spring MVC controllers must go in the servletname-servlet.xml context.
Beans.xml contain beans that are shared between all servlets in a webapp.Usually the beans.xml context is not necessary if you have only one servlet in your webapp.
You could define all your beans in servletname-servlet.xml but it's not a good practice.
Usually if you create a web application in 'pure' spring (ie. without spring MVC) then you will add ContextLoaderListener as a filter to your web.xml. Then spring will look for applicationContext.xml when you will usually import beans.xml.
In servletname-servlet.xml you define servlets. Servlets can refer other beans. So it's good practice to separate front (servlets) from backend (beans.xml).
Also remember that beans declared in servletname-servlet.xml are overriding the definitions of any beans defined with the same name in the global scope.
See also better answer at: ContextLoaderListener or not?

spring mvc applicationcontext.xml and spring servlet.xml difference

In my project, we have a spring mvc application.
It has got both applicationcontext.xml as well as -servlet.xml config files.
Bean definitons are spread in both the files.
I want to know when we have -servlet.xml wats the need for applicationcontext.xml?
Please provide any explanation in this area.
applicationContext.xml will have the bean definitions of the core spring components.
project-servlet.xml will have bean definitions of indivisual servlets.
-servlet.xml can have references to applicationContext.xml not the other way round.
What you refer as applicationContext.xml is the root application context (you put beans there when you need application-wide access to them) and what you refer as the [servlet]-context.xml is a specific Spring Bean configuration xml for Spring MVC's DispatcherServlet.
servlet-context is specific to a servlet and application context is shared for whole application. So when you define a Bean in servlet-context.xml the Bean is available to the context of that specific servlet, but when you define a Bean in application-context.xml it is available in the whole application. So if you have multiple dispatcherServlet you can have separate servlet-context for each servlet. But there is only one application-context for the application

Is it possible to use Spring #Autowired in Liferay hook?

I wonder if it is possible to have a bean form xml file injected by #Autowired somehow into Liferay hook. In this particular case I'm using UpgradeProcess hook. My project is already configured for using Spring with liferay, I can decode xml with ClassPathXmlApplicationContext's getBean("bean name") method, but it would be much nicer if this had happened automatically.
greetz!
It is possible denu.
Configure your application's web.xml listener class as org.springframework.web.context.ContextLoaderListener and place applicationContext.xml in the WEB-INF folder. It will pick the respective beans from your applicationContext.xml and you can autowire those objects in your beans.
Cheers

Servlet and Spring integration

Here is my JSF and spring integration:
I add
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
in the web.xml such that the Spring beans specified in the /WEB-INF/applicationContext.xml will be initialized and put in the ServletContext when the application starts up.
Then I get the spring beans using
FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()).getBean("XXXXBean");
It requires the FacesContext that causes it cannot be unit tested easily and cannot used outside the web container. Can I make get the spring beans from the spring context without specifying a FacesContext ?
If you are using spring-test, then take a look at this. Also you can get beans from spring context by ApplicationContext.getBean(). You can build this context in #Before method or in #BeforeClass method in your test. Contexts can be build from various sources - external xml files, classpath resources and others (read "All Known Implementing Classes" section in the javadoc above).

Resources