2017-08-02 133 views
0

我在當前正在處理的項目上添加了一些更改。我有一個用戶類和3個不同的配置文件(管理員,docteur,病人)。org.springframework.beans.factory.BeanCreationException:創建名爲'appConfig'的bean時出錯:注入自動裝配依賴關係失敗

我已經通過添加兩個用戶子類(Docteur和Patient)更改了項目的結構,並且這些配置文件是User或admin。

用戶

package com.vivalio.springmvc.model; 

import java.io.Serializable; 
import java.util.HashSet; 
import java.util.Set; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.JoinTable; 
import javax.persistence.ManyToMany; 
import javax.persistence.ManyToOne; 
import javax.persistence.OneToMany; 
import javax.persistence.OneToOne; 
import javax.persistence.Table; 

import org.hibernate.validator.constraints.NotEmpty; 

    @Entity 
    @Table(name = "USER") 
    public class User implements Serializable { 

     @Id 
     @GeneratedValue(strategy = GenerationType.IDENTITY) 
     @Column(name = "id") 
     private Integer id; 

     @NotEmpty 
     @Column(name = "SSO_ID", unique = true, nullable = false) 
     private String ssoId; 

     @NotEmpty 
     @Column(name = "PASSWORD", nullable = false) 
     private String password; 

     @NotEmpty 
     @Column(name = "FIRST_NAME", nullable = false) 
     private String firstName; 

     @NotEmpty 
     @Column(name = "LAST_NAME", nullable = false) 
     private String lastName; 

     @NotEmpty 
     @Column(name = "EMAIL", nullable = false) 
     private String email; 

     @Column(name = "DTCREATION", nullable = true) 
     private String dateCreation; 

     @Column(name = "JJCREATION", nullable = true) 
     private String jjCreation; 

     public String getJjCreation() { 
      return jjCreation; 
     } 

     public void setJjCreation(String jjCreation) { 
      this.jjCreation = jjCreation; 
     } 

     @Column(name = "MMCREATION", nullable = true) 
     private String mmCreation; 

     public String getMmCreation() { 
      return mmCreation; 
     } 

     public void setMmCreation(String mmCreation) { 
      this.mmCreation = mmCreation; 
     } 

     @Column(name = "YYCREATION", nullable = true) 
     private String aaCreation; 

     public String getAaCreation() { 
      return aaCreation; 
     } 

     public void setAaCreation(String aaCreation) { 
      this.aaCreation = aaCreation; 
     } 

     public String getDateCreation() { 
      return dateCreation; 
     } 

     public void setDateCreation(String dateCreation) { 
      this.dateCreation = dateCreation; 
     } 

     @NotEmpty 
     @ManyToMany(fetch = FetchType.LAZY) 
     @JoinTable(name = "USER_USER_PROFILE", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { 
       @JoinColumn(name = "USER_PROFILE_ID") }) 
     private Set<UserProfile> userProfiles = new HashSet<UserProfile>(); 

     public Integer getId() { 
      return id; 
     } 

     public void setId(Integer id) { 
      this.id = id; 
     } 

     public String getSsoId() { 
      return ssoId; 
     } 

     public void setSsoId(String ssoId) { 
      this.ssoId = ssoId; 
     } 

     public String getPassword() { 
      return password; 
     } 

     public void setPassword(String password) { 
      this.password = password; 
     } 

     public String getFirstName() { 
      return firstName; 
     } 

     public void setFirstName(String firstName) { 
      this.firstName = firstName; 
     } 

     public String getLastName() { 
      return lastName; 
     } 

     public void setLastName(String lastName) { 
      this.lastName = lastName; 
     } 

     public String getEmail() { 
      return email; 
     } 

     public void setEmail(String email) { 
      this.email = email; 
     } 

     public Set<UserProfile> getUserProfiles() { 
      return userProfiles; 
     } 

     public void setUserProfiles(Set<UserProfile> userProfiles) { 
      this.userProfiles = userProfiles; 
     } 

     @Override 
     public int hashCode() { 
      final int prime = 31; 
      int result = 1; 
      result = prime * result + ((id == null) ? 0 : id.hashCode()); 
      result = prime * result + ((ssoId == null) ? 0 : ssoId.hashCode()); 
      return result; 
     } 

     @Override 
     public boolean equals(Object obj) { 
      if (this == obj) 
       return true; 
      if (obj == null) 
       return false; 
      if (!(obj instanceof User)) 
       return false; 
      User other = (User) obj; 
      if (id == null) { 
       if (other.id != null) 
        return false; 
      } else if (!id.equals(other.id)) 
       return false; 
      if (ssoId == null) { 
       if (other.ssoId != null) 
        return false; 
      } else if (!ssoId.equals(other.ssoId)) 
       return false; 
      return true; 
     } 

     @Override 
     public String toString() { 
      return "User [id=" + id + ", ssoId=" + ssoId + ", password=" + password + ", firstName=" + firstName 
        + ", lastName=" + lastName + ", email=" + email + "]"; 
     } 

    } 

法學博士

package com.vivalio.springmvc.model; 

    import java.io.Serializable; 
    import java.util.HashSet; 
    import java.util.Set; 

    import javax.persistence.Column; 
    import javax.persistence.Entity; 
    import javax.persistence.GeneratedValue; 
    import javax.persistence.GenerationType; 
    import javax.persistence.Id; 
    import javax.persistence.OneToMany; 
    import javax.persistence.Table; 

    @Entity 
    @Table(name = "DOCTEUR") 
    public class Docteur extends User implements Serializable { 

     @Id 
     @GeneratedValue(strategy = GenerationType.IDENTITY) 
     @Column(name = "id") 
     private Integer id; 

     @OneToMany(mappedBy = "doc") 
     private Set<Patient> patients = new HashSet<Patient>(0); 

     public Set<Patient> getPatients() { 
      return patients; 
     } 

     public void setPatients(Set<Patient> patients) { 
      this.patients = patients; 
     } 

     public Integer getId() { 
      return id; 
     } 

     public void setId(Integer id) { 
      this.id = id; 
     } 

    } 

病人

package com.vivalio.springmvc.model; 

import java.io.Serializable; 
import java.util.HashSet; 
import java.util.Set; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 

@Entity 
@Table(name = "PATIENT") 
public class Patient extends User implements Serializable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "id") 
    private Integer id; 

    @OneToMany(mappedBy = "patient") 
    private Set<Consultation> consultations = new HashSet<Consultation>(0); 

    @ManyToOne 
    @JoinColumn(name = "id") 
    private Docteur doc; 

    public Docteur getDoc() { 
     return doc; 
    } 

    public void setDoc(Docteur doc) { 
     this.doc = doc; 
    } 

    public Set<Consultation> getConsultations() { 
     return consultations; 
    } 

    public void setConsultations(Set<Consultation> consultations) { 
     this.consultations = consultations; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

} 

轉換

package com.vivalio.springmvc.converter; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.core.convert.converter.Converter; 
import org.springframework.stereotype.Component; 

import com.vivalio.springmvc.model.UserProfile; 
import com.vivalio.springmvc.service.UserProfileService; 

/** 
* A converter class used in views to map id's to actual userProfile objects. 
*/ 
@Component 
public class RoleToUserProfileConverter implements Converter<Object, UserProfile>{ 

    static final Logger logger = LoggerFactory.getLogger(RoleToUserProfileConverter.class); 

    @Autowired 
    UserProfileService userProfileService; 

    /** 
    * Gets UserProfile by Id 
    * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object) 
    */ 
    public UserProfile convert(Object element) { 
     Integer id = Integer.parseInt((String)element); 
     UserProfile profile= userProfileService.findById(id); 
     logger.info("Profile : {}",profile); 
     return profile; 
    } 
} 

控制器

package com.vivalio.springmvc.controller; 

import java.util.Calendar; 
import java.util.List; 
import java.util.Locale; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 
import javax.validation.Valid; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.MessageSource; 
import org.springframework.security.authentication.AuthenticationTrustResolver; 
import org.springframework.security.core.Authentication; 
import org.springframework.security.core.context.SecurityContextHolder; 
import org.springframework.security.core.userdetails.UserDetails; 
import org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.validation.BindingResult; 
import org.springframework.validation.FieldError; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.SessionAttributes; 

import com.vivalio.springmvc.model.Consultation; 
import com.vivalio.springmvc.model.User; 
import com.vivalio.springmvc.model.UserProfile; 
import com.vivalio.springmvc.model.UserProfileType; 
import com.vivalio.springmvc.service.AppetitService; 
import com.vivalio.springmvc.service.BouleService; 
import com.vivalio.springmvc.service.ConsultationService; 
import com.vivalio.springmvc.service.DocteurService; 
import com.vivalio.springmvc.service.DouleurService; 
import com.vivalio.springmvc.service.FaiblesseService; 
import com.vivalio.springmvc.service.PatientService; 
import com.vivalio.springmvc.service.UserProfileService; 
import com.vivalio.springmvc.service.UserService; 
import com.vivalio.springmvc.utils.Mail; 

@Controller 
@RequestMapping("/") 
@SessionAttributes("roles") 
public class AppController { 

    @Autowired 
    UserService userService; 

    @Autowired 
    UserProfileService userProfileService; 

    @Autowired 
    AppetitService appetitService; 

    @Autowired 
    FaiblesseService faiblesseService; 

    @Autowired 
    DouleurService douleurService; 

    @Autowired 
    BouleService bouleService; 

    @Autowired 
    MessageSource messageSource; 

    @Autowired 
    PersistentTokenBasedRememberMeServices persistentTokenBasedRememberMeServices; 

    @Autowired 
    AuthenticationTrustResolver authenticationTrustResolver; 

    @Autowired 
    ConsultationService consultationService; 

    @Autowired 
    PatientService patientService; 

    @Autowired 
    DocteurService docteurServices; 

    /** 
    * This method will list all existing users. 
    */ 
    @RequestMapping(value = { "/", "/list" }, method = RequestMethod.GET) 
    public String listUsers(ModelMap model) { 

     List<User> users = userService.findAllUsers(); 
     model.addAttribute("users", users); 
     model.addAttribute("loggedinuser", getPrincipal()); 
     return "userslist"; 
    } 

    /** 
    * This method will provide the medium to add a new user. 
    */ 
    @RequestMapping(value = { "/newuser" }, method = RequestMethod.GET) 
    public String newUser(ModelMap model) { 
     User user = new User(); 
     model.addAttribute("user", user); 
     model.addAttribute("edit", false); 
     model.addAttribute("loggedinuser", getPrincipal()); 
     return "registration"; 
    } 


    @RequestMapping(value = { "/newconsultation" }, method = RequestMethod.GET) 
    public String newConsultation(ModelMap model) { 
     Consultation consultation = new Consultation(); 
     model.addAttribute("consultation", consultation); 
     model.addAttribute("edit", false); 
     model.addAttribute("loggedinuser", getPrincipal()); 
     return "registrationConsultation"; 
    } 

    @RequestMapping(value = { "/newuser" }, method = RequestMethod.POST) 
    public String saveUser(@Valid User user, BindingResult result, ModelMap model) { 

     if (result.hasErrors()) { 
      return "registration"; 
     } 

     if (!userService.isUserSSOUnique(user.getId(), user.getSsoId())) { 
      FieldError ssoError = new FieldError("user", "ssoId", messageSource.getMessage("non.unique.ssoId", 
        new String[] { user.getSsoId() }, Locale.getDefault())); 
      result.addError(ssoError); 
      return "registration"; 
     } 

     userService.saveUser(user); 
     Mail.sendMailRegistration("[email protected]", user.getEmail(), "[email protected]", 
       "[email protected]", "Creation de compte vivalio", "message"); 

     model.addAttribute("success", 
       "User " + user.getFirstName() + " " + user.getLastName() + " registered successfully"); 
     model.addAttribute("loggedinuser", getPrincipal()); 
     // return "success"; 
     return "redirect:/list"; 
    } 

    /* ajouter la consultation du patient */ 

    @RequestMapping(value = "/newconsultation", method = RequestMethod.POST) 
    public String saveConsultation(@Valid Consultation consultation, BindingResult result, ModelMap model) { 

     if (result.hasErrors()) { 
      return "registrationConsultation"; 
     } 
     // Patient patient = (Patient) getUser(); 
     consultation.setPatient(null); 
     consultationService.saveConsultation(consultation); 

     if (consultation != null && consultation.getPatient() != null) { 
      Mail.sendMailAfterConsultation("[email protected]", consultation.getPatient().getEmail(), 
        "[email protected]", "[email protected]", "Envoi de la consultation a votre medecin : ", 
        "Votre consultation REF-" + consultation.getId() 
          + " a ete envoyee a votre medecin avec succes. Vous serez contacte en cas d'urgence.<br/>Ci dessous un recap de votre consultation :<br/>" 
          + "<br/>Poid : " + consultation.getParam1() + "<br/>" + "Appetit :" 
          + consultation.getParam2() + "<br/>" + "Faiblesse :" + consultation.getParam3() + "<br/>" 
          + "Douleur :" + consultation.getParam4() + "<br/>" + "Boule :" + consultation.getParam5() 
          + "<br/>" + "Fievre :" + consultation.getParam6() + "<br/>" + "Commentaire :" 
          + consultation.getCommentaire() + "<br/>" + "<br/>L'equipe vivalio Group" 

      ); 
     } 

     model.addAttribute("param1", consultation.getParam1()); 
     model.addAttribute("param2", consultation.getParam2()); 
     model.addAttribute("param3", consultation.getParam3()); 

     model.addAttribute("param4", consultation.getParam4()); 
     model.addAttribute("param5", consultation.getParam5()); 
     model.addAttribute("param6", consultation.getParam6()); 
     model.addAttribute("commentaire", consultation.getCommentaire()); 

     return "redirect:/list"; 
    } 

    /** 
    * This method will provide the medium to update an existing user. 
    */ 
    @RequestMapping(value = { "/edit-user-{ssoId}" }, method = RequestMethod.GET) 
    public String editUser(@PathVariable String ssoId, ModelMap model) { 
     User user = userService.findBySSO(ssoId); 
     model.addAttribute("user", user); 
     model.addAttribute("edit", true); 
     model.addAttribute("loggedinuser", getPrincipal()); 
     return "registration"; 
    } 

    @RequestMapping(value = { "/edit-user-{ssoId}" }, method = RequestMethod.POST) 
    public String updateUser(@Valid User user, BindingResult result, ModelMap model, @PathVariable String ssoId) { 

     if (result.hasErrors()) { 
      return "registration"; 
     } 

     userService.updateUser(user); 
     Mail.sendMailRegistration("[email protected]", user.getEmail(), "[email protected]", 
       "[email protected]", "Modification des informations vivalio", 
       "Votre utilisateur vivalio a été modifié"); 

     model.addAttribute("success", 
       "User " + user.getFirstName() + " " + user.getLastName() + " updated successfully"); 
     model.addAttribute("loggedinuser", getPrincipal()); 
     return "redirect:/list"; 
    } 

    /** 
    * This method will delete an user by it's SSOID value. 
    */ 
    @RequestMapping(value = { "/delete-user-{ssoId}" }, method = RequestMethod.GET) 
    public String deleteUser(@PathVariable String ssoId) { 
     userService.deleteUserBySSO(ssoId); 
     return "redirect:/list"; 
    } 

    /** 
    * This method will provide UserProfile list to views 
    */ 
    @ModelAttribute("roles") 
    public List<UserProfile> initializeProfiles() { 
     return userProfileService.findAll(); 
    } 

    /** 
    * This method will provide Docs list to views 
    */ 
    @ModelAttribute("docteurs") 
    public List<User> initializeAllDocs() { 
     return userService.findAllDocs(); 
    } 

    /** 
    * This method will provide Appetit list to views 
    */ 
    @ModelAttribute("appetits") 
    public List<String> initializeAppetits() { 
     return appetitService.findAll(); 
    } 

    /** 
    * This method will provide Faiblesse list to views 
    */ 
    @ModelAttribute("faiblesses") 
    public List<String> initializeFaiblesses() { 
     return faiblesseService.findAll(); 
    } 

    /** 
    * This method will provide Douleur list to views 
    */ 
    @ModelAttribute("douleurs") 
    public List<String> initializeDouleurs() { 
     return douleurService.findAll(); 
    } 

    /** 
    * This method will provide Boules list to views 
    */ 
    @ModelAttribute("boules") 
    public List<String> initializeBoules() { 
     return bouleService.findAll(); 
    } 

    /** 
    * This method handles Access-Denied redirect. 
    */ 
    @RequestMapping(value = "/Access_Denied", method = RequestMethod.GET) 
    public String accessDeniedPage(ModelMap model) { 
     model.addAttribute("loggedinuser", getPrincipal()); 
     return "accessDenied"; 
    } 


    @RequestMapping(value = "/login", method = RequestMethod.GET) 
    public String loginPage() { 
     if (isCurrentAuthenticationAnonymous()) { 
      return "login"; 
     } else { 
      return "redirect:/list"; 
     } 
    } 


    @RequestMapping(value = "/logout", method = RequestMethod.GET) 
    public String logoutPage(HttpServletRequest request, HttpServletResponse response) { 
     Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
     if (auth != null) { 
      // new SecurityContextLogoutHandler().logout(request, response, 
      // auth); 
      persistentTokenBasedRememberMeServices.logout(request, response, auth); 
      SecurityContextHolder.getContext().setAuthentication(null); 
     } 
     return "redirect:/login?logout"; 
    } 

    /** 
    * This method returns the principal[user-name] of logged-in user. 
    */ 
    private String getPrincipal() { 
     String userName = null; 
     Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 

     if (principal instanceof UserDetails) { 
      userName = ((UserDetails) principal).getUsername(); 
     } else { 
      userName = principal.toString(); 
     } 
     return userName; 
    } 

    @SuppressWarnings("rawtypes") 
    private User getUser() { 
     String username = null; 
     Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
     username = ((UserDetails) principal).getUsername(); 
     User usr = userService.findBySSO(username); 
     return usr; 

    } 

    /** 
    * This method returns true if users is already authenticated [logged-in], 
    * else false. 
    */ 
    private boolean isCurrentAuthenticationAnonymous() { 
     final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
     return authenticationTrustResolver.isAnonymous(authentication); 
    } 

    /** 
    * This method will redirect to dashboard 
    */ 
    @RequestMapping(value = { "/board" }, method = RequestMethod.GET) 
    public String tableauBord(ModelMap model, HttpSession session) { 

     List<User> users = userService.findAllUsers(); 

     // TOTAL des utilisateurs de la base 
     int totalAdmin = userService.findAllAdmins().size(); 
     int totalDocs = userService.findAllDocs().size(); 
     int totalPatients = userService.findAllPatients().size(); 
     // ANNEE COURANTE 
     int year = Calendar.getInstance().get(Calendar.YEAR); 
     StringBuilder sbyear = new StringBuilder(); 
     sbyear.append(year); 
     // RECHERCHER LES COMPTES PAR TYPE DE L'ANNEE EN COURS 
     List<Object[]> totalComptesAdminParMois = userService 
       .findAllUsersByTypeAndyear(UserProfileType.ADMINISTRATEUR.getUserProfileType(), sbyear.toString()); 
     List<Object[]> totalComptesDocParMois = userService 
       .findAllUsersByTypeAndyear(UserProfileType.ADMINISTRATEUR.getUserProfileType(), sbyear.toString()); 
     List<Object[]> totalComptesPatParMois = userService 
       .findAllUsersByTypeAndyear(UserProfileType.UTILISATEUR.getUserProfileType(), sbyear.toString()); 

     return "board"; 
    } 

} 

試圖重新啓動服務器時,我在報文對象錯誤。

的AppConfig

package com.vivalio.springmvc.configuration; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.MessageSource; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.support.ResourceBundleMessageSource; 
import org.springframework.format.FormatterRegistry; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; 
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
import org.springframework.web.servlet.view.InternalResourceViewResolver; 
import org.springframework.web.servlet.view.JstlView; 

import com.vivalio.springmvc.converter.RoleToUserProfileConverter; 


@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages = "com.vivalio.springmvc") 
public class AppConfig extends WebMvcConfigurerAdapter{ 


    @Autowired 
    RoleToUserProfileConverter roleToUserProfileConverter; 


    /** 
    * Configure ViewResolvers to deliver preferred views. 
    */ 
    @Override 
    public void configureViewResolvers(ViewResolverRegistry registry) { 

     InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
     viewResolver.setViewClass(JstlView.class); 
     viewResolver.setPrefix("/WEB-INF/views/"); 
     viewResolver.setSuffix(".jsp"); 
     registry.viewResolver(viewResolver); 
    } 

    /** 
    * Configure ResourceHandlers to serve static resources like CSS/ Javascript etc... 
    */ 
    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/static/**").addResourceLocations("/static/"); 
    } 

    /** 
    * Configure Converter to be used. 
    * In our example, we need a converter to convert string values[Roles] to UserProfiles in newUser.jsp 
    */ 
    @Override 
    public void addFormatters(FormatterRegistry registry) { 
     registry.addConverter(roleToUserProfileConverter); 
    } 


    /** 
    * Configure MessageSource to lookup any validation/error message in internationalized property files 
    */ 
    @Bean 
    public MessageSource messageSource() { 
     ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); 
     messageSource.setBasename("messages"); 
     return messageSource; 
    } 

    /**Optional. It's only required when handling '.' in @PathVariables which otherwise ignore everything after last '.' in @PathVaidables argument. 
    * It's a known bug in Spring [https://jira.spring.io/browse/SPR-6164], still present in Spring 4.1.7. 
    * This is a workaround for this issue. 
    */ 
    @Override 
    public void configurePathMatch(PathMatchConfigurer matcher) { 
     matcher.setUseRegisteredSuffixPatternMatch(true); 
    } 
} 

堆棧跟蹤

重度:上下文初始化失敗 org.springframework.beans.factory.BeanCreationException:錯誤 創建名爲 'AppConfig的' 豆:注入自動線 依賴關係失敗;嵌套的異常是 org.springframework.beans.factory.BeanCreationException:無法 自動裝配字段: com.vivalio.springmvc.converter.RoleToUserProfileConverter com.vivalio.springmvc.configuration.AppConfig.roleToUserProfileConverter; 嵌套的異常是 org.springframework.beans.factory.BeanCreationException:錯誤 創建名爲'roleToUserProfileConverter'的bean:注入 自動裝配的依賴關係失敗;嵌套的異常是 org.springframework.beans.factory.BeanCreationException:不能 autowire字段:com.vivalio.springmvc.service.UserProfileService com.vivalio.springmvc.converter.RoleToUserProfileConverter.userProfileService; 嵌套的異常是 org.springframework.beans.factory.BeanCreationException:錯誤 創建名爲'userProfileService'的bean:注入自動裝配的 依賴關係失敗;嵌套的異常是 org.springframework.beans.factory.BeanCreationException:不能 autowire字段:com.vivalio.springmvc.dao.UserProfileDao com.vivalio.springmvc.service.UserProfileServiceImpl.dao;嵌套0​​異常是org.springframework.beans.factory。BeanCreationException: 創建名爲'userProfileDao'的bean時出錯:注入自動裝配的 依賴關係失敗;嵌套的異常是 org.springframework.beans.factory.BeanCreationException:不能 autowire字段:private org.hibernate.SessionFactory com.vivalio.springmvc.dao.AbstractDao.sessionFactory;嵌套異常 是org.springframework.beans.factory.BeanCreationException:錯誤 創建具有名稱 '的sessionFactory' 類路徑限定 資源 [COM/vivalio /用SpringMVC /配置/ HibernateConfiguration.class]豆:init方法的 調用失敗;嵌套的異常是 java.lang.ClassCastException: org.hibernate.mapping.SingleTableSubclass不能在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java被強制轉換爲 org.hibernate.mapping.RootClass: 334)

+2

要了解問題 - 需要配置類和堆棧跟蹤。 –

+1

@badaboum,提供代碼,其中使用了appConfig bean以及bean定義代碼 –

+0

@ J-Alex我添加了堆棧跟蹤和配置類 – badaboum

回答

2

這是異常的根本原因:

org.hibernate.mapping.SingleTableSubclass cannot be cast to org.hibernate.mapping.RootClass 

這意味着你必須在這兩個孩子,父類@Id

從兩個子類中刪除@Id字段,它們將從父類繼承。

的第二個問題是,你必須@JoinColumn具有相同的名稱爲@Id

@ManyToOne 
@JoinColumn(name = "doc_id") // give some unique column name here 
private Docteur doc; 

附:小問題:

您有10 + @Autowired依賴關係在您的Controller。這不是設計課程的好方法。每次觀察太多@Autowired一個類中的依賴關係會引發你思考你的應用程序設計 - 這個類承擔了太多的責任。

+0

刪除來自兩個子類的Id字段仍然存在以下錯誤: 引起:org.hibernate.MappingException:實體映射中的重複列:com.vivalio.springmvc.model.Patient列:id(應使用insert = 「false」update =「false」) – badaboum

+0

這裏是stacktrace: 引起:org.springframework.beans.factory.BeanCreationException:在類路徑資源[com/vivalio/springmvc/configuration]中定義名稱爲'sessionFactory' /HibernateConfiguration.class]:調用初始化方法失敗;嵌套異常是org.hibernate.MappingException:實體映射中的重複列:com.vivalio.springmvc.model.Patient列:id(應該使用insert =「false」update =「false」映射) \t at org.springframework .beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory。 – badaboum

+1

看看更新的答案 –

相關問題