views:

203

answers:

2

I am using a properties File to store some configuration properties, that are accessed this way:

@Value("#{configuration.path_file}")
private String pathFile;

Is it possible (with Spring 3) to use the same @Value annotation, but loading the properties from a database instead of a file ?

+1  A: 

Although not having used spring 3, I'd assume you can, if you make a bean that reads the properties from the database and exposes them with getters.

Bozho
+1  A: 

Assuming you have a table in your database stored key/value pairs:

Define a new bean "applicationProperties" - psuedo-code follows...

public class ApplicationProperties {
    @AutoWired
    private DataSource datasource;

    public getPropertyValue(String key) {
        // transact on your datasource here to fetch value for key
        // SNIPPED
    }
}

Inject this bean where required in your application. If you already have a dao/service layer then you would just make use of that.

anger
Thank you, but the application is already annotated with @Value annotations, so I'd like to avoid changing the code. Do you think it is possible to keep @Value("#{configuration.path_file}") and tell Spring "hey, when I use #{configuration.path_file} I want you to call ApplicationProperties.getPath_file· or something like that ?This way I can keep the current code untouched.
Guido
What is satisfying the dependency then? Is the class responsible for loading the properties file itself?
anger
It works, using a @Bean. The only problem is that a change in the DB value is not reflected, as the value is injected only once, thus keeping the initial value. I suppose this is not possible.
Guido