, , , Spring. , .
, , , , ServerConfiguration, server.properties .
, 1, bean, ServerProperties, server.properties, .
:
@Component
public class ServerProperties
{
@Value("${server.ip}");
private String ipAddress;
...
public void setIpAddress(String ipAddress)
{
this.ipAddress = ipAddress;
}
public String getIpAddress()
{
return this.ipAddress;
}
}
-, , , ServerProperties @Value :
@Component
public class ConfigureMe
{
@AutoWired
private ServerProperties serverProperties;
@PostConstruct
public void init()
{
if(serverProperties.getIpAddress().equals("localhost")
{
...
}
else
{
...
}
}
}
-, Controller, ServerProperties, - , :
@Controller
public class UpdateProperties
{
@AutoWired
private ServerProperties serverProperties;
@RequestMapping("/updateProperties")
public String updateProperties()
{
serverProperties.setIpAddress(...);
return "done";
}
, @PreDestroy ServerProperties, , ApplicationContext , :
@Component
public class ServerProperties
{
@PreDestroy
public void close()
{
...Open file and write properties to server.properties.
}
}
, . , , .