I'm trying to deploy a war file with an embedded Jetty. You can clone a minimal working example here: https://github.com/guilty/jetty-test
When you build the project and try to run the war-project with a jetty-runner (which can be downloaded here: http://mvnrepository.com/artifact/org.eclipse.jetty/jetty-runner/9.2.10.v20150310), everything works fine.
As you can see, I have a jar-project, which has a dependency to war-project (with classes classifier). It tries to start an embedded Jetty server and deploy the war-project, but problem is, it reads the application-context.xml twice. As I understand, it finds it twice in the classpath: once in a built *.war, and once in a dependency I made with classes classifier.
The same happens if you use jetty-runner and call Runner.main(new String[] { "path-to.war" }), though as I said before, it works fine when called from command line.
So my question, I guess, is how to restrict classpath to Jetty, if that's at all possible.
As I mentioned, this is a minimal working example, not a real-world application. If I can be of any assistance -- please let me know.
Edit: I've been asked to put relevant code here, because links may rot.
Since I won't post all the code, I'm going to describe the situation. I have a *.war project, which is a standard web application powered by spring. It has a class, which throws an exception if more than one instance of it is created by spring (which should never happen, since it's scope is singleton).
Another project references the war-project with a classes classifier (i.e. gets everything that lays in src/main/java and src/main/resources) and tries to deploy the war-file produced by war-project. Here is the relevant deployment code:
Server server = new Server(8080);
WebAppContext webAppContext = new WebAppContext(args[0], "/");
server.setHandler(webAppContext);
server.start();
This code runs, but the server fails to start, because a second instance of the bean I wrote about before gets created and it throws an exception.
web.xml of the war-project:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:application-context.xml</param-value>
</context-param>
</web-app>
application-context.xml of the war-project contains only one bean, with default (singleton) scope and nothing else.
Here's the relevant pom.xml part of jar-project:
<dependency>
<groupId>com.github.guilty.jettyTest</groupId>
<artifactId>web-project</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>classes</classifier>
</dependency>
Related
I want to use springboot with security and primefaces.
I have followed this article but with one little change, I don't want to use JoinFaces. Everything works fine except the spring sec taglib.
xmlns:sec="http://www.springframework.org/security/tags
Now I'm not sure how to configure servletContext so that the sec tags would work.
Here is GitHub repo for now its really simple project just to test the combination SB + SS + PF.
I had the same scenario before and I ended up adding springsecurity.taglib.xml in my project. You can copy the file content from here and then register it in your web.xml file as follow:
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/springsecurity.taglib.xml</param-value>
</context-param>
Saw same issues in others queries, but did not find the answer, hence posting it afresh.
Created a sample Helloworld service in Rest and deployed it in Tomcat-Jersey. Not using Maven.
Injecting a spring bean define in applicationContext.xml which is present in WEB-INF directory.
Deployed the service in Tomcat within Eclipse.
But when I execute the rest service, cannot find file applicationContext.xml.
Once I copy the file to WEB-INF/classes only, it works.
What is the way to make it work by keeping it only in WEB-INF?
web.xml :
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
Rest service code snippet :
ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
It seems reasonable to work when you drop the context descriptor under /WEB-INF/classes because this location is the root of classpath.
So try loading with below uri (without prefix) if you want to drop your applicationContext.xml under WEB-INF folder:
ctx = new ClassPathXmlApplicationContext("/WEB-INF/applicationContext.xml");
Otherwise, can you explain why are you trying to load your applicationContext.xml within your web application?
Alternatively, you can load your application context programatically as follows:
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
BR.
If you are using Intellj IDEA and having problem with ApplicationContext.xml path;
Go to File and click Project Structure. Under the Project Settings, click Facets, at the Web Section you will see Web Resource Directories. You should define Web Resource Directory to your /WEB-INF folder. If your /WEB-INF folder is in the subpath like below;
/web/WEB-INF/...
Then choose /web directory to cover all your files.
I've created a jar file through spring roo (maven project - persistence archive) unit tests are running fine, the concerned files are on the following location
jarFile/META-INF/persistence.xml
jarFile/META-INF/applicationContext.xml
jarFile/META-INF/applicationContext-jpa.xml
jarFile/META-INF/database.properties
Unit tests are running fine.
Because its a maven project I added it to local repository by executing the command "mvn install" and after that I added it as a dependency to another maven based web-application.
I am running the web application using mvn jetty:run command. the concerned files in web application are.
webApp/WEB-INF/web.xml
webApp/WEB-INF/applicationContext.xml
The Problem
* Its loading the webapp/WEB-INF/applicationContext.xml but how can I verify its loading the child jarFile/META-INF/applicationContext.xml or not? actually when i try to access the service class methods from persistence archive the entityManager is NULL.
* If i try to put contextConfigLocation directive (tried various options) within web.xml, Its not even loading the webapp/WEB-INF/applicationContext.xml.
What I want
Use the service methods (which uses entitymanager) from persistence archive from within my web application.
Thanks in advance.
Found the answer by digging around a bit.
Actually I was confused with various ways/syntax to include the context file, was trying with all sort of classpath*:xxx syntax but actually the WEB-INF is not on the class path so following have to be added to web.xml to load the main webApp/WEB-INF/applicationContext.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
Then had to add the following to the webApp/WEB-INF/applicationContext.xml
<import resource="classpath*:META-INF/spring/applicationContext*.xml" />
Now the webapplication is loading the context file from jar file as well. And things are working.
I have to load 3 spring config xml files in myproj spring mvc app named myproj-controllers.xml, myproj-services.xml and myproj-dao.xml. I have two options to load them.
Firstly Use import resources in myproj-servlet.xml
<import resource="myproj-controllers.xml"/>
<import resource="myproj-services.xml"/>
<import resource="myproj-dao.xml"/>
or secondly in the web.xml using context param like this
<context-param>
<param-name>contextConfigLocation</param-name>
<param-values>/WEB-INF/myproj-controllers.xml</param-values>
<param-values>/WEB-INF/myproj-services.xml</param-values>
<param-values>/WEB-INF/myproj-dao.xml</param-values>
</context-param>
and adding ContextLoader listener
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Which approach is recommend? And why? In my opinion I find import approach easier as we only need to make changes to myproj-servlet.xml instead of web.xml.
Spring lets you declare multiple contexts in a parent-child relation so I always went for one root applicationContext.xml containing my application beans (services, DAOs etc) and one action-servlet.xml for servlet contexts (request mappings, view resolvers etc).
I once needed action-servlet-2.xml file but still had just one root applicationContext.xml for both servlet contexts.
So (for me) it was always parent context + child context.
The only need for splitting the files into more pieces was just to reduce the size of the XMLs (which is what <import> does best).
For me, the contextConfigLocation param refers to application context files being loaded together into a single application context instance. But your files (myproj-controllers.xml, myproj-services.xml, myproj-dao.xml) seem like parts of one application context file.
For this reason I would personally go for the <import> statements and have just one value (for the root application context) in the contextConfigLocation param.
I prefer the context loader listener approach, but perhaps that's because I've never considered the import method. I'll try it out. Thanks.
I don't see any performance advantages. The WAR file has to be redeployed in either case. It's modifying one file as opposed to another. I don't see any difference. It has a bike shed feel to me, but I could be wrong.
I just filed a bug in the Spring bugsystem ( https://jira.springsource.org/browse/SPR-8551 ), but I am still unsure if I am missing something
I tracked down a problem with <context:component-scan/> to this statement.
Given the two following classes which are in the same JAR in WEB-INF/lib of a web application (The JAR file has the directory structure):
test/TheBean.java:
package test;
#Component
public class TheBean{
}
test/BeanSearcher.java:
package test;
public class BeanSearcher{
public void init(){
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.scan("test");
ctx.refresh();
TheBean b= ctx.getBean(TheBean.class);
// What is the value of b?
}
}
If I run new BeanSearcher().init() in a jUnit test case or other type of standalone application, b is getting assigned an instance of TheBean, but if I run it, say, in a JSP, ctx.getBean() is returning null.
So, am I doing something wrong or not taking something into account, is this just a bug...?
EDIT 8/8/2011: It seems to work good as I tried to simplify the problem, but still, when I try to make it work, in the initialization of OpenCms, it fails. Now I am trying to look for the differences between working versions and the one which doesn't work. (Classloader, ubication of the relevant classes in different JARs or directly in WEB-INF/classes, calls via reflection, etc.)
As I wrote in the comment, the solution is given by the answer here:
Spring Annotation-based controllers not working if it is inside jar file
When you export the jar file using the export utility in eclipse there
is a option called Add directory entries.
The obvious question is whether you have things like these in your web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/foo.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Without these, Spring won't actually load at all, let alone properly build beans…