wood burning stoves*
The moose likes Spring and the fly likes ClassPathXmlApplicationContext wildcard problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "ClassPathXmlApplicationContext wildcard problem" Watch "ClassPathXmlApplicationContext wildcard problem" New topic
Author

ClassPathXmlApplicationContext wildcard problem

Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Hey,

my application currently consists of 2 spring context xml files, which are to be loaded via the ClassPathXmlApplicationContext.

The application is set up with maven and the context files are located under resources/spring/

This is how I create the context



This, however, only loads the services-context.xml which is located in the services project, but fails to load the database-context.xml (located in the database project).

If i turn the creation into

it does load the database config.

Why does it fail to load it when I use only wildcards?


Thank's for your help


JDBCSupport - An easy to use, light-weight JDBC framework -
Atul Kotwale
Greenhorn

Joined: Dec 22, 2011
Posts: 17
I am not able to understand the problem. Please provide your maven project structure, it would really helpful understand the problem. Also please let us know which version of spring you are using. I checked the java doc(spring version 2.0) for ClassPathXmlApplicationContextclass and found that there is no constructor which takes two string argument. As you stated in your problem.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Hi,

thanks for your reply. I am currently trying to fix the problem, but no luck so far.

This is the (simplified) project structure.

Maven Projects:
- configuration
- database (depends on configuration)
- services (depends on database and on configuration)

in the source/main/resources folder, the database and services projects have a spring folder which contains the files services-context.xml and database-context.xml.

the configuration project contains a class that holds the application context. In essence, is encapsulates this:




Now, I solely run a main method in the database project and trigger the application context to initialize, I expect it to find the database-context.xml, and in fact it does.

When I run a main method in the services project and trigger the app context to initialize, I expect it to find the database-context.xml and the services-context.xml. As the services project depends on the database project, this shouldn't be a problem. However, it ends up finding the services-context.xml but not the database-context.xml.

I have checked the class path, the database-context.xml is included, but spring won't consider it.

Hope that helps to describe the problem.

James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Sebastian

Have you tried loading the files this way:


Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
yes, this works fine. but it's not what I want.

What I would like is all context files that are located in any project under spring/*-context.xml to be part of the application context, without having to add them to the list of context files.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

I don't think you will be able to use wildcards here. The constructor you are using takes an array of Strings but each String must point to a single file.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
ah, okay.

I thought that the ClassPathAppContext scans the class path to find all files that match the pattern.

It works id I do the following.

in the services-context.xml




That works and is acceptable I guess, even though the scanning variant would be more elegant.

There has to be a way to get this working, after all all those frameworks scan projects for spring files.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Hey,

I have found a solution to get it working.


Here is how:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: ClassPathXmlApplicationContext wildcard problem
 
Similar Threads
Spring - ClassPathXmlApplicationContext not able to load the XML file
Classpath issue: class path resource [beans.xml] cannot be opened because it does not exist
Integrating Logging with Main EJB Application
Spring application context
Referencing XMLs from a runnable jar