如何在Java Servlet的方法中插入一个字符串?

huangapple go评论78阅读模式
英文:

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

示例:

  1. https://www.tutorialspoint.com/guice/guice_field_injection.htm
  2. 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:

  1. https://www.tutorialspoint.com/guice/guice_field_injection.htm
  2. https://www.tutorialspoint.com/guice/guice_method_injection.htm

huangapple
  • 本文由 发表于 2020年9月29日 18:44:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64118005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定