- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
就像我们在 Spring 中执行以下操作
@Value("${varName:0}")
int varName;
有没有办法使用 Google Guice 来做到这一点?
最佳答案
在 Guice 中,您可以注释该方法并使其可选。然后您只需分配默认值即可。如果没有要注入(inject)的属性,则它将是默认值。
例如:
public class TestModule3 extends AbstractModule {
@Override
protected void configure() {
// Properties p = new Properties();
// p.setProperty("myValue", "12");
// Names.bindProperties(binder(), p); // this binds the properties that usually come for a file
bind(Manager.class).to(ManagerImpl.class).in(Singleton.class);
}
public static interface Manager {
public void talk();
}
public static class ManagerImpl implements Manager {
@Inject(optional = true)
@Named("myValue")
int test = 0;
@Override
public void talk() {
System.out.println(test);
}
}
public static void main(String[] args) {
Manager instance = Guice.createInjector(new TestModule3()).getInstance(Manager.class);
instance.talk();
}
}
这将为您打印“0”,因为我注释掉了属性绑定(bind)。如果删除注释,它会将值 12 绑定(bind)到字符串 myValue。注入(inject)注释负责剩下的事情。
希望有帮助,
编辑:
正如@TavianBarnes 指出的,Guice 4+ 有一个OptionalBinder。我针对您的用例尝试了此方法,但无法使其开箱即用。
看来OptionalBinding对于类(实际实例)非常有用,而不是对于属性。原因如下:
您必须提前了解所有属性并将它们绑定(bind)到默认值。人们很容易忘记它们。 OP 所示的示例还表明,他不知道自己是否拥有可用的属性(根据名称)。
属性绑定(bind)的默认实现不能与OptionalBinding 结合使用。
因此,实现该功能的方法如下:
OptionalBinder.newOptionalBinder(binder(), Key.get(String.class, Names.named("myValue"))).setDefault()
.toInstance("777");
Properties p = new Properties();
p.setProperty("myValue", "12");
// use enumeration to include the default properties
for (Enumeration<?> e = p.propertyNames(); e.hasMoreElements();) {
String propertyName = (String) e.nextElement();
String value = p.getProperty(propertyName);
OptionalBinder.newOptionalBinder(binder(), Key.get(String.class, Names.named(propertyName))).setBinding()
.toInstance(value);
}
我必须复制命名绑定(bind)代码并将其更改为支持可选绑定(bind)。
总结:
我更愿意在属性代码中使用Optional=true 标志+默认值。
对可选的实际类使用OptionalBinding。
最后,您还可以做一件事 - 这是我的代码中的解决方案。我有类似的要求(不是可选的,而是默认值)。
我想要:
Apache 已经为此提供了一个方便的库,我已经重用了它。这是我的属性的样子:
myProperty=${ENV_VAR_NAME:-600}
这是如何定义默认值的默认注释。上面的属性说:
然后我将其绑定(bind)如下:
InputStream resourceAsStream = getClass().getResourceAsStream(path);
if(resourceAsStream == null) {
throw new IllegalArgumentException("No property file found for path: " + path);
}
try {
p.load(resourceAsStream);
EnvironmentVariableSubstitutor envSubstitutor = new EnvironmentVariableSubstitutor(false);
Set<Object> keys = p.keySet();
for(Object k : keys) {
String property = p.getProperty(k.toString());
property = envSubstitutor.replace(property);
p.put(k, property);
}
} catch (IOException e) {
throw new IllegalStateException("Could not load properties", e);
} finally {
try {
resourceAsStream.close();
} catch (IOException e) {
log.error("Could not close stream for resource " + path);
}
}
Names.bindProperties(binder(), p);
这段代码的作用是:
这些是我可以在短时间内想出的所有解决方案:)如果有不清楚的地方请告诉我
编辑2:
还有一些关于OptionalBindings和属性的信息+如何处理这个谷歌线程中的默认值:https://groups.google.com/forum/#!topic/google-guice/7Ga79iU_sb0
阿图尔
关于java - 如何在 Guice 中为变量设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38703732/
我有很多 TabularResultSet 类的带注释的命名实例,我想将它们传递给多个类并传递给静态最终属性。我该如何注入(inject)?我认为它不喜欢该属性的最终和静态性质。必须在属性声明中直接调
我是 Guice 的新手,我想知道我能走多远。 我有一个接口(interface)UserInfo具有多个实现类 GoogleUserInfo , FacebookUserInfo , Twitter
我的应用程序中绑定(bind)了两个类型的命名实例: bind(Foo.class).toProvider(FooProvider.class); bind(Foo.class).annotatedW
我有一个 Guice 模块,它有一个 @Provides 方法,它接受 2 个参数并返回接口(interface)的实现: public class **ClientModule** extends
请解释在以下使用工厂的场景中使用 Google-Guice 自动连接的正确方法。我正在使用 XML 工厂进行 XSLT 处理。 StringWriter strWriter = new StringW
我有一个 Guice Module提供 List使用 @Provides - 带注释的方法。 class TestModule() : Module { override fun configur
为什么Guice 3.0针对错误的配置组件(例如,缺少@Inject)抛出此异常,而不是格式化消息? Exception in thread "main" com.google.inject.inte
我有以下供应商: public class GuiceResourceProvider implements Provider { @Inject private Configur
使用 Guice-servlet,我们可以轻松地进行 servlet 映射,如下所示: filter(*.jsp).through(MyFilter.class) 但是,谁能告诉我如何将过滤器映射到
我正在尝试向我的项目添加 Swagger 。我们的设置与示例项目略有不同。我们使用 guice 和 guice-servlet 来注入(inject)并启动我们的 JerseyServletModul
假设我有一个 Guice 模块 ProdModule,我想依赖其他 GuiceModule、ProdDbModule 和 ProdPubSubModule。我将如何实现 ProdModule 的 co
我正在考虑在我的应用程序中使用 Guice for DI,我应该能够在运行时交换实现。下面提供了一个示例来说明要求: class ValidationEngine { public void v
我想设置 Guice 绑定(bind),所以我用 Java 创建了一个完美运行的模块: public class CrashLoggerModule extends AbstractModule {
所以我有一个模块将接口(interface)与实现类绑定(bind)。 bind(ILocalStore.class).to(LocalStore.class); 此绑定(bind)是否还会注入(in
guice 4.0 是否向后兼容 3.x?无法从发行说明或常见问题解答中弄清楚... 如果没有,是否有兼容性问题列表? 最佳答案 我没有任何官方来源但根据我的经验,没有任何兼容性问题。 我使用了几个
我正在尝试使用 Guice,并且我来自 Spring。 我想知道 @Inject 是否相当于 Spring 中的 @Autowired 以及我是否可以在 Web 应用程序中使用它,就像在 Spring
我是 Guice DI 的新手。我想弄清楚我的情况。 简单来说,有没有通过Guice @annotations来替代MapBinder的? 我的场景: Interface A{} Class A1 i
我的项目正在使用 Guice作为负责为大型对象图(主要是单例)提供依赖项(服务类)的 IOC 容器。有时,如果在构造过程中依赖项失败,并且许多对象都需要此依赖项,则失败将一遍又一遍地发生,并将异常添加
我有一个类 (CustomConnectionProvider),它将由第三方库 (hibernate) 使用 class.forName().newInstance() 实例化。我需要注入(inje
删除 guice servlet 后,我需要进行一些清理。使用 guice servlet 时是否可以 Hook 到 servlet 破坏?我需要使用喷油器进行清理工作。 我可以覆盖 contex
我是一名优秀的程序员,十分优秀!