我的第一个Java EE应用程序出了问题。我使用Wildfly 8.1服务器和Netbeans 我的应用程序由Entity类(SocialUser.java)组成。 :
package com.mycompany.entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "soc_user")
public class SocialUser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String login;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof SocialUser)) {
return false;
}
SocialUser other = (SocialUser) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.mycompany.entity.NewEntity[ id=" + id + " ]";
}
/**
* @return the login
*/
public String getLogin() {
return login;
}
/**
* @param login the login to set
*/
public void setLogin(String login) {
this.login = login;
}
}
Contrroller类(UserMB.java):
package com.mycompany;
import com.mycompany.service.CRUD;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
@Named
@RequestScoped
public class UserMB {
@Inject
private CRUD crud;
private String login;
public UserMB() {
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public void save(){
crud.businessMethod();
}
}
和CRUD.java类做业务逻辑:
package com.mycompany.service;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
@LocalBean
public class CRUD {
@PersistenceContext(unitName = "SocialPU")
private EntityManager em;
public void businessMethod() {
em.persist(this);
System.out.println("zapisano");//this is for checking
}
}
我有这个错误:
Request
{
"address" => [("deployment" => "Praktyki-SocialPage-ear-1.0-SNAPSHOT.ear")],
"operation" => "deploy"
}
Response
Internal Server Error
{
"outcome" => "failed",
"failure-description" => {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"Praktyki-SocialPage-ear-1.0-SNAPSHOT.ear\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"Praktyki- SocialPage-ear-1.0-SNAPSHOT.ear\".WeldStartService: Failed to start service
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type CRUD with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private com.mycompany.UserMB.crud
at com.mycompany.UserMB.crud(UserMB.java:0)
"}},
"rolled-back" => true
}
代码似乎没问题。我在META-INF文件夹中也有beans.xml文件。 我试着找到答案,但我找不到符合我案例的答案。 你能解释一下我的原因吗?
答案 0 :(得分:0)
您的类CRUD
是EJB,而您尝试使用CDI注入它(@Inject
在EE容器中使用CDI)。将您的代码更改为:
Contrroller类(UserMB.java):
@Named
@RequestScoped
public class UserMB {
@EJB
private CRUD crud;