• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Spring in Action - How to organize Bean Configuration files

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Graig,

I read the previous version of your book and liked it very much. I was a beginner with Spring and it gave me a good overall understanding.

My question is:
What is a good way to organize your bean config files in your project? I don't like long xml-files which are hard to read and I try to separate the beans in groups and use "<import resource="file"/>" to keep my configs short. The problem with this approach is that all beans in imported file are hardcoded and I can't affect the names of the or anything else. One thing that could solve my problem might be to define own config elements but I have found it too complicated for this kind of simple thing.

Thanks.
 
author
Posts: 422
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Juuso Ala-Uotila wrote:Hi Graig,

I read the previous version of your book and liked it very much. I was a beginner with Spring and it gave me a good overall understanding.

My question is:
What is a good way to organize your bean config files in your project? I don't like long xml-files which are hard to read and I try to separate the beans in groups and use "<import resource="file"/>" to keep my configs short. The problem with this approach is that all beans in imported file are hardcoded and I can't affect the names of the or anything else. One thing that could solve my problem might be to define own config elements but I have found it too complicated for this kind of simple thing.

Thanks.



I tend to break my XML files down by the specific function that they serve (web, security, persistence, etc). I sometimes use the <import> element as you mentioned, but I also more often just list them out in web.xml for ContextLoaderListener to load. I can't say that I've settled on any particular approach here, but I usually let ContextLoaderListener load up the first level of XML files and then if any of those require further breakdown, I use <import> to pull in the finer-grained configs.

That said, taking advantage of things like Spring's component-scanning goes a long way in keeping your XML minimal. For instance, in my mvc configuration, I usually have just a few elements, one of them being a <context:component-scan> that pulls in all of my controller beans (that are annotated with @Controller).
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a number of solutions for this.

1) Using Maven, you can create a directory in the src/main/resources directory for each environment/variation in which each directory has the same named config file. Them using Maven profile's when you build it will copy the corresponding config files from the corresponding directory for that profile. Maven automatically copies files from the resources directory into your classpath of your archive.

2) Use the new Spring 3.x Expression Language to dyamically figure out what environment you are in and import the correct file. You will need to read the SPel documentation.

3) In the upcoming Spring 3.1 they are introducing Profiles in your Spring config file. Basically the implementation will be <beans> tags inside <beans> tag where the inner <beans> tag will have an attribute "profile" So you could do something like



This new approach is similar to what is already in Grails config files, and is extremely cool.

4) Use the PropertyPlaceholderConfigurer and in your <import resource="${config.fileName}" then in a Properties file, or a System environment variable on that machine set the config.fileName property to the filename you want imported in that environment. (This same type of approach can be used with Spring Expression Language with "#{systemEnvironment.config_fileName}" which would look for an OS environment variable called config_fileName

5) You can do it the long laborous way and change your xml config file just before you compile and create your archive. ;)

Mark>
 
reply
    Bookmark Topic Watch Topic
  • New Topic