tsuisou tsuisou - 3 months ago 51
Java Question

How change property values at runtime in Spring

I need change properties in my application at runtime.
For example I have a service which send a e-mail with resset password. Request is valid 12 hours. But I want to change this time to 24 or more at runtime. I need to give the opportunity to this action for admin.

My property file has

hours.expired=12


My service

private int hoursExpiredPassword;

public void setHoursExpiredPassword(int hoursExpiredPassword) {
this.hoursExpiredPassword = hoursExpiredPassword;
}

@Override
public ERequests checkRequest(String number, Date date) {
PasswordResetRequest findedObject = passwordResetRequestDao.getObjectByElement(PasswordResetRequest.class, "requestId", number);
if (findedObject == null){
return ERequests.BAD_REQUEST;
}else{
long result = getDateDiff(findedObject.getRequestDate(),date,TimeUnit.HOURS);
if(result >= hoursExpiredPassword){
return ERequests.EXPIRED_REQUEST;
}
}
return ERequests.CORRECT_REQUEST;
}


my spring xml configuration

<bean id="passwordResetRequestService" class="pl.lublin.example.services.servicesDAO.PasswordResetRequestService">
<property name="passwordResetRequestDao" ref="passwordResetRequestDao"></property>
<property name="hoursExpiredPassword" value="${hours.expired}"></property>
</bean>


Could I change this value in someway at runtime?

Answer

Just move away from xml configuration its almost 2017.

@Service
public class PasswordResetRequestService {

@Value("${hours.expired:12}") 
private int hoursExpiredPassword;

@Autowired
private PasswordResetRequestDao passwordResetRequestDao;

public void setHoursExpiredPassword(int hoursExpiredPassword) {
    this.hoursExpiredPassword = hoursExpiredPassword;
}


@Override
public ERequests checkRequest(String number, Date date) {
    PasswordResetRequest findedObject = passwordResetRequestDao.getObjectByElement(PasswordResetRequest.class, "requestId", number);
    if (findedObject == null){
        return ERequests.BAD_REQUEST;
    }else{
        long result = getDateDiff(findedObject.getRequestDate(),date,TimeUnit.HOURS);
        if(result >= hoursExpiredPassword){
            return ERequests.EXPIRED_REQUEST;
        }
    }
    return ERequests.CORRECT_REQUEST;
   }

}

With @Value you are pulling hours.expired value from properties file, if there is no value default will be 12. You can also call setHoursExpired at runtime and set new value and expose that functionality to your admins.

This is convenient for one time actions. If you want your admins to permanently change password expiration time i would instead persist hours.expired value in mysql or whatver db you are using.

EDIT : answering to perfectly valid @matt remark . If that is the case and moving to Java confing is not an option. For custom behavior you can just autowire your XML-defined beans in your service and perform whatever logic you want to.

@Autowired
private pl.lublin.zeto.zetoRA.services.servicesDAO.PasswordResetRequestService passwordResetRequestService;
Comments