我目前正在尝试遵循文档以在Java SE中使用DeltaSpike,并制作了一个简单的界面进行测试,但我似乎做错了。
根据此处的文档,我应该能够进行基于接口的配置:https://deltaspike.apache.org/documentation/configuration.html#_interface_based_configuration
@Configuration(prefix = "application.")
public interface AppConfig {
@ConfigProperty(name = "name", evaluateVariables = false)
String getApplicationName();
}
我尝试通过BeanProvider#getContextualReference
和@Inject
都使用它。
@Inject
public Commandler(final AppContext context, final BeanManager beanManager, AppConfig app) {
this.appContext = context;
this.beanManager = beanManager;
logger.info("Initialization application with name {}.", app.getApplicationName());
logger.info("Loaded all configuration and dependencies in {}.", appContext.getTimeSinceStartupFormatted());
}
Exception in thread "main" org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type AppConfig with qualifiers @Default
我也收到警告:
不满意的依赖关系:没有一个bean与注入点匹配
我已经尝试摆弄build.gradle中的beans.xml
文件和依赖项,但无济于事,与文档相比,我不确定自己在做什么错。
有人可以指出正确的方向吗?
我找到了解决问题的方法,实际上不是使用DeltaSpike,而是使用Weld,这是我在幕后使用的。
问题和解决方案在这里得到了很好的记录:https://discuss.gradle.org/t/application-plugin-run-task-should-first-consolidate-classes-and-resources-folder-or-depend-on-installapp-or-stuff-like-weld-se-wont-work/1248
如果链接消失,我将在此处进行总结,此问题是由Gradle将main
和resource
分开构建引起的,因此首先需要修改资源输出以将它们放在一起,如下所示:
sourceSets {
main {
output.resourcesDir = output.classesDirs.singleFile
}
test {
output.resourcesDir = output.classesDirs.singleFile
}
}
这只是将资源输出设置到classes目录。
现在,我们需要使用Gradle而不是IDE运行应用程序,我们添加了application
插件。
plugins {
id "application"
id "java"
id "io.spring.dependency-management" version "1.0.9.RELEASE"
}
现在有了application
插件,您还需要指定主类。
application {
mainClassName = "org.elypia.deltaspike.Main"
}
然后使用application
插件,执行run
,您应该一切顺利。
如果使用的是Deltaspike批注,您可能需要更改beans.xml
才能找到all
,而不仅仅是annotated
,因为您可能使用的某些批注可能不是标准的CDI批注。