英文:
How to inject a string in a JAVA servlet´s method?
问题
我是新手使用依赖注入。有人可以告诉我如何配置一个Servlet来注入一个指向文件的字符串URI吗?方法签名如下:
@Inject
public void setCar(String value)
{
CarUri =value;
}
提前感谢!
英文:
I am new using dependency injection. Can anyone tell me how to configure a servlet to inject a string URI pointing to a file? The method signature is the following:
@Inject
public void setCar(String value)
{
CarUri =value;
}
Thanks in advance!
答案1
得分: 0
你可以尝试注入@Named
字段
// 在模块内部
@Provides
@Named("CarURI")
public String carURI() {
return "URI";
}
@Inject
public void setCar(@Named("CarURI") String value) {
CarUri = value;
}
示例:
- https://www.tutorialspoint.com/guice/guice_field_injection.htm
- https://www.tutorialspoint.com/guice/guice_method_injection.htm
英文:
You can try injecting @Named
fields
// inside module
@Provides
@Named("CarURI")
public String carURI() {
return "URI";
}
@Inject
public void setCar(@Named("CarURI") String value) {
CarUri = value;
}
example:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论