Split Application Context File in Spring - spring

I would like to have step-by-step information on :
how to split the ApplicationContext file (eg.: myapp-servlet.xml) into multiple XML files in Spring with some examples ?
I have tried configuring web.xml with "ContextLoaderListener" and have contextConfigLocation like :
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value> /WEB-INF/business-services.xml </param-value>
</init-param>
but it is creating problems.
Please give me in-detail explaination on how to do this.
Thanks in advance !

What I like to do, if I have multiple context files, is to have my base context class import the other pieces via the import tag.
<import resource="applicationContext-otherStuff.xml"/>
We typically use this model, to keep out datasource configuration separate from the bean instantiations.

e.g. with:
<param-value>classpath*:spring/persistence/*.xml, classpath*:spring/*.xml</param-value>
the paths depend on your locations of the splitted .xml
Example with WEB-INF Directories
<param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
sidenote: seems to work without ','
Reference:
spring doc chapter: 3.8.5. Convenient ApplicationContext instantiation for web applications

Related

com.sun.jersey.api.container.ContainerException : The ResourceConfig instance does not contain any root resource classes [duplicate]

What's going wrong here?
The ResourceConfig instance does not contain any root resource classes.
Dec 10, 2010 10:21:24 AM com.sun.jersey.spi.spring.container.servlet.SpringServlet initiate
SEVERE: Exception occurred when intialization
com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
at com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:103)
at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1182)
at com.sun.jersey.server.impl.application.WebApplicationImpl.access$600(WebApplicationImpl.java:161)
at com.sun.jersey.server.impl.application.WebApplicationImpl$12.f(WebApplicationImpl.java:698)
at com.sun.jersey.server.impl.application.WebApplicationImpl$12.f(WebApplicationImpl.java:695)
at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:197)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:695)
at com.sun.jersey.spi.spring.container.servlet.SpringServlet.initiate(SpringServlet.java:117)
Filter:
<filter>
<filter-name>JerseyFilter</filter-name>
<filter-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</filter-class>
<init-param>
<param-name>com.sun.jersey.config.feature.Redirect</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
<param-value>/views/</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
<param-value>/(images|css|jsp)/.*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>JerseyFilter</filter-name>
<url-pattern>/myresource/*</url-pattern>
</filter-mapping>
Code:
#Path ("/admin")
public class AdminUiResource {
#GET
#Produces ("text/html")
#Path ("/singup")
public Viewable getSignUp () {
return new Viewable("/public/signup", "Test");
}
}
Have you tried adding
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>my.package.name</param-value>
</init-param>
to your SpringServlet definition? Obviously replace my.package.name with the package that AdminUiResource is in and make sure it is in the classpath.
I am new to Jersey - I had the same issue, But when I removed the "/" and just used the #path("admin") it worked.
#Path("admin")
public class AdminUiResource { ... }
YOU NEED TO ADD YOUR PACKAGE NAME AT
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>your.package.name</param-value>
</init-param>
ALSO ONE SILLY THING I HAVE NOTICED,
I Need to refresh my project after MAVEN BUILD else it show me same error.Please comment If you know reason why we need to refresh project?
This means, it couldn't find any class which can be executed as jersey RESTful web service.
Check:
Whether 'com.sun.jersey.config.property.packages' is missing in your
web.xml.
Whether value for 'com.sun.jersey.config.property.packages'
param is missing or invalid (the mentioned package doesn't exists). It should be a package where you have put your POJO classes which runs as jersey services.
Whether there exists at least one POJO class, which has a method annotated with #Path attribute.
Your resource package should contain at least one pojo which is either annotated with #Path or have at least one method annotated with #Path or a request method designator, such as #GET, #PUT, #POST, or #DELETE. Resource methods are methods of a resource class annotated with a request method designator. This resolved my issue...
I ran across this problem with JBOSS EAP 6.1. I was able to deploy my code through eclipse to the JBOSS server but once I attempted to deploy the file as a WAR file to JBOSS I started getting this error.
The solution was configuring the web.xml to work properly with JBOSS by allowing the two to work together.
The following two lines were commented out in web.xml to allow JBOSS to do it's own configurations
<!--
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.your.package</param-value>
</init-param> -->
And then add the following context params after
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.resources</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.providers</param-name>
<param-value>false</param-value>
</context-param>
Basically I corrected it like below and everything worked fine.
<servlet>
<servlet-name >MyWebApplication</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.feature.Redirect</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
<param-value>/views/</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
<param-value>/(images|css|jsp)/.*</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyWebApplication</servlet-name>
<url-pattern>/myapp/*</url-pattern>
</servlet-mapping>
I am getting this exception, because of a missing ResourseConfig in Web.xml.
Add:
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>/* Name of Package where your service class exists */</param-value>
</init-param>
Service class means: class which contains services like: #Path("/orders")
I had the same issue with trying to run the webapp from an eclipse project. As soon I copied the .class files to /WEB-INF/classes it worked perfectly.
I had the same issue, testing a bunch of different examples, and tried all the possible solutions. What finally got it working for me was when I added a #Path("") over the class line, I had left that out.
Had the same issue and found out it was a problem with the way I deployed my source code. As the error message says: "...does not contain any root resource classes". So it couldn't find any resource classes in the configured package. I just deployed the classes wrong - that's why it didn't pick it up.
I forgot to deploy my class files in the /WEB-INF/classes directory of the WAR - initially I just had it directly in the root of the WAR file. So when it looked for resource classes it didn't find them - because they existed in a different (wrong) location.
Same issue - web.xml looked like this:
<servlet>
<servlet-name>JerseyServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.mystuff.web.JerseyApplication</param-value>
</init-param>
...
Providing a custom application overrides any XML configured auto detection of classes. You need to implement the right methods to write your own code to wire up the classes. See the javadocs.
Another possible cause of this error is that you have forgotten to add the libraries that are already in the /WEBINF/lib folder to the build path (e.g. when importing a .war-file and not checking the libraries when asked in the wizard). Just happened to me.
It happened to me when I deployed my main.jar, without checking the add directory entries box in the export jar menu in Eclipse.
Well, it's a little late to reply. I have faced the same problem and my Google searches were in vain. However, I managed to find what the problem was. There might be many reasons for getting this error but I got the error due to the following and I wanted to share this with my fellow developers.
I previously used Jersey 1.3 and I was getting this error. But when I upgraded the jars to the latest version of Jersey, this issue was resolved.
Another instance in which I got this error was when I was trying to deploy my service into JBoss by building a war file. I made the mistake of including the Java files in the .war instead of java classes.
I had to add a trailing forward slash to the end of #path
#Path ("/admin/")
Ok... For me work fine just only assigning the "servlet-class" to com.sum.jersey.spi.container.servlet.ServletContainer, I am using IDE (Eclipse Mars)
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/frontend/*</url-pattern>
</servlet-mapping>
but for some reason I had to reboot my computer in order to work in my localhost. If still not work? You have to add in your web.xml this code in between "servlet" tag.
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>the.package.name</param-value>
</init-param>
"the.package.name" is the package name where you have your classes. If you are using IDE, refresh the project and run again in Tomcat. still not work? reboot your computer and will work.
Another thing to check is a combination of previous entries
You can have in your web.xml file this:
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.acme.rest</param-value>
</init-param>
and you can have
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.providers</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.resources</param-name>
<param-value>false</param-value>
</context-param>
but you cannot have both or you get this sort of error. The fix in this case would be to comment out one or the other (probably the first code snippet would be commented out)
yes adding the init param for com.sun.jersey.config.property.packages fixed this issue for me.
was merging a jersey rest services into maven based spring application and got this error.
I also got this kind of error, please take care of the configurations in xml.
I wrote
com.sun.jersey.comfig.property.packages
Instead of
com.sun.jersey.config.property.packages
After correction it's working.
that issue is because jersey can't find a dependecy package for your rest
service declarated
check your project package distribution and assert that is equals to your web.xml param value
Probably too late but this is how I resolved this error.
If this solution is not working,
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>/* Name of Package where your service class exists */</param-value>
</init-param>
In eclipse:
RightClick on your Project Or Select Project and press Alt + Enter On the left-hand side of the opened window find Java Build Path
Select Libraries from the right tab panel: If there is anything which is corrupted or showing cross mark on top of the jars, remove and add the same jar again
Apply and Close
Rebuild your project
In my case I have added the jars twice in build path after importing from war.
It worked fine after removing the extra jars which was showing error deployment descriptor error pages
adding
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>service.package.name</param-value>
</init-param>
Also came accross this problem, twice for different reasons. The first time I forgot to include
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>my.package.name</param-value>
</init-param>
as described in previous comments, and once I did that, it started working.
Yet... another day I started Eclipse, expecting to continue where I left off, and instead of having my program working, it showed the very same error once again. I started checking if I accidentally had made some changes and saved corrupted file, but could find no such error and the file looked exactly like examples I have, all in order. Since it worked the day before, after some initial searching, I thought, well, maybe it's a Eclipse, or Tomcat glitch or something, so let's just try to make some changes and see if it reacts. So, I did a space + backspace in web.xml file, just to fool Eclipse that the file is changed, and saved it then. The next step was restarting Tomcat server (from Eclipse IDE) and voila, it works again!
Maybe someone with broader experience could explain what the problem really was behind all of this?
Main cause of this Exception is:
You have not given the proper package name where you using the #Path or forgot to configure in web.xml / Configuration file(Rest API Class File package Name, Your Class Package Name)
Check this Configuration inside <init-param>

Eclipse Spring MVC Project Configuration Files

I have created a Spring MVC project through eclipse. I believe I used some plugins to generate the project directory. I find here there configuration files.
web.xml
root-context.xml
servlet-context.xml
I am kinda of familiar with Spring MVC & its dependency injection. However I have problems understanding the last two configuration files (root-context & servlet-context).
What kind of configurations do they contain?
Also in may online examples I see mvc-dispatcher-servlet.xml. Why did eclipse not generate this xml file in my project?
[IMPORTANT] I wanted to set up strong security and user authentication for my web app. I have been following online tutorials again and they all create a seperate
xml file named spring-security.xml and add the namespace information to that file. Does it suffice if I just create this file and add the name space information? I mean
dont' I need to import this file to a main file that is scanned by Spring framework?
How do I define and where do I put spring application context.xml file and start wiring the dependencies together? Also if I define everything (all dependencies here) how is this file picked up by the framework?
Thanks,
Configuration Files
If you check your web.xml you will find both of root-context.xml and servlet-context.xml files being referred here. One used by Dispatcher Servlet and other by Context Loader Listenter. You can name your files to whatever unless they are being refereed in web.xml
Eclipse Not generating files
Every editor works its own way. some may generate full fledged project/app with both DispatcherServlet and ContextLoaderListner configured or some with only DispatcherServlet ( with minimal configutaion). Check Spring Roo it starts with basic and gives you the flexibility to generate a strong app.
mvc-dispatcher-servlet.xml is not there
Some of the thing in spring projects are convention based, for example if you are not providing any file to your DispatcherServlet in web.xml spring looks for mvc-dispatcher-servlet.xml file, and if you have provided it won't look for.
Spring Security
To Configure Spring Security you need to provide at least some configuration. But the question is where. You need to add this configuration to your web.xml only. and Hence no need to import this to any other file.
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener- class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/spring-security.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Where to define application context.xml
Just define it any where, configure beans in it.
You can add this file as follows:
a) Either Import this into some other configuration file like root-context.xml or servlet-context.xml
as <import resource="application-context.xml"/>
b) Add this into web.xml with ContextLoaderListner as context param
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/spring/application-context*.xml
classpath*:META-INF/spring/abc*.xml
</param-value>
</context-param>

ServletContext in Spring Application

I have one simple question.
If web.xml web application descriptor like this.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml,
/WEB-INF/anotherContext.xml,
/WEB-INF/another2Context.xml
</param-value>
</context-param>
ContextLoaderListener create three differents ServletContexts or three differents ServletContext childs inside one general parent ServletContext?
dispatcher-servlet.xml configuration of DispatcherServlet is another child than others three contexts above?
SOLUTION
I have been investigating about this area, I have created one example application, and every xml files from make one ServletContext only, the same root ServletContext application created by ContextLoaderListener.
maybe you can take a look at this old answer, have a great explanation.
Namespace vs contextConfigLocation Spring init parameters in web.xml
I hope it helps.

Spring: link between WebApplicationContext and ApplicationContext?

I am working on a Spring application. I started from creating a small java app using spring. Later, it became necessary to add a web interface. I decided to use Spring MVC. Now I am confused. In my web.xml I have
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/mvc-dispatcher-servlet.xml,
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
where mvc-dispacher-servlet.xml contains beans necessary for web logic while applicationContext.xml contains beans performing some specific operations. My question is: Are beans in these files going to be aware of each other? Is it going to be a one big container which includes beans from both config files? or these containers are separate?
Yes it will be in one context which will be loaded from the web application context. Its the same as you would do when using the application context and passing in multiple files to it.

Reading 1 bean from other bean defined in different xml file in different directory

How can we read one from other bean defined in a different xml file in a different directory. For example, if there is a bean defined in
src\main\resources\serviceconfig\org\project\core_spring\file1.xml
that needs to reference another bean defined in
src\main\java\org\project\core\commons\persistence_spring\file2.xml
Is there any way to reference it or to import the resources of file2.xml into file1.xml?
Specifying the whole relative classpath as in
<import resource="\org\project\core\commons\persistence_spring\file2.xml" />
solved the issue for me.
Still haven't tried the answer given by Chao though. Would be great if that one works because entering relative path and importing some resource everytime I need to refer to it can be quite cumbersome.
You can import file2.xml into file1.xml. Using
<import resource="classpath:file2.xml" />
After importing you can use bean defined in file2.xml in file1.xml.
you can use the <context-param> tag in web.xml to include multiple bean definition files in your application. You can refer to both contexts both on the classpath or in your web folder. and you will be able to use the beans defined in those files as if they were defined in one file.
For example:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/com/your-company/file1.xml
/WEB-INF/file2.xml
</param-value>
</context-param>

Resources