首页 新闻 搜索 专区 学院

关于spring+hibernate+jpa的一个问题

0
悬赏园豆:50 [已解决问题] 解决于 2016-04-20 21:17

 按照书上给的例子,部署完起服务的时候老是报错,我不知道错在哪里,以下是错误信息:

10:55:55.115 [localhost-startStop-1] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactoryBean' defined in class com.wrox.config.RootContextConfiguration: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/boot/registry/selector/StrategyRegistrationProvider
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973) ~[spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:750) ~[spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) ~[spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) ~[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) [spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) [spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4809) [catalina.jar:8.0.33]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5251) [catalina.jar:8.0.33]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) [catalina.jar:8.0.33]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) [catalina.jar:8.0.33]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) [catalina.jar:8.0.33]
at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_77]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_77]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_77]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
Caused by: java.lang.NoClassDefFoundError: org/hibernate/boot/registry/selector/StrategyRegistrationProvider
at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_77]
at java.lang.ClassLoader.defineClass(Unknown Source) ~[?:1.8.0_77]
at java.security.SecureClassLoader.defineClass(Unknown Source) ~[?:1.8.0_77]
at java.net.URLClassLoader.defineClass(Unknown Source) ~[?:1.8.0_77]
at java.net.URLClassLoader.access$100(Unknown Source) ~[?:1.8.0_77]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_77]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_77]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_77]
at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_77]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_77]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_77]
at java.lang.Class.forName0(Native Method) ~[?:1.8.0_77]
at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_77]
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1291) ~[catalina.jar:8.0.33]
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139) ~[catalina.jar:8.0.33]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:222) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_77]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_77]
at java.lang.Class.forName0(Native Method) ~[?:1.8.0_77]
at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_77]
at java.util.ServiceLoader$LazyIterator.nextService(Unknown Source) ~[?:1.8.0_77]
at java.util.ServiceLoader$LazyIterator.next(Unknown Source) ~[?:1.8.0_77]
at java.util.ServiceLoader$1.next(Unknown Source) ~[?:1.8.0_77]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.loadJavaServices(ClassLoaderServiceImpl.java:339) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.boot.registry.selector.internal.StrategySelectorBuilder.buildSelector(StrategySelectorBuilder.java:172) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:253) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.buildBootstrapServiceRegistry(EntityManagerFactoryBuilderImpl.java:518) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:206) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:51) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:182) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:177) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:336) ~[spring-orm-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) ~[spring-orm-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
... 21 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.boot.registry.selector.StrategyRegistrationProvider
at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_77]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_77]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_77]
at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_77]
at java.lang.ClassLoader.defineClass(Unknown Source) ~[?:1.8.0_77]
at java.security.SecureClassLoader.defineClass(Unknown Source) ~[?:1.8.0_77]
at java.net.URLClassLoader.defineClass(Unknown Source) ~[?:1.8.0_77]
at java.net.URLClassLoader.access$100(Unknown Source) ~[?:1.8.0_77]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_77]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_77]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_77]
at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_77]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_77]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_77]
at java.lang.Class.forName0(Native Method) ~[?:1.8.0_77]
at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_77]
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1291) ~[catalina.jar:8.0.33]
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139) ~[catalina.jar:8.0.33]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:222) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_77]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_77]
at java.lang.Class.forName0(Native Method) ~[?:1.8.0_77]
at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_77]
at java.util.ServiceLoader$LazyIterator.nextService(Unknown Source) ~[?:1.8.0_77]
at java.util.ServiceLoader$LazyIterator.next(Unknown Source) ~[?:1.8.0_77]
at java.util.ServiceLoader$1.next(Unknown Source) ~[?:1.8.0_77]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.loadJavaServices(ClassLoaderServiceImpl.java:339) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.boot.registry.selector.internal.StrategySelectorBuilder.buildSelector(StrategySelectorBuilder.java:172) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:253) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.buildBootstrapServiceRegistry(EntityManagerFactoryBuilderImpl.java:518) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:206) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:51) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:182) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:177) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:336) ~[spring-orm-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) ~[spring-orm-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
... 21 more

 

《=================================》

以下是出错的位置的Java代码

 

package com.wrox.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.Ordered;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
import org.springframework.web.bind.annotation.ControllerAdvice;

import javax.persistence.SharedCacheMode;
import javax.persistence.ValidationMode;
import javax.sql.DataSource;
import java.nio.charset.StandardCharsets;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.Executor;

@Configuration
@EnableScheduling
@EnableAsync(
mode = AdviceMode.PROXY, proxyTargetClass = false,
order = Ordered.HIGHEST_PRECEDENCE
)
@EnableTransactionManagement(
mode = AdviceMode.PROXY, proxyTargetClass = false,
order = Ordered.LOWEST_PRECEDENCE
)
@ComponentScan(
basePackages = "com.wrox.site",
excludeFilters =
@ComponentScan.Filter({Controller.class, ControllerAdvice.class})
)
public class RootContextConfiguration implements
AsyncConfigurer, SchedulingConfigurer
{
private static final Logger log = LogManager.getLogger();
private static final Logger schedulingLogger =
LogManager.getLogger(log.getName() + ".[scheduling]");

@Bean
public MessageSource messageSource()
{
ReloadableResourceBundleMessageSource messageSource =
new ReloadableResourceBundleMessageSource();
messageSource.setCacheSeconds(-1);
messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
messageSource.setBasenames(
"/WEB-INF/i18n/titles", "/WEB-INF/i18n/messages",
"/WEB-INF/i18n/errors", "/WEB-INF/i18n/validation"
);
return messageSource;
}

@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean()
{
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setValidationMessageSource(this.messageSource());
return validator;
}

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor()
{
MethodValidationPostProcessor processor =
new MethodValidationPostProcessor();
processor.setValidator(this.localValidatorFactoryBean());
return processor;
}

@Bean
public ObjectMapper objectMapper()
{
ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE,
false);
return mapper;
}

@Bean
public Jaxb2Marshaller jaxb2Marshaller()
{
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan(new String[] { "com.wrox.site" });
return marshaller;
}

@Bean
public DataSource springJpaDataSource()
{
JndiDataSourceLookup lookup = new JndiDataSourceLookup();
return lookup.getDataSource("jdbc/SpringJpa");
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean()
{
Map<String, Object> properties = new Hashtable<>();
properties.put("javax.persistence.schema-generation.database.action",
"none");

HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect");

LocalContainerEntityManagerFactoryBean factory =
new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(adapter);
factory.setDataSource(this.springJpaDataSource());
factory.setPackagesToScan("com.wrox.site.entities");
factory.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE);
factory.setValidationMode(ValidationMode.NONE);
factory.setJpaPropertyMap(properties);
return factory;
}

@Bean
public PlatformTransactionManager jpaTransactionManager()
{
return new JpaTransactionManager(
this.entityManagerFactoryBean().getObject()
);
}

@Bean
public ThreadPoolTaskScheduler taskScheduler()
{
log.info("Setting up thread pool task scheduler with 20 threads.");
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(20);
scheduler.setThreadNamePrefix("task-");
scheduler.setAwaitTerminationSeconds(60);
scheduler.setWaitForTasksToCompleteOnShutdown(true);
scheduler.setErrorHandler(t -> schedulingLogger.error(
"Unknown error occurred while executing task.", t
));
scheduler.setRejectedExecutionHandler(
(r, e) -> schedulingLogger.error(
"Execution of task {} was rejected for unknown reasons.", r
)
);
return scheduler;
}

@Override
public Executor getAsyncExecutor()
{
Executor executor = this.taskScheduler();
log.info("Configuring asynchronous method executor {}.", executor);
return executor;
}

@Override
public void configureTasks(ScheduledTaskRegistrar registrar)
{
TaskScheduler scheduler = this.taskScheduler();
log.info("Configuring scheduled method executor {}.", scheduler);
registrar.setTaskScheduler(scheduler);
}
}

嗜血苏菲的主页 嗜血苏菲 | 初学一级 | 园豆:92
提问于:2016-04-20 11:05
< >
分享
最佳答案
1

没有找到这样的类”错误
表面上看是类的名称错误,其实更有可能是classpath环境变量没有配置好。

建议复查类的名称包括大小写,如果有包的情况下,注意类文件放的位置。
环境变量classpath配置中有没有小数点“.”,有没有java默认类库的位置,有没有你自己的类文件顶层目录等.

http://zhidao.baidu.com/link?url=_aZj8Mumu1x8z-LPP7eM-t09TB2Eg_SsNmwYPsShcRoC5s10AHvhXEE0ygmGwz92usKdpM1aDZqZW0nIGTOkHa

收获园豆:50
墨瞋 | 菜鸟二级 |园豆:477 | 2016-04-20 17:53

 原因已找到,在tomcat里多放了几个jar包,还是谢谢你,给我找问题的思路

嗜血苏菲 | 园豆:92 (初学一级) | 2016-04-20 21:16
其他回答(1)
0

我也遇到过这个问题,具体怎么解决的也记不清了,好像是配置文件不对,检查bean

景行彼高松 | 园豆:160 (初学一级) | 2016-04-20 14:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册