5

I am using Spring 3.2.8 and keep my settings in a properties file. Now I want some of them override at runtime. ​​I want to keep the new values persistent by overwriting the old values in the properties file​​.

How can I do this in Spring? Some properties ​​I inject with @ Value and others get with MessageSource.getMessage(String, Object [], Locale). The beans are already instantiated with these values​​. How can I access the properties, store them and update all beans system wide?

Thanks!

5
  • How are the properties overridden and how frequently do they change? Commented May 6, 2014 at 12:56
  • The properties are actually not overridden. I have to implement that. It seems to me the implementation of such a simple feature is very complex in Spring. Now I think to have a additional file with overridden values and keep the defaults in the Spring properties. I try to using my own ConfigurationBean and inject it where I need properties values. So I have a better control.
    – Robert
    Commented May 6, 2014 at 13:10
  • I think now I understand your question. The values of the properties are set on the /settings page. Not so often, but the server can not be restarted.
    – Robert
    Commented May 6, 2014 at 13:12
  • And is it a one-to-one mapping between property and bean or is a single property used by multiple beans? So if a property changes, are there multiple locations that need to be updated? Commented May 6, 2014 at 13:17
  • Sure multiple beans used a single property. No one wants the update all beans manually :).
    – Robert
    Commented May 6, 2014 at 13:41

1 Answer 1

1

OK, given your follow up answers I would keep this fairly simple and use what you already know of Spring. I'll make some assumptions that annotation configuration is OK for you.

In my example, I'll assume all the properties that you want to configure relate to something called a ServerConfiguration and that initially these are read from server.properties on the classpath.

So part 1, I would define a bean called ServerProperties that has the original values from the server.properties injected into it.

So:

@Component
public class ServerProperties
{
     @Value("${server.ip}");
     private String ipAddress;

     ...

     public void setIpAddress(String ipAddress)
     {
        this.ipAddress = ipAddress;
     }

     public String getIpAddress()
     {
        return this.ipAddress;
     }

}

Secondly, anywhere that relies on these properties, I would inject an instance of ServerProperties rather than using @Value e.g:

@Component
public class ConfigureMe
{
    @AutoWired
    private ServerProperties serverProperties;

    @PostConstruct
    public void init()
    {
        if(serverProperties.getIpAddress().equals("localhost")
        {
            ...
        }
        else
        {
            ...
        }
    }
}

Thirdly I would expose a simple Controller into which ServerProperties is injected so that you can use your web page to update system properties e.g:

@Controller
public class UpdateProperties
{

    @AutoWired
    private ServerProperties serverProperties;        

    @RequestMapping("/updateProperties")
    public String updateProperties()
    {
       serverProperties.setIpAddress(...);
       return "done";
}

Finally, I would use @PreDestroy on ServerProperties to flush the current property values to file when the ApplicationContext is closed e.g:

@Component
public class ServerProperties
{

    @PreDestroy
    public void close()
    {
        ...Open file and write properties to server.properties.
    }
}

That should give you a framework for what you need. I'm sure it can be tweaked, but it will get you there.

4
  • That's exactly what I've done in the meantime. Only what I do not have implemented is the @PreDestroy annotated method. I still have to consider whether it is not better to store the properties immediately because the JVM can be possibly shot with a KILL signal or something similar so there are no time to execute the @PreDestroy methods. Annotations are basically ok, but I use it on a setter and not the variable directly. I like to have a setter / getter Layout about my fields.
    – Robert
    Commented May 6, 2014 at 15:04
  • Thanks, I thing this solution is simple and good enough. Funny that Spring does not offer properties handling like that out of the box.
    – Robert
    Commented May 6, 2014 at 15:04
  • Apache Commons Configuration looks interesting. Maybe I can use it without reinventing the wheel.
    – Robert
    Commented May 6, 2014 at 15:18
  • Spring Boot does introduce something like this (docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/…), but it is definitely not part of the main Spring code base. Commented May 6, 2014 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.