WELD-001408: Unsatisfied dependencies for type Customer with qualifiers @Default

mike128 picture mike128 · Feb 5, 2015 · Viewed 109.9k times · Source

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.

My code is as follows: Customer.java:

package de.java2enterprise.onlineshop.model;

public class Customer {
    private String email;
    private String password;
}

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

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.

Answer

Antoine Sabot-Durand picture Antoine Sabot-Durand · Feb 6, 2015

Your Customer class has to be discovered by CDI as a bean. For that you have two options:

  1. 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

  2. Change the bean discovery mode in your bean archive from the default "annotated" to "all" by adding a beans.xml file in your jar.

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.