Spring include context file classpath
In my web. Could you pls tell which one will be picked up as contextConfigLocation file. But the way it looks to me right now, only the dispatcher-servlet-config.
The web-application-config. If you DO include such a listener, then both context files will be loaded. The beans from web-application-config. Hi Ben, I have a issue with loading spring-aplication-context. The application is not getting loaded and says filenotfound for spring-aplication-context. The project is a maven project. What can be the possible error?
An absolute location without a classpath: prefix is an absolute location and a relative location is relative to the working directory of the application. Hmm, I see I mist the whole thread of comments. Did not notice it was a response to another comment.
This 2nd project has been added to my current project and the context in web. Very nice article. I will use module specific names. What if I want to load two application context files with dependencies like applicationContextTwo uses beans in applicationContextOne and both are in different classpaths. The only way to have multiple contexts in a program is to have the program start two separate instances of the Spring container.
I agree with Uri. Note that my post was intended as an overview and not so much as a recommendation. An advantage of explicitly defining the actual application context names without wildcards is that it provides you with yet another view of your layered architecture. Hidden way over there in the far right of the error message, three screens to the right with the horizontal scroll bar, is this fragment: cannot be resolved to absolute file path because it does not reside in the file system And you're getting a FileNotFoundException.
So something in the bean creation is under the mistaken impression that you are giving it a file name. Perhaps that's because of line 17 in your bean class which calls a getFile method? I'm just guessing, I know very little about Spring. I tried to put the text file location as this in the config. I should have mentioned it clearly in the original post, but the three files are at the root of the "fichiers.
In the original project that was not the case, but I quickly found with this test that it did not matter either way, as Spring did not manage to find it. As for your question Paul, the stacktrace shows that the error occurs during Spring bean initialisation, and not in the class itself. Even then, the whole point of this little project is to act like a test case, it must not fail anywhere because it mimics the desired outcome, which is to have access to the files from within the class for further use, from resources given by Spring.
A few things I noticed since yesterday: 1- In the end of the output the last three lines show that the bean methods were called setResource, init, etc I'll do more tests in those method to test the output, just in case the error message was a false positive. However, this is not a desirable solution because the project is meant to be executed as an applet , and therefore must be totally contained within signed jars. I'll post new developments soon.
Ok, I got help from a fellow programmer and the problem was thus: But first, an exerpt from the Spring documentation: 4. In a nutshell, this means that you must NOT use a normal File String constructor with a Spring resource, because the underlying implementation will try to use some kind of absolute path. Therefore, to prevent this, you must use the getFile method from a URL object as the documentation says in a weird way. That URL object can be fetched from the Resource instance. The resulting path will be the correct one.
That means that Paul's hunch was correct: the error was not in the Spring loading sequence despite the stacktrace but in the bean class setters. I was fooled, but to my defense it really was counter-intuitive. I corrected the code and it works. See below. Therefore, we must use an ApplicationContext instead of a bean factory in the main class for it to work. It will throw an NullPointerException otherwise because the init method will be called without the resourceLoader being initialized.
Abhishek Abhishek 6, 21 21 gold badges 60 60 silver badges 76 76 bronze badges. Can you post your code where you try instantiating the appContext? This seems ok, also the exception you are getting seems to indicate that the problem is not with creating the appContext, but getting the bean from it. How are you getting the bean, and please also post the definition of it in the applicationContext.
It works as charm. I try to load the bean in same project A works fine. I believe its not an issue with the mapping nor code its something with the configuration.. Show 2 more comments. Active Oldest Votes. This should work, I just created the 2 projects and checked. Improve this answer. Subir Kumar Sao 7, 3 3 gold badges 21 21 silver badges 45 45 bronze badges.
Thanks a bunch. I was unable to figure out what the issue was. I just cleared up everything dumped the projects and recreated from scratch and it works. You response clarified my question, I have voted up for this. Add a comment.
0コメント