Multiple Spring configurations: test and production

Hi all,

I'm not sure if I'm just doing things wrong or if I've run across a bug.  I have a Spring project setup with source and test directories.  I have a set of Spring configuration files for production and a set for unit tests.  I have File Sets defined for both to keep them separate.  The problem is that I'm using annotation based configuration and IDEA is saying that it can't autowire the beans because there are multiple definitions for the beans i'm attempting to inject (one defined in the production configuration and one in the test configuration).  Since I have File Sets defined for both, it was my assumption that IDEA would not confuse the two by thinking they are both part of the same configuration (isn't this the purpose of File Sets?).  Am I just setting this up wrong or is this a bug?  What is the proper way to have production and unit test Spring configurations in the same project without having IDEA consider them as all one big configuration?

Thanks!

7 comments
Comment actions Permalink

If you have a Annotation based config, you, most probably, have a pattern to lookup annotated files, e.g. com.yourdomain.*. In that case for the second (test) fileset, classpath includes all the annotated classes, so idea behaviour is quite clear. Did you try @Qualifier?

regards, Alex

0
Comment actions Permalink

You are correct; I do have patterns in each of the configurations.  Each configuration should have all of the annotated classes because they are needed by both.  Why doesn't IDEA keep the scope of each File Set separated?  The production File Set defines all of the classes needed for it and the test File Set defines what it needs.  IDEA should not be confused since each File Set should basically be considered a different application. (the test application context and the production application context)

I don't want to use @Qualifier since it really doesn't apply for this use case.  I don't have multiple beans defined in the same File Set; they are completely different configurations.

Am I misinterpreting the File Set concept for IDEA or is this a bug?

0
Comment actions Permalink

As i understand, you have some pattern to scan for beans in fileset 1. That Pattern will discover beans of the same type on the test classpath and on the application classpath. So the issue  doesn't relate to the number of filesets, most probably even single fileset will be reported as wrong by IDEA.
I concluded it from:

I have a set of Spring configuration files for production and a set for unit tests.  I have File Sets defined for both to keep them separate.

Altgthoug you wrote in the second post:

I don't have multiple beans defined in the same File Set; they are completely different configurations.

But these 2 stetments are contradicting to me. To make fileset Autowring works correctly, you have to separate beans of the same type on the classpath, either use @Qualifier instead.




regards, Alex

0
Comment actions Permalink

I'm sorry, but I must not be stating my problem clearly.  My understanding is that File Sets are used to organize Spring configuration files that are mutually related so that Intellij IDEA can determine that the definitions belong to the same applicaton context.  Configuratoin files that are not part of the same File Set (or related via inheritance) should not be considered as part of that application and should not be considered when compiling the set of beans for the application.

I have my applicatoin File Set that contains the main production bean definitions.  It also scans the source for annotated beans.  This is the set of beans for production.

I also have a test File Set that contains many mock bean definitions and it too scans the source to inject these mock objects for testing.  The is a separate set of beans for testing.

The problem is that IDEA sees the application context injecting beans into the annotated beans and it also sees the test applicaton context injecting beans into the annotated beans.  It seems to be ignoring the fact that the beans being injected are from two different File Sets that are unrelated.  So it marks the autowired beans as an error stating that there are multiple bean definitions.  To me this is an error since the beans being injected are from two different and unrelated File Sets.

The @Qualifier annotation cannot fix this problem.  I need both the test and application contexts to be injected depending on whether or not i'm running the applicatoin or running unit tests.

0
Comment actions Permalink

Sorry to open an old thread, but I too agree that this is an issue.

In my case, the project used in Springsource Tool Suite, didn't have any false positives.  But in IDEA, my @Autowired integration test dependencies, are considering both the production 'beans.xml' as well as my 'TestIntegration-Context.xml' files, and erroring up in the saying that could't autowire as the beans are defined twice.

I want to be able to tell IDEA that only one the 'TestIntegration-Context.xml' config files is to be considered a candidate for wiring in any of my Junit tests in a test folder.  It sounded like the File Sets facility was the place for this but alas doesn't appear to be the case.

I know work has been going on in this area very recently with regard to Spring profiles IDEA-80589.  Same error msg.  The defect is marked as 'fixed' but no comments to indicate how one would switch profiles.  Something like that though for the more general case would be great.

0
Comment actions Permalink

Haha. Thanks for the visual response Sergey. You should put those on the wiki.

Any ideas how we can get profile like behaviour for pre 3.1 code?

0

Please sign in to leave a comment.