线程" main"中的例外情况org.springframework.beans.factory.BeanCreationException:使用名称' actelService'

时间:2016-02-16 11:55:42

标签: java spring hibernate jpa

我正在做spring + hibernate应用程序。有一个类似的问题,但公认的解决方案不适用。

详细说明:

java项目:

com.springJPA.domain

-Actels.java

-Adresse.java

-Region.java

-Ville.java

-Utilisateur.java ...

com.springJPA.service

-ActelsService.java

-AdresseService.java

-RegionService.java

-VilleService.java

-UtilisateurService.java

-DAOfonction.java(界面)

-FonctionDAO.java(实现DAOfonction)  ...

com.springJPA.util

-MyEntityManagerFactory.java

-TransactionAspect.java ...

com.springJPA.test

-Main.java

我得到的错误是:

févr. 16, 2016 11:54:09 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
    Infos: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@9446e4: startup date [Tue Feb 16 11:54:09 WAT 2016]; root of context hierarchy
    févr. 16, 2016 11:54:09 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    Infos: Loading XML bean definitions from class path resource [application-context.xml]
    févr. 16, 2016 11:54:10 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    Infos: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@176982e: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,actelService,adresseService,demandeService,fonctionDAO,regionService,utilisateurService,villeService,myEMF_appActel,tr,org.springframework.aop.config.internalAutoProxyCreator,adrPC,utlPC,rgPC,villePC,actelPC,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,org.springframework.aop.aspectj.AspectJPointcutAdvisor#4,org.springframework.aop.aspectj.AspectJPointcutAdvisor#5,org.springframework.aop.aspectj.AspectJPointcutAdvisor#6,org.springframework.aop.aspectj.AspectJPointcutAdvisor#7,org.springframework.aop.aspectj.AspectJPointcutAdvisor#8,org.springframework.aop.aspectj.AspectJPointcutAdvisor#9]; root of factory hierarchy
    févr. 16, 2016 11:54:10 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
    Infos: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@176982e: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,actelService,adresseService,demandeService,fonctionDAO,regionService,utilisateurService,villeService,myEMF_appActel,tr,org.springframework.aop.config.internalAutoProxyCreator,adrPC,utlPC,rgPC,villePC,actelPC,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,org.springframework.aop.aspectj.AspectJPointcutAdvisor#4,org.springframework.aop.aspectj.AspectJPointcutAdvisor#5,org.springframework.aop.aspectj.AspectJPointcutAdvisor#6,org.springframework.aop.aspectj.AspectJPointcutAdvisor#7,org.springframework.aop.aspectj.AspectJPointcutAdvisor#8,org.springframework.aop.aspectj.AspectJPointcutAdvisor#9]; root of factory hierarchy
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'actelService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.springJPA.util.MyEntityManagerFactory com.springJPA.service.ActelsService.emf; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.springJPA.util.MyEntityManagerFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=myEMF)}
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1055)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
        at com.springJPA.test.Main.main(Main.java:19)
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.springJPA.util.MyEntityManagerFactory com.springJPA.service.ActelsService.emf; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.springJPA.util.MyEntityManagerFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=myEMF)}
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:507)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)
        ... 13 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.springJPA.util.MyEntityManagerFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=myEMF)}
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:901)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:770)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:685)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
        ... 15 more

Main.java

    package com.springJPA.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springJPA.service.ActelsService;
import com.springJPA.service.AdresseService;
import com.springJPA.service.FonctionDAO;
import com.springJPA.service.RegionService;
import com.springJPA.service.UtilisateurService;
import com.springJPA.service.VilleService;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
        UtilisateurService utlService = (UtilisateurService) context.getBean("utilisateurService");
        AdresseService adrService = (AdresseService) context.getBean("adresseService");
        RegionService rgService = (RegionService) context.getBean("regionService");
        VilleService villeService = (VilleService) context.getBean("villeService");
        ActelsService atlService = (ActelsService) context.getBean("actelService");

        FonctionDAO fonctionDAO = new FonctionDAO();
        fonctionDAO.createUTILISATEUR(utlService);
        fonctionDAO.createADRESSE(adrService);
        fonctionDAO.createREGION(rgService);
        fonctionDAO.afficherLISTE_REGION();
        fonctionDAO.afficherNOM_REGION_PAR_ID(52);
        fonctionDAO.afficherNOM_REGION_PAR_NUM(10);
        fonctionDAO.createACTEL(atlService);
        fonctionDAO.afficherLISTE_ACTEL();
        fonctionDAO.afficherLISTE_ACTEL_PAR_REGION(52);
        fonctionDAO.createVILLE(villeService);
        fonctionDAO.afficherNOM_VILLE_PAR_NUM(10);
        fonctionDAO.afficherLISTE_VILLE();
        fonctionDAO.afficherLISTE_VILLE_PAR_ACTEL(52);
    }
}

应用context.xml中:

 <?xml version="1.0" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.springJPA.service"/>
<context:component-scan base-package="com.springJPA.util"/>
<bean id="tr" class="com.springJPA.util.TransactionAspect">
    <property name="entityManagerFactory" ref="myEMF"/>
</bean>

<aop:config>
    <aop:pointcut expression="execution(* com.springJPA.service.AdresseService.*(..))" id="adrPC"/>
    <aop:pointcut expression="execution(* com.springJPA.service.UtilisateurService.*(..))" id="utlPC"/>
    <aop:pointcut expression="execution(* com.springJPA.service.RegionService.*(..))" id="rgPC"/>
    <aop:pointcut expression="execution(* com.springJPA.service.VilleService.*(..))" id="villePC"/>
    <aop:pointcut expression="execution(* com.springJPA.service.ActelsService.*(..))" id="actelPC"/>
    <aop:aspect ref="tr">
     <aop:before pointcut-ref="adrPC" method="begin"></aop:before>
     <aop:after pointcut-ref="adrPC" method="commit"></aop:after>

     <aop:before pointcut-ref="utlPC" method="begin"></aop:before>
     <aop:after pointcut-ref="utlPC" method="commit"></aop:after> 

     <aop:before pointcut-ref="rgPC" method="begin"></aop:before>
     <aop:after pointcut-ref="rgPC" method="commit"></aop:after>

     <aop:before pointcut-ref="villePC" method="begin"></aop:before>
     <aop:after pointcut-ref="villePC" method="commit"></aop:after>                 

     <aop:before pointcut-ref="actelPC" method="begin"></aop:before>
     <aop:after pointcut-ref="actelPC" method="commit"></aop:after>
    </aop:aspect>
 </aop:config>
</beans>

ActelsService.java(AdresseService,RegionService,UtilisateurService是相同的代码,只是函数和查询不同)

package com.springJPA.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.springJPA.domain.Actels;
import com.springJPA.util.MyEntityManagerFactory;

@Component("actelService")
public class ActelsService {
    @Autowired @Qualifier("myEMF")
    private MyEntityManagerFactory emf;
    private EntityManager entityManager;

    public void ajoutActel(Actels actel) {
        entityManager = emf.getEntityManager();
        entityManager.persist(actel);
    }
    public void modifActel(Actels actel) {
        entityManager = emf.getEntityManager();
        entityManager.merge(actel);

    }
    public void supprimerActel(Actels actel) {
        entityManager = emf.getEntityManager();
        entityManager.remove(actel);
    }
    public List<String> listeActel() {
        entityManager = emf.getEntityManager();
        TypedQuery<String> query = entityManager.createQuery("Select DISTINCT LOWER(e.nom_actel) from Actels e ORDER BY e.id_actels ASC", String.class);
        List<String> results = query.getResultList();
        return results;
    }

    /**
     * listeActel
     * @param id_region
     * @return
     */
    public List<String> listeActelParID_REGION(int id_region) {
        entityManager = emf.getEntityManager();
        TypedQuery<String> query = entityManager.createQuery("Select DISTINCT LOWER(e.nom_actel) from Actels e where e.region_id_region = ?1 ORDER BY e.id_actels ASC", String.class);
        query.setParameter("1", id_region);
        List<String> results = query.getResultList();
        return results;
    }

    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public MyEntityManagerFactory getEmf() {
        return emf;
    }

    public void setEmf(MyEntityManagerFactory emf) {
        this.emf = emf;
    }
}

DAOfonction.java

public interface DAOfonction {
.
.
.

public void createUTILISATEUR(UtilisateurService utlService);
    public void createADRESSE(AdresseService adrService);
    public void createREGION(RegionService rgService);
    public void createACTEL(ActelsService atlService);
    public void createVILLE(VilleService villeService);

    public List<String> afficherLISTE_REGION();
    public String afficherNOM_REGION_PAR_ID(int id_region);
    public String afficherNOM_REGION_PAR_NUM(int num_region);
    public String afficherNOM_VILLE_PAR_NUM(int num_ville);
    public List<String> afficherLISTE_ACTEL();
    public List<String> afficherLISTE_ACTEL_PAR_REGION(int id_region);
    public List<String> afficherLISTE_VILLE();
    public List<String> afficherLISTE_VILLE_PAR_ACTEL(int id_actel);

}

FonctionDAO.java

public class FonctionDAO implements DAOfonction {
.
.
.
public List<String> afficherLISTE_VILLE_PAR_ACTEL(int id_actel) {
    ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
    VilleService atlService = (VilleService) context.getBean("villeService");
    List<String> listeVilleParActel= atlService.listeVilleParID_ACTEL(id_actel);
     for(String e:listeVilleParActel) {
         System.out.println("Ville par actel :"+e);
      }
    return listeVilleParActel;      
}

public String afficherNOM_VILLE_PAR_NUM(int num_ville) {
    ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
    VilleService villeService = (VilleService) context.getBean("villeService");

    System.out.println("Nom Ville par num: "+villeService.getNomVilleParNUM_VILLE(num_ville));
    return villeService.getNomVilleParNUM_VILLE(num_ville);
}

public String afficherNOM_REGION_PAR_NUM(int num_region) {
    ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
    RegionService rgService = (RegionService) context.getBean("rgService");
    System.out.println("Nom Region par num: "+rgService.getNomRegionParNUM_REGION(num_region));
    return rgService.getNomRegionParNUM_REGION(num_region);
}

public String afficherNOM_REGION_PAR_ID(int id_region) {
    ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
    RegionService rgService = (RegionService) context.getBean("rgService");

    System.out.println("Nom Region par id: "+rgService.getNomRegionParID_REGION(id_region));
    return rgService.getNomRegionParID_REGION(id_region);
}
public List<String> afficherLISTE_ACTEL_PAR_REGION(int id_region) {
    ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
    ActelsService atlService = (ActelsService) context.getBean("atlService");
    List<String> listeActelparRegion= atlService.listeActelParID_REGION(id_region);
     for(String e:listeActelparRegion) {
         System.out.println("Actel par region :"+e);
      }
    return listeActelparRegion;
}
public List<String> afficherLISTE_ACTEL() {
    ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
    ActelsService atlService = (ActelsService) context.getBean("atlService");
    List<String> listeActel= atlService.listeActel();
     for(String e:listeActel) {
         System.out.println("Actel :"+e);
      }
    return listeActel;
}

public List<String> afficherLISTE_VILLE() {
    ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
    VilleService villeService = (VilleService) context.getBean("villeService");
    List<String> listeVille= villeService.listeVille();
      for(String e:listeVille) {
             System.out.println("Ville :"+e);
          }
    return listeVille;
}

public List<String> afficherLISTE_REGION() {
    ApplicationContext context = new          ClassPathXmlApplicationContext("application-context.xml");
    RegionService rgService = (RegionService) context.getBean("rgService");
    List<String> listeRegion= rgService.listeRegion();
      for(String e:listeRegion) {
             System.out.println("Region :"+e);
          }
    return listeRegion;
}

public void createUTILISATEUR(UtilisateurService utlService) {
    creatUtilisateur(utlService, 1, "user1", "user1", "user1");
    creatUtilisateur(utlService, 2, "user2", "user2", "user2");
}

public void creatUtilisateur(UtilisateurService utlService, int id_utilisateur, String nom, String nom_utilisateur, String motdepasse) {
    Utilisateur utilisateur = new Utilisateur();
    utilisateur.setNom(nom);
    utilisateur.setNomUtilisateur(nom_utilisateur);
    utilisateur.setMotdepasse(motdepasse);
    utlService.ajoutUtilisateur(utilisateur);
}
public void createADRESSE(AdresseService adrService) {
    createAdresse(adrService, 1,"kantawi", "Sousse", "Tunisie");
    createAdresse(adrService, 2,"kantawi2", "Sousse2", "Tunisie2");
}
public void createAdresse(AdresseService adrService, int id_adresse, String rue, String ville, String pays) {
    Adresse adresse = new Adresse();
    adresse.setRue(rue);
    adresse.setVille(ville);
    adresse.setPays(pays);
    adrService.ajoutAdresse(adresse);
}

public void createACTEL(ActelsService atlService) {
    createActel(atlService, 1, 1, "actel_borjlouzir", 0x002);
    createActel(atlService, 2, 1, "actel_ariana", 0x001);
    createActel(atlService, 3, 2, "actel_mjezelbeb", 0x004);
    createActel(atlService, 4, 2, "actel_beja", 0x003);
    createActel(atlService, 5, 3, "actel_ezzahra", 0x048);
    createActel(atlService, 6, 3, "actel_rades", 0x006);
    createActel(atlService, 7, 3, "actel_benarous", 0x005);
    createActel(atlService, 8, 4, "actel_mzbourguib", 0x008);
    createActel(atlService, 9, 5, "actel_bizerte", 0x007);
    createActel(atlService, 10, 6, "actel_gabessud",0x010);
    createActel(atlService, 11, 6, "actel_gabes",0x009);
    createActel(atlService, 12, 6, "actel_elhammat",0x011);
    createActel(atlService, 13, 7, "actel_gafsa",0x012);
    createActel(atlService, 14, 8, "actel_kairouan",0x013);
    createActel(atlService, 15, 8, "actel_baytelhekm",0x014);
    createActel(atlService, 16, 9, "actel_sbitla",0x016);
    createActel(atlService, 17, 9, "actel_kasserine",0x015);
    createActel(atlService, 18, 10, "actel_kebili",0x017);
}
public void createVILLE(VilleService villeService) {
    createVille(villeService, 1, 1, "borjlouzir", 0x000, "rue", "RUE MUSTAPHA MOH", "R_Semiramis");
    createVille(villeService, 2, 1, "borjlouzir", 0x000, "rue", "Rue Abderrazek J", "R_El Ahmadi");
    createVille(villeService, 3, 1, "borjlouzir", 0x000, "rue", "Rue DE VARSOVIE", "R_NADA");
    createVille(villeService, 4, 1, "borjlouzir", 0x000, "rue", "Rue Jawhara", "R_Emma");
    createVille(villeService, 5, 2, "ariana", 0x000, "rue", "Rue Abderrahmen ", "R_MALEK");
    createVille(villeService, 6, 2, "ariana", 0x000, "rue", "Rue Belhassen", "R_les Reves");
    createVille(villeService, 7, 2, "ariana", 0x000, "rue", "Rue Fethi Zouhei", "R_Ennour");
    createVille(villeService, 8, 2, "ariana", 0x000, "avenue", "Ave Taieb MHiri", "R_YASMINA");
    createVille(villeService, 9, 2, "ariana", 0x000, "avenue", "Ave Hédi Nouira", "R_les Jasmins");
    createVille(villeService, 10, 3, "mjezelbeb", 0x000, "rue", "Rue Abderrahmen", "R_E");
    createVille(villeService, 11, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_El Ahmadi");
    createVille(villeService, 12, 54, "manar", 0x000, "rue", "Rue de", "");
    createVille(villeService, 13, 43, "Kalaa_Essghi", 0x000, "rue", "Rue de", "");
    createVille(villeService, 14, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_MALEK");
    createVille(villeService, 15, 42, "kalaa_Kebira", 0x000, "rue", "Rue Abderrazek E", "R_Ennour");
    createVille(villeService, 16, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 17, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 18, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 19, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 20, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 21, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 22, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 23, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 24, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 25, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 26, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 27, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 28, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 29, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 30, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 31, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 32, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
    createVille(villeService, 33, 42, "kalaa_Kebira", 0x000, "rue", "RUE 1ER JUIN", "R_Ennour");
}
public void createREGION(RegionService rgService) {
    createRegion(rgService, 1, "Ariana", 1);
    createRegion(rgService, 2, "Beja", 2);
    createRegion(rgService, 3, "BenArous", 3);
    createRegion(rgService, 4, "Bizerte", 4);
    createRegion(rgService, 5, "Gabes", 5);
    createRegion(rgService, 6, "Gafsa", 6);
    createRegion(rgService, 7, "Kairouan", 7);
    createRegion(rgService, 8, "Kasserine", 8);
    createRegion(rgService, 9, "Kebili", 9);
    createRegion(rgService, 10, "Kef", 10);
    createRegion(rgService, 11, "Manouba", 11);
    createRegion(rgService, 12, "Medenine", 12);

}

private static void createRegion(RegionService rgService, int id_region,String nom_region, int num_region) {
    Region region = new Region();
    region.setNom_region(nom_region);
    region.setNum_region(num_region);
    rgService.ajoutRegion(region);
}

private static void createVille(VilleService villeService, int id_ville, int id_region, String nom_ville, int num_ville, String type_voie, String voie, String resedence){
    Ville ville = new Ville();
    ville.setNom_ville(nom_ville);
    ville.setNum_ville(num_ville);
    ville.setType_voie(type_voie);
    ville.setVoie(voie);
    ville.setResedence(resedence);
    villeService.ajoutVille(ville);
}

private static void createActel(ActelsService atlService, int id_actels, int id_ville, String nom_actel, int num_actel){
    Actels actel = new Actels();
    actel.setNomActels(nom_actel);
    actel.setNum_actel(num_actel);
    atlService.ajoutActel(actel);
}

}

MyEntityManagerFactory.java

package com.springJPA.util;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.springframework.stereotype.Component;

@Component("myEMF")
public class MyEntityManagerFactory {
    private EntityManager entityManager;
    private String unitName = "SpringJPA";

    public EntityManager getEntityManager() {
        if(entityManager == null){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(unitName);
        entityManager = emf.createEntityManager();
        }
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public String getUnitName() {
        return unitName;
    }

    public void setUnitName(String unitName) {
        this.unitName = unitName;
    }

}

1 个答案:

答案 0 :(得分:1)

MyEntitymanagerFacotry的bean命名错误。在您的名称为@Component("myEMF_appActel")的bean上,您应该使用@Component("myEMF")