I have service classes which are deployed in an EAR with no WAR or Web apps. In what folder structure should I ideally store the applicationContext.xml?
Currently we load it as
Resource res = new ClassPathResource("META-INF/applicationContext.xml");
That's as good a place as any. It doesn't really matter, and I'm not aware of any convention for this.
If you are using Spring in a web application, you shouldn't ever load a ClasspathXmlApplicationContext manually. Your context will be a web application context, not a classpath application context, and will be loaded automatically if you put the following in your web.xml:
Code:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>
assuming your main Spring config file is called spring-config.xml and is located in the WEB-INF folder.
Loading classpath context programmatically is always wrong for web app, you should have one context loaded by listener and (optionally) one more context loaded by DispatcherServlet (if you use Spring web mvc as your mvc framework). I suggest you start by reading some documentation and try the Petclinic example to gain some insight in how to correctly use Spring in a web application.
Also, your ear packaging strategy is wrong; your Spring configuration is webapp specific so you should put your Spring config files inside the WAR, under the root of the web app (probably the best place is somewhere under WEB-INF, alongside web.xml and other descriptors). If you have resources like classes used by both ejb and webapp, you should package those classes inside a jar, put this jar under the lib directory in your ear and add a Class-Path entry for the jar in the MANIFEST.MF of webapp & each of the ejb jars. If you have config files with environment-dependant properties (like jdbc config and disk paths) you shouldn't package them inside your ear but instead let the application read it externally from the file system (PropertyPlaceholderConfigurer is your best shot).
For starters, I suggest you use Eclipse ear creation function to see how your web app should be packaged before you start using ant to build your project.
I also suggest you consider Maven as your compile - build - package strategy since this will automate and standardize the project and the web app maven archetype will let you create a correct pattern for j2ee web app in a blink.
Related
I want to start with the most simple Maven Project with a simple REST resource not using Spring Boot and generate a .war artifact that is deployed in a servlet container. I am using Eclipse IDE. So I would like to know what is the basic things in place needed to create such a Project.
I think I need at least this dependences:
Spring-core, Spring-mvc, Spring-web, Spring-context,
I also need the stuff with a class annotated with the #RestController annotation, with some method annotated with the #Requestmapping and so.
But whats the minimum content I should have in the WebContent directory and its subfolders META-INF, WEB-INF ... in order to the servlet container to know how to use the .war component? I dont want any HTML nor JSP pages.
In your WEB-INF folder, you will need a web.xml file. This is where you will configure your dispatcher servlet. This is the part of your application that receives requests and delegates them to the appropriate part of your application.
You will also need some sort of REST configuration file. You can define beans for Spring and component scan config.
A good explanation of this can be found here, https://www.programming-free.com/2014/01/spring-mvc-40-restful-web-services.html
I am not able to use web.xml with spring boot in war deployment.
I am aware that there are ways of converting web.xml using #Configuration, but I must stick with web.xml for now. I was following the official document, here: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html#howto-create-a-deployable-war-file-for-older-containers
I was able to deploy my application in WAR file, but it was not using web.xml.
Maybe I am not following the step 85.2 correctly, as I am not sure what it means by "load an applicationContext via a DispatcherServlet":
85.2 Create a deployable war file for older servlet containers
Older Servlet containers don’t have support for the
ServletContextInitializer bootstrap process used in Servlet 3.0. You
can still use Spring and Spring Boot in these containers but you are
going to need to add a web.xml to your application and configure it to
load an ApplicationContext via a DispatcherServlet.
Can you guide me or point me to any examples or working sample project using web.xml in spring boot? I am using weblogic server.
I just started to learn Spring and wrote a Hello Word program for SpringMVC and this error stops me
Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
java.lang.NoClassDefFoundError: javax/servlet/ServletContextListener
I have all the necessary jar files inside lib directory.I am using spring 3.2,tomcat server and eclipse Indigo IDE.
In web.xml I declared dispatcher servlet (named dispatcher) and also, corresponding dispatcher-servlet.xml under web-inf.
Also in web.xml declared listener and under contextConfigLocation /WEB-INF/dispatcher-servlet.xml.
I searched google but not been able to found any answer.During my search I found something about maven pom.xml (don't know what is that)but not been able to understand what exactly it is?Do I need maven for springmvc?
What will be the solution for this problem?
In which lib directory do you put the JAR files?
If it is inside the WEB-INF directory, it is supposed to be loaded. Otherwise, the JAR files should be imported in the WEB Deployment Assembly as well.
If you would need a step-by-step tutorial at the beginning, you can try to follow Hello World Example Using Spring Framework MVC 3.1
I'm looking for some kind of "best practice" informations about Spring jar configuration. I have a web project (war) and I need connect some jar libraries - my jars which contains additional functions. These jars contains Spring services. But when I connect jar, the service class did not work, because Spring don't know about that. So I need to tell Spring about this by “package auto scan” configuration inside my jar.
The final solution must be war project (main functions) and some additional jars which contains other functions. When I add jar into war project, I don't want to change configuration in applicationContext.xml (in war). I want minimal dependency to war project. I was thinking, when if I place applicationContext.xml to META-INF folder in jar it will be auto loaded by Spring, but it is not.
Do you know how can i solve this? May be some kind of “after startup dynamic configuration” :-). thanx
If you are trying to load annotated beans from the jars into your war's Spring context, you can set up a component scan in the war's context xml file to scan the packages in the jars.
If you are trying to load beans defined in XML files from the jars, you can include them using something like this in your war's Spring context xml file:
<import resource="classpath:path/to/config/in/jar/beans-from-jar.xml"/>
You shouldn't need to have your jar know anything about your war this way. You just scan the annotated beans and/or import the config from the jar.
I'm new to Spring and try to setup a project which is split into 3 submodules. As build tool I'm using maven. My problem is, that I don't know where to add Springs "magic".
My 3 submodules are "ORM" (holds all the hibernate staff to access the database) "BusinessLogic" (which should hold the complete logic) and "WebApp" (adds as the only "client" to the app / logic).
I want to use SpringMVC for the WebApp which seems to be no problem. As "BusinessLogic" should hold the complete logic I thought of adding the Spring related stuff (Bean definition / DI) in that module. But then I don't know how to setup Spring when accessing the module form the webapp.
The hole project is being ported from a JavaEE / JBoss app where "ORM" and "BusinessLogic" (implemented as EJBs) where put into one .ear archive and the webapp into a seperate one (.war). JNDI was used to access the beans from the webapp, but I completely want to decouple the application from JBoss and deploy it on a Tomcat webserver.
At the moment I've created all three modules as separate Maven projects ("ORM" and "BusinessLogic" as .jar, "WebApp" as .war packaging), linked by a "parent" project.
Thanks for any hints on project setup :).
Greetings
Ben
you could configure spring context in your web.xml and you can perform import of Spring sub-modules context. You can add import's configuration of sub-modules in your webApp application context.