注意:我已经尝试过类似问题的可用解决方案。
我正在使用JSF,Java 8,Tomcat 9,Maven,H2 db开发一个简单的CRUD应用程序。 当我尝试启动服务器时,发生以下异常
由以下原因引起:org.jboss.weld.exceptions.DeploymentException:WELD-001408: 带有限定符@Default的ContactsDAO类型的依赖关系不令人满意 在注入点[BackedAnnotatedParameter]的参数1 [BackedAnnotatedConstructor] @Inject公共 com.contacts.ContactsController(ContactsDAO)
当我从ContactsController的构造函数中删除@Inject批注时,异常消失了,但是contactsDao为空,因此我无法获取联系人列表。
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>