当前位置: 首页 > 知识库问答 >
问题:

类型"接口"与限定符@Default在注入点[BackedAnNotatedField]@Inject"实现"的不满足的依赖项

皇甫心思
2023-03-14

我想在maven项目中使用CDI JSF HIBERNATE PRIMEFACES的一个实现。我的pom。xml是这样的:

    <dependencies>
    <!-- PROJECT LOMBOK -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.6</version>
        <scope>provided</scope>
    </dependency>
    <!-- MYSQL -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.9</version>
    </dependency>
    <!-- HIBERNATE -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.3.Final</version>
    </dependency>
    <!-- CDI -->
    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>1.2</version>
    </dependency>
    <!-- SLF4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.12</version>
    </dependency>
    <!-- PRIMEFACES -->
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>5.2</version>
    </dependency>
</dependencies>

我的项目的结构是这样的,当我在不同的类中实现时,我使用一个通用接口。我在3个类IGenericDAO中实现的一个接口,我在UserDao PersonDao RoleDao中实现它,如下所示

public interface IGenericDAO<T> {

void save(T object);

void update(T object);

T getObjectById(int id);

List<T> getAll();  }

实现是

public class UserDAOImpl implements IGenericDAO<User> {

@Override
public void save(User user) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        session.save(user);
        transaction.commit();
    } catch (Exception e) {
        if (transaction != null)
            transaction.rollback();
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}

@Override
public void update(User user) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        session.update(user);
        transaction.commit();
    } catch (Exception e) {
        if (transaction != null)
            transaction.rollback();
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}

@Override
public User getObjectById(int id) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        return (User) session.createCriteria(User.class).add(Restrictions.eq("id", id)).list().get(0);
    } catch (Exception e) {
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}

@SuppressWarnings("unchecked")
@Override
public List<User> getAll() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        return session.createCriteria(User.class).list();
    } catch (Exception e) {
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}}

像这样,我将服务编码为,我使用通用接口,并在3个类中实现代码,如下所示:

public interface IGenericService<T> {

void save(T object);

void update(T object);

T getObjectById(int id);

List<T> getAll(); }

实施

@RequestScoped
public class UserServiceImpl implements IGenericService<User> {
@Setter
@Inject
private IGenericDAO<User> dao;

@Override
public void save(User user) {
    dao.save(user);
}

@Override
public void update(User user) {
    dao.update(user);
}

@Override
public User getObjectById(int id) {
    return dao.getObjectById(id);
}

@Override
public List<User> getAll() {
    return dao.getAll();
}}

最后,ManagedBean是这样的:

@Named(value = "authentication")

@RequestScope公共类AuthenticationBean实现可序列化{

private static final long serialVersionUID = 2288439665206779666L;

@Getter
@Setter
private String message;

@Setter
@Inject
private IGenericService<User> userService;

@PostConstruct
public void init() {
    message = userService.getObjectById(1).getLastname();
}}

每次我运行glassfish服务器时,我都有这个错误:异常0:org.jboss.weld.exceptions.部署异常: WELD-001408:类型IGenericDAO的未满足的依赖关系,带有限定符@默认在注入点[BackedAnNotatedField]@注入私有ma.salaried.service.impl.PaymentServiceImpl.dao

共有2个答案

温开畅
2023-03-14

我也遇到了同样的错误,但是使用了Wildfly和ejb功能。原因是——在web中。xml文件中有元数据complete=“true”属性。当该属性设置为false时,服务器不会扫描ejb和servlet注释。所有功能,在它们不工作的情况下进行管理。

有趣的是——cdi注释确实工作正常。

解决方法:要么删除它,要么设置为false。

司徒炎彬
2023-03-14

我通过在DAO的实现中添加@RequestScoped和这样的实现od服务(在我所有的DAO IMPL和服务IMPL中)解决了这个问题

@RequestScoped public class EmployeeDAOImpl implements IGenericDAO<Employee> {

@Override
public void save(Employee Employee) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        session.save(Employee);
        transaction.commit();
    } catch (Exception e) {
        if (transaction != null)
            transaction.rollback();
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}

@Override
public void update(Employee Employee) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        session.update(Employee);
        transaction.commit();
    } catch (Exception e) {
        if (transaction != null)
            transaction.rollback();
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}

@Override
public Employee getObjectById(int id) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        return (Employee) session.createCriteria(Employee.class).add(Restrictions.eq("id", id)).list().get(0);
    } catch (Exception e) {
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}

@SuppressWarnings("unchecked")
@Override
public List<Employee> getAll() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        return session.createCriteria(Employee.class).list();
    } catch (Exception e) {
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}

}

像这样服役

@RequestScope公共类EmployeeServiceImpl实现IGenericService{

@Setter
@Inject
private IGenericDAO<Employee> dao;

@Override
public void save(Employee Employee) {
    dao.save(Employee);
}

@Override
public void update(Employee Employee) {
    dao.update(Employee);
}

@Override
public Employee getObjectById(int id) {
    return dao.getObjectById(id);
}

@Override
public List<Employee> getAll() {
    return dao.getAll();
}}
 类似资料: