以下是我面临的问题的近似值。
认为我们有一个带有一些规则的密码验证器。
public interface RuleChecker{
//Checks for a password strenght, returns 10
//for strong or 0 for soft password.
int check(String pass);
}
然后我们有几种实现,我们的服务只接受超过8分的密码。
public class NoCheck implements RuleChecker {
public int check(String pass){return 10;}
}
public class LengthCheck implements RuleChecker{
...
}
public class AlphanumericCheck implements RuleChecker{
...
}
public class AlphaAndLenghtCheckAdapter implements RuleChecker{
...
}
但出于测试目的,我们希望在应用程序中实现一个 Web 服务,我们可以在其中“管理”这些规则,并选择要使用的规则。
public class PasswordCheckService{
private RuleChecker checker;
@Inject
public PasswordCheckService(final RuleChecker checker){
this.checker = checker;
}
public boolean checkPassword(String password){
return checker.check(password) > 8;
}
}
那么,Guice 有什么方法可以在运行时更改服务的注入吗?
Example:
我们启动了应用程序,默认情况下选择 LengthCheck 并将其注入到应用程序中,在网站上我们选择 NoCheck 复选框并保存选项,该选项存储到数据库中,我可以配置 Guice 以自动更改服务之前注入的 bean 吗?那么从现在开始就不再检查新密码了?
--
就目前而言,我已经找到了这些主题
Google Guice 和运行时的不同注入 https://stackoverflow.com/questions/7651980/google-guice-and-varying-injections-at-runtime但我不知道那种提供商是否适合我的问题。
Guice运行时依赖参数重新注入 https://stackoverflow.com/questions/7641565/guice-runtime-dependency-parameters-reinjection这个很好的问题是在谈论类似的事情,但不是我想要的形式。
guice:命令行运行时注入/绑定 https://stackoverflow.com/questions/7663618/guice-runtime-injection-binding-at-command-line这是最接近我的问题,但他只在启动“运行时”时执行,并且不会随着时间的推移而改变它。
有什么帮助吗?
谢谢你!
使用第一条评论的提示,我实现了这个 POC,但仍然不起作用,如果您更改选择另一个按钮,则服务 bean 不会更新。https://bitbucket.org/ramonboza/guicedynamicconfig https://bitbucket.org/ramonboza/guicedynamicconfig