[英]Unsatisfied dependencies for type "Interface" with qualifiers @Default at injection point [BackedAnnotatedField] @Inject "Implementation"
I want to use in maven project an implementation of CDI+JSF+HIBERNATE+PRIMEFACES.我想在 maven 项目中使用 CDI+JSF+HIBERNATE+PRIMEFACES 的实现。 my pom.xml is like this:
我的 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>
The structure of my project is like this, I use a generic interface when I implement in different Class.我的项目的结构是这样的,我在不同的类中实现时使用了一个通用接口。 One interface that I implement in 3 classes IGenericDAO I implement it in UserDao + PersonDao + RoleDao like this
我在 3 个类中实现的一个接口 IGenericDAO 我在 UserDao + PersonDao + RoleDao 中实现它,就像这样
public interface IGenericDAO<T> {
void save(T object);
void update(T object);
T getObjectById(int id);
List<T> getAll(); }
the implementation is实施是
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();
}
}}
like this I code the Service to, I use a Generic Interface and I implement the code in 3 classes separetaded like this:像这样我对服务进行编码,我使用通用接口,并在 3 个类中实现代码,如下所示:
public interface IGenericService<T> {
void save(T object);
void update(T object);
T getObjectById(int id);
List<T> getAll(); }
the implementation实施
@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();
}}
And finally, the ManagedBean is like this:最后,ManagedBean 是这样的:
@Named(value = "authentication")
@RequestScoped public class AuthenticationBean implements Serializable { @RequestScoped 公共类 AuthenticationBean 实现了 Serializable {
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();
}}
Everytime I run the glassfish server i have this error : Exception 0 : org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type IGenericDAO with qualifiers @Default at injection point [BackedAnnotatedField] @Inject private ma.salaried.service.impl.PaymentServiceImpl.dao每次我运行 glassfish 服务器时,我都会遇到此错误:异常 0:org.jboss.weld.exceptions.DeploymentException:WELD-001408:在注入点 [BackedAnnotatedField] @Inject private ma.salaried 处,带有限定符 @Default 的 IGenericDAO 类型的不满意依赖项。 service.impl.PaymentServiceImpl.dao
I solved this problem by adding @RequestScoped in implementation of DAO and Implementation od Service Like this (In all my DAO IMPL and Service Impl)我通过在 DAO 的实现和实现 od 服务中添加 @RequestScoped 解决了这个问题(在我所有的 DAO IMPL 和 Service 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();
}
}
} }
and in service like this并像这样服务
@RequestScoped public class EmployeeServiceImpl implements IGenericService { @RequestScoped 公共类 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();
}}
I have met the same error, but with Wildfly and ejb functionality.我遇到了同样的错误,但有 Wildfly 和 ejb 功能。 The reason was - in
web.xml
file there was metadata-complete="true"
attribute.原因是 - 在
web.xml
文件中有metadata-complete="true"
属性。 With this attribute set to false the server does not scan ejb and servlet annotations.将此属性设置为 false 时,服务器不会扫描 ejb 和 servlet 注释。 All functionality, managed with them not working.
所有功能,管理与他们不工作。
What is interesting - cdi annotations do work correctly.有趣的是 - cdi 注释确实可以正常工作。
Solution: either remove it, or set to false.解决方案:要么删除它,要么设置为false。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.