简体   繁体   中英

JSF - org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type

Note: I have already tried the solutions available in similar question on SO.

I am working on a simple CRUD application using JSF, Java 8, Tomcat 9, Maven, H2 db. When I am trying to start the server, following exception occurs

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type ContactsDAO with qualifiers @Default at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject public com.contacts.ContactsController(ContactsDAO)

When I remove @Inject annotation from the constructor of ContactsController, the exception is gone but the contactsDao is null hence I am unable to get the list of contacts.

ContactsController.java

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
@Named("contactsController")
@SessionScoped
public class ContactsController implements Serializable {
public ContactsController() {
    }

    @Inject
    public ContactsController(ContactsDAO contactDAO) {
        this.contactDAO = contactDAO;
    }
@PostConstruct
    public void init() {

        logger.info("retrieving list of contacts");

        try {
            if(contactDAO != null) {
                System.out.println("ifff");
                contacts = contactDAO.getAllContacts();
            }
            else {
                System.out.println("Contacts DAO null");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

ContactsDAO.java

public interface ContactsDAO {
List<Contact> getAllContacts() throws SQLException;
}

ContactsDAOImpl.java

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

@Named
@ApplicationScoped
public class ContactsDAOImpl implements ContactsDAO, Serializable{
@Override
    public List<Contact> getAllContacts() throws SQLException {
        List<Contact> contacts = new ArrayList<>();
        try {
            String sql = "SELECT * FROM contacts";
            connect();

            PreparedStatement pstmt = jdbcConnection.prepareStatement(sql);

            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Contact newContact = new Contact(); 
                newContact.setId(rs.getInt("id"));

                contacts.add(newContact);
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        return contacts;
    }
}

WEB-INF/beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="annotated">
</beans>

Tomcat does not support CDI. Look at this post from @BalusC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM