gpt4 book ai didi

java - 使用 CDI 注入(inject)命名字符串

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:21:14 27 4
gpt4 key购买 nike

我想以这种方式注入(inject)一个配置参数:

public class MyManagedBean {
@Inject
public MyManagedBean(@Named("user") String user){
....

}
}

所以我尝试以这种方式实现生产者方法:

@ApplicationScoped
public class MyConfiguration {
private Properties loadProperties() {
Properties properties = new Properties();
try {
properties.load(getClass().getResourceAsStream(
"user.properties"));
} catch (IOException e) {
throw new RuntimeException();
}
return properties;
}

@Produces
@Named("user")
String getUser() {
return loadProperties().getProperty("user");
}
}

我以这种方式定义了其他 bean:

public class OtherManagedBean {
@Inject
public OtherManagedBean(MyManagedBean myManagedBean){
....

}
}

但是,当我尝试部署它时出现此异常:

INFO: WEB0671: Loading application [example-ear#example-war.war] at [example]
SEVERE: Exception while loading the app
SEVERE: Exception while loading the app : WELD-001410 The injection point [parameter 1] of [constructor] @Inject public com.example.OtherManagedBean(MyManagedBean) has non-proxyable dependencies
org.jboss.weld.exceptions.UnproxyableResolutionException: WELD-001410 The injection point [parameter 1] of [constructor] @Inject public com.example.OtherManagedBean(MyManagedBean) has non-proxyable dependencies
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:317)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:139)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:162)
at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:385)
at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:371)
at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:390)
at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:190)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:128)
at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:298)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:461)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:370)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:355)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:370)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1067)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:96)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1247)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1235)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:465)
at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:222)
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:168)
at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:234)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)

有什么想法吗?

谢谢。

最佳答案

看起来你需要一个 default (no-arg) constructor为您的 MyManagedBean 使其可代理。我不太确定为什么需要它,因为 MyManagedBean 是一个 @Dependent bean,所以不是代理 AFAIK; 我什至不知道为什么要代理老实说,bean 需要一个默认构造函数 这似乎是一个实现细节或 CDI 规范中被忽略的一点。无论如何,我敢打赌它可以提出一个很好的新问题 :)

编辑:我发现了为什么可代理 bean 需要默认构造。实际上,这并不神秘:因为 bean 的代理是同一 bean 的子类,所以代理需要在其自己的构造中调用 super() 构造函数。如果它没有非私有(private)的默认构造函数,它就不知道要调用哪个构造函数。人们甚至可以想象这样一种场景,调用注入(inject)的构造函数来创建代理,但我不知道它会给 CDI 实现增加什么样的复杂性;做起来可能不是那么容易...

关于java - 使用 CDI 注入(inject)命名字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7583871/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com