PDA

View Full Version : Variable number of spring beans from properties file



d80tb7
Oct 20th, 2009, 02:56 AM
Hi,

we currently configure our application via a Spring XML context file and us a PropertyPlaceholderConfigurer so that external properties can be exposed. We now want to allow functionality sich that a variable number of sping beans can be set up via the properties file: For example:


We declare a Spring bean "foo" which takes a list of type "bar"

"bar" has two properties: property1 and property2.

In the properties file the user will enter something like:

bar.1.property1=a
bar.1.propperty2=b
bar.2.property1=c
bar.2.property2=d

...

bar.n.property1=x
bar.n.property2=y



This will cause spring to instantiate the "foo bean" with a list of n "bars" each with the properties given in the properties file.

Does anybody know if something like this is possible with the Spring framework as it stands at the moment? If not where would be the best place to start in order to add the functionality?


many thanks,

Chris

denis.zhdanov
Oct 20th, 2009, 04:14 AM
Generally speaking you're asking about '*.properties' format for spring beans definition. From my point of view that doesn't make sense because its much more convenient to provide beans configuration at '*.xml' format and create application context with wildcard (http://static.springsource.org/spring/docs/2.5.x/reference/resources.html#resources-app-ctx-wildcards-in-resource-paths) instead of explicitly defined config names.

d80tb7
Oct 20th, 2009, 04:27 AM
Generally speaking you're asking about '*.properties' format for spring beans definition. From my point of view that doesn't make sense because its much more convenient to provide beans configuration at '*.xml' format and create application context with wildcard (http://static.springsource.org/spring/docs/2.5.x/reference/resources.html#resources-app-ctx-wildcards-in-resource-paths) instead of explicitly defined config names.


Hi Denis,

yes ideally we want to use the properties file for bean definition. The reason for doing this is that we want to give the user of our software the ability to do some configuration, but without exposing him or her to the Spring context while which reveals the internal workings of our program. If I understand you correctly what you are proposing is to provide a second spring context xml file which the user can edit. This may be possible and I will look into it, but from a user point of view I would much rather give them a properties file with which to configure the system.

Chris

denis.zhdanov
Oct 20th, 2009, 04:43 AM
Hi Denis,

yes ideally we want to use the properties file for bean definition. The reason for doing this is that we want to give the user of our software the ability to do some configuration, but without exposing him or her to the Spring context while which reveals the internal workings of our program. If I understand you correctly what you are proposing is to provide a second spring context xml file which the user can edit. This may be possible and I will look into it, but from a user point of view I would much rather give them a properties file with which to configure the system.

Chris

Chris,

It seems that you may be interested in providing your users ability to setup the configuration via scripting (http://static.springsource.org/spring/docs/2.5.x/reference/dynamic-language.html) - it offers much more convenient configuration than flat properties file.

Regards, Denis