我使用 CDI 作为注入(inject)框架,但我发现它的使用有一些限制,这就是其中之一。我正在尝试使用 runtime 值初始化 bean 实例的创建。示例:
@RequestScoped
public class MyNumber {
int number;
public MyNumber(int number) {
this.number = number;
}
public String toString() {
return "Your number is: " + number;
}
}
public class UseNumber {
@Inject
Instance<MyNumber> number;
public void doStuff() {
int a = 8;
MyNumber mN = number.select(a).get(); // ?? - Obviously this does not work.
System.out.print(mN); // Should print: "Your number is: 8"
}
}
请注意,“a”在示例中是一个常量,但实际上它是一个变量;我澄清了这一点,这样您就不会发布带有 @Producer 的答案,然后将值注入(inject) MyNumber 的构造函数中。
现在,有人知道我该怎么做吗?
最佳答案
我不确定您要做什么,但据我了解,您希望使用注入(inject)点注释中的数据或在运行时通过编程查找来初始化您的 bean。您可以通过在 bean 中使用 InjectionPoint 元数据来做到这一点(唯一的限制是将 bean 放在依赖范围内)
你可以这样做。
首先创建一个具有非绑定(bind)值的限定符。
@Qualifier
@Target({TYPE, METHOD, PARAMETER, FIELD})
@Retention(RUNTIME)
@Documented
public @interface Initialized {
@Nonbinding int value() default 0; // int value will be store here
}
您必须在 bean 上添加此限定符并在创建时分析 InjectionPoint。
@Initialized
public class MyNumber {
int number;
private int extractValue(InjectionPoint ip) {
for (Annotation annotation : ip.getQualifiers()) {
if (annotation.annotationType().equals(Initialized.class))
return ((Initialized) annotation).value();
}
throw new IllegalStateException("No @Initialized on InjectionPoint");
}
@Inject
public MyNumber(InjectionPoint ip) {
this.number = extractValue(ip);
}
public String toString() {
return "Your number is: " + number;
}
}
您现在可以像这样注入(inject)一个初始化数字:
@Inject
@Initialized(8)
MyNumber number;
如果你想在运行时决定初始化值,你将不得不使用编程查找:
首先为“@Initialized”创建注释文字
public class InitializedLiteral extends AnnotationLiteral<Initialized> implements Initialized {
private int value;
public InitializedLiteral(int value) {
this.value = value;
}
@Override
public int value() {
return value;
}
}
然后您可以使用 Instance 来创建您的 bean。
public class ConsumingBean {
@Inject
@Any
Instance<MyNumber> myNumberInstance;
public MyNumber getMyNumberBeanFor(int value) {
return myNumberInstance.select(new InitializedLiteral(value)).get();
}
...
}
请记住,只有当 MyNumber 处于依赖范围内时,这才有效,因为这是在每次注入(inject)时更改初始化值的唯一方法。
关于java - 将参数传递给 @Inject Bean 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22994716/
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案