如何在Spring的运行时更改属性值 [英] How change property values at runtime in Spring

查看:551
本文介绍了如何在Spring的运行时更改属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时在应用程序中更改属性. 例如,我有一项服务,该服务发送带有重置密码的电子邮件.要求有效期为12小时.但是我想在运行时将此时间更改为24或更多.我需要给管理员一次执行此操作的机会.

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.

我的财产文件具有

hours.expired=12

我的服务

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;
}

我的spring xml配置

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?

推荐答案

仅从xml配置开始就走了将近2017年.

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;
   }

}

使用@Value,您将从属性文件中获取hours.expired值,如果没有值,默认值为12.您还可以在运行时调用setHoursExpired并设置新值,并将此功能向管理员公开.

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.

这对于一次操作很方便.如果您希望管理员永久更改密码的到期时间,我会在MySQL或您正在使用的任何数据库中保留hours.expired的值.

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.

回答完全有效的@matt备注.如果是这种情况,则不能选择使用Java confing.对于自定义行为,您只需将XML定义的bean自动连接到服务中,然后执行所需的任何逻辑即可.

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;

这篇关于如何在Spring的运行时更改属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆