When running a test using default gradle set of tasks includes a "processTestResources" step, which looks for test resources at C:\.\src\test\resources (Gradle helpfully provides the complete path). For my gradle project I had laid out the workspace as defined at The solution (for gradle at least) lies in the way gradle processes resources. Even specifying the complete path as below did not help ! context = new ClassPathXmlApplicationContext("C:\\.\\applicationContext.xml") Copying the applicationContext.xml all over the place did not help. I was facing this issue when using gradle to run my build and test. I have resolved this issue by adding classpath lookup while importing at applicationContext.xml like below Īnd removed the the line ClassPathXmlApplicationContext("beans.xml") in java code, so that there will be only one ApplicationContext loaded. This is looking the beans.xml at WEB-INF/classes OR in eclipse src/main/resources. Where as in the code if it is declared like below ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml") See the difference between beans.xml and applicationContext.xmlĪnd if appliationContext.xml under has declared with then this appliationContext.xml is loading the beans.xml under the same location META-INF/spring of appliationContext.xml. New ClassPathXmlApplicationContext("beans.xml") private static final ApplicationContext context = Where as below code will also tries to create one more applicationContext. Note that the first applicationContext is loaded as part of web.xml which is mentioned with the below. I would recommend reading articles about classpath as supplementary. rar) and see its content with the directories structure. It can be verified by unpacking a jar with an archiver (i.e. When using ClassPathXmlApplicationContext, the proper location declaration for beans xml definitions, in this case, was "/beans.xml", since this is path where it belongs in jar and later on in classpath. When code (from the post) was in running mode, it couldn't find nothing like "src/main/resources/beans.xml" due to the fact, that beans.xml was in the root of jar (copied to /beans.xml in created jar/ear/war). This is a convention respected by many tools like maven or sbt in process of building project (note: as a default configuration!). The same rule applies to 'src/main/resources'. When the project is being compiled and packaged, all the files and subdirs from 'src/main/java' in the project goes to the root directory of the packaged jar (the artifact we want to create). Since I get many people thumbs up for the solution and had had first experience with Spring as student few years ago, I feel desire to explain shortly why it works. Then the file was left in src/main/resources/ but I changed declaration to: ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml") Īnd it solved the problem - maybe it will be helpful for someone. Then I put the configuration file to src/main/resources and changed initialization to: ApplicationContext context = new ClassPathXmlApplicationContext("src/main/resources/beans.xml") I thought I would refer to root directory of the project when beans.xml file was there. Since I'd done a declaration: ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml") I found it out why it wasn't working for me.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |