How to set up a class without adding code?

I would like to write (in Java) something like the following:

public class A implements MagicallyConfigurable { @configuration_source_type{"xml"} @configuration_dir{"some/rel/path/"} /* or maybe some other annotations specifying a db-based configuration etc. */ int length = 4 /* some kind of default */; char c = '*' /* some kind of default */; @do_not_configure String title; A(String title) { /* possibly, but hopefully no need to, something like: configure(); call here. */ this.title = title; } void goForIt() { System.out.println(title); for(int i=0; i < length; i++) { System.out.print(c); } } } 

and so that it works as expected. That is, the only thing I would need to initialize the fields based on the configuration is to add some annotations, implement the interface, and possibly make one function call (but hopefully without it).

I'm sure this is theoretically feasible, the question is whether there is an existing library / framework / add -on / thingie that allows this. (Maybe Apache commons.configuration somehow? Didn't work with it before.)

+1
source share
1 answer

What you can do is use Spring 3.0 EL. An example can be found here , but basically it comes down to the fact that you can do the following:

 @Value("#{systemProperties.databaseName}") public void setDatabaseName(String dbName) { ... } 

And your properties will be automatically set. This will work on setters and properties.

0
source

All Articles