春季启动启动错误BOOT-INF /找不到类

时间:2019-02-18 07:32:09

标签: java spring hibernate spring-boot jpa

我有一个Spring Boot项目

@SpringBootApplication(exclude = {GsonAutoConfiguration.class})
    public class TranslationApplication {
        public static void main(String[] args) {
    SpringApplication.run(TranslationApplication.class, args);
    }
     @Bean
    public LocalEntityManagerFactoryBean entityManagerFactory(){
            LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();
           factoryBean.setPersistenceUnitName("CIT_DEV");
    return factoryBean;
    }
}

我用

 EntityManager em = localEntityManagerFactoryBean.getObject().createEntityManager();

获取EntityManager实例

persistence.xml中META-INF文件夹下的CIT_DEV持久单元

我使用gradle bootJar生成jar文件,在jar中BOOT-INF / classes / META-INF具有persistence.xml

并且我使用java -jar jarFile启动Spring Boot,它会引发错误

at java.util.zip.ZipFile.open(Native Method) ~[na:1.8.0_40-ea]
 at java.util.zip.ZipFile.(ZipFile.java:220) ~[na:1.8.0_40-ea]
 at java.util.zip.ZipFile.(ZipFile.java:150) ~[na:1.8.0_40-ea]
 at java.util.jar.JarFile.(JarFile.java:166) ~[na:1.8.0_40-ea]
 at java.util.jar.JarFile.(JarFile.java:103) ~[na:1.8.0_40-ea]
 at org.hibernate.boot.archive.internal.JarFileBasedArchiveDescriptor.resolveJarFileReference(JarFileBasedArchiveDescriptor.java:168) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
 at org.hibernate.boot.archive.internal.JarFileBasedArchiveDescriptor.visitArchive(JarFileBasedArchiveDescriptor.java:51) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
 at org.hibernate.boot.archive.scan.spi.AbstractScannerImpl.scan(AbstractScannerImpl.java:47) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
 at org.hibernate.boot.model.process.internal.ScanningCoordinator.coordinateScan(ScanningCoordinator.java:76) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
 at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.prepare(MetadataBuildingProcess.java:99) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.(EntityManagerFactoryBuilderImpl.java:232) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.(EntityManagerFactoryBuilderImpl.java:174) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
 at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:76) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
 at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:171) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
 at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:119) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
 at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:61) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
 at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:50) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
 at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79) [javaee-api-8.0.jar!/:na]
 at org.springframework.orm.jpa.LocalEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalEntityManagerFactoryBean.java:96) [spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) [spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) [spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$87/1170794006.getObject(Unknown Source) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1244) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$87/1170794006.getObject(Unknown Source) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) [spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) [spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) [spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
 at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
 at oracle.cit.translation.TranslationApplication.main(TranslationApplication.java:18) [classes!/:na]
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40-ea]
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40-ea]
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40-ea]
 at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40-ea]
 at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [cit-trans-microservice-1.0.jar:na]
 at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [cit-trans-microservice-1.0.jar:na]
 at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [cit-trans-microservice-1.0.jar:na]
 at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [cit-trans-microservice-1.0.jar:na]

2 个答案:

答案 0 :(得分:2)

只需使用:

@Autowired
EntityManager em;

它由springboot管理。

答案 1 :(得分:0)

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html

默认情况下,Spring Boot不会搜索或使用META-INF / persistence.xml。如果您喜欢使用传统的persistence.xml,则需要定义自己的LocalBeanManagerFactoryBean类型的@Bean(ID为“ entityManagerFactory”),并在其中设置持久性单元名称。

我使用这种方式,但是它仍然有filenotfound异常