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

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.

Related

How to add Struts2 to a web application without web.xml?

Can someone help me with a minimal project setup with Spring Boot and Struts2?
I have already create a Spring Boot application with a H2-database. I also added a h2Configuration class, so that I'm able to access the database with localhost:8080/console.
But how can I add Struts2 to my Application without web.xml?
Without web.xml you can only write a Struts2 filter using servlet 3.0 or higher
#WebFilter("/*")
public class Struts2Filter extends Struts2PrepareAndExecuteFilter {
}
The content could be empty, it's enough to add annotated filter without any inclusion in the web.xml file.
If you want to integrate Struts2 with Spring, then you should use a plugin.
Struts 2 provides a plugin that enables Spring to inject into the ActionSupport classes any dependent objects you've specified in the Spring configuration file. Consult Spring Plugin documentation for more information about how the plugin works.

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?

How can I pass certain properties to the Spring context through servlet params?

So we decided to deploy our application no more as a war on the usual tomcat, but from an embedded jetty. My app uses context:property-placeholder to resolve properties from a file. Now I'd need to pass some (or all) properties programatically before starting jetty. Is there a way that allows me to set some properties by code, before running jetty, instead of relying on the .properties file? For example as Servlet params?
You can use ServletContextPropertyPlaceholderConfigurer. This PropertyPlaceholderConfigurer extract the properties from the servlet context init params.
From Spring Javadocs:
Subclass of PropertyPlaceholderConfigurer that resolves placeholders as ServletContext init parameters (that is, web.xml context-param entries).
But this class is deprecated from Spring version 3.1.
From version 3.1 you don't need to use any special configuration because all Web Based servlet context use the class org.springframework.web.context.support.StandardServletEnvironment that resolve properties from servlet context params by default.

Resteasy and Spring integration without Spring ContextLoadListener

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.

Dealing with two different values of a property (cloud and default) in cloudfoundry and the #Value annotation

I am in reference to Spring's #Value annotation as documented here: #Value and Spring profiles.
I need to be able to have different values for a given property such as:
websiteContext=http://localhost:8080/kadjoukor
...according to whether the app is running locally or on the cloud. I am not sure how to achieve that with the #Value("${websiteContext}") annotation...
What is the best practice for dealing with such an issue?
If you are using Spring 3.1 or later, you can take advantage of bean profiles and the CloudFoundry "cloud" profile to load a different properties file depending on the environment. That might look something like this in a Spring XML configuration file:
<beans profile="default">
<context:property-placeholder location="default.properties"/>
</beans>
<beans profile="cloud">
<context:property-placeholder location="cloud.properties"/>
</beans>
Here are a few good blog posts that explain how this works in more detail:
SPRING 3.1 M1: UNIFIED PROPERTY MANAGEMENT
USING CLOUD FOUNDRY SERVICES WITH SPRING: PART 4 – SPRING PROFILES

Resources