问题描述
我是 Java EE 新手.我想测试JSF,因此做了一个简单的程序,但无法部署它.我收到以下错误消息:
I'm a Java EE-newbie. I want to test JSF and therefore made a simple program but can not deploy it. I get the following error message:
cannot Deploy onlineshop-war
deploy is failing=Error occurred during deployment: Exception while loading the app : CDI deployment failure:WELD-001408: Unsatisfied dependencies for type Customer with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private de.java2enterprise.onlineshop.RegisterController.customer
at de.java2enterprise.onlineshop.RegisterController.customer(RegisterController.java:0)
. Please see server.log for more details.
我的代码如下:客户.java:
My code is as follows:Customer.java:
package de.java2enterprise.onlineshop.model;
public class Customer {
private String email;
private String password;
}
registerController.java:
registerController.java:
package de.java2enterprise.onlineshop;
import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import javax.inject.Inject;
import de.java2enterprise.onlineshop.model.*;
@Named
@RequestScoped
public class RegisterController {
private static final long serialVersionUID = 1L;
@Inject
private Customer customer;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public String persist() {
return "/index.xhtml";
}
}
为了编译它,我必须包含 cdi-api.jar 作为外部库.这里有人可以帮助我吗?提前谢谢大家.
For compiling it I had to include cdi-api.jar as an external library. Anyone here who could help me? Thank you all in advance.
推荐答案
您的 Customer
类必须作为 bean 被 CDI 发现.为此,您有两个选择:
Your Customer
class has to be discovered by CDI as a bean. For that you have two options:
在上面放一个bean定义注释.由于
@Model
是一种刻板印象,这就是它起作用的原因.像@Named
这样的限定符不是定义注释的 bean,它不起作用的原因
Put a bean defining annotation on it. As
@Model
is a stereotype it's why it does the trick. A qualifier like@Named
is not a bean defining annotation, reason why it doesn't work
更改 bean 存档中的 bean 发现模式通过在 jar 中添加 beans.xml
文件,将默认注释"为全部".
Change the bean discovery mode in your bean archive from the default "annotated" to "all" by adding a beans.xml
file in your jar.
请记住,@Named 只有一种用法:将您的 bean 暴露给 UI.其他用法用于不良做法或与旧框架兼容.
Keep in mind that @Named has only one usage : expose your bean to the UI. Other usages are for bad practice or compatibility with legacy framework.
这篇关于WELD-001408:带有限定符 @Default 的类型 Customer 的不满意依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!