Java 如何在 Spring 运行时更改属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38761781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:37:17  来源:igfitidea点击:

How change property values at runtime in Spring

javaspring

提问by Tomasz D?bski

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.

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

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

我的 spring xml 配置

<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?

我可以在运行时以某种方式更改此值吗?

采纳答案by SeaBiscuit

Just move away from xml configuration its almost 2017.

几乎在 2017 年就不再使用 xml 配置了。

@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.

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

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.

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

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.

编辑:回答完全有效的@matt 评论。如果是这种情况并且转向 Java confing 不是一个选择。对于自定义行为,您只需在您的服务中自动装配 XML 定义的 bean 并执行您想要的任何逻辑。

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

回答by Tomasz D?bski

I have to use XML configuration. Our project is based on it. Finally I need to get all configuration values from db. The simplest solution is used service for configuration and always invoke configuration state from db.

我必须使用 XML 配置。我们的项目就是基于它。最后,我需要从 db 获取所有配置值。最简单的解决方案是使用服务进行配置并始终从数据库调用配置状态。

configurationService.findAllConfigurations().get("hours.expired")

this return value what is stored in db.

这个返回值存储在 db 中。

But I think there is better solution.

但我认为有更好的解决方案。

回答by Suman Behara

It's not a good practice to change value from property files using @Value("${hours.expired}"). If your change any value from property file,you need to restart your server or re-run application. so its better to store hours.expired value in Database. You can easily update whenever you want.

使用@Value("${hours.expired}") 更改属性文件中的值不是一个好习惯。如果您更改了属性文件中的任何值,则需要重新启动服务器或重新运行应用程序。所以最好在数据库中存储 hours.expired 值。您可以随时轻松更新。