2017-07-07 132 views
-1

而在Apache Tomcat運行9我的項目,我得到這個錯誤:Spring的MVC與Hibernate Maven項目

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/cd/coe/configuration/PersistenceJPAConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean com.cd.coe.configuration.PersistenceJPAConfig.entityManagerFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/cd/coe/configuration/PersistenceJPAConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.cd.coe.configuration.PersistenceJPAConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [jdbc.driverClassName] not found 
org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:597) 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1094) 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:989) 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) 
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) 
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973) 
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:750) 
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) 
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658) 
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:530) 
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484) 
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) 
javax.servlet.GenericServlet.init(GenericServlet.java:158) 
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620) 
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) 
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784) 
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802) 
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1452) 
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
java.lang.Thread.run(Thread.java:745) 

這是我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>com.cd.elibrary</groupId> 
<artifactId>E-library</artifactId> 
<packaging>war</packaging> 
<version>0.0.1-SNAPSHOT</version> 
<name>E-library Maven Webapp</name> 
<url>http://maven.apache.org</url> 

<properties> 
    <springframework.version>4.0.6.RELEASE</springframework.version> 
    <hibernate.version>5.2.10.Final</hibernate.version> 
    <mysql.version>5.1.31</mysql.version> 
</properties> 

<dependencies> 
    <!-- Spring --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-tx</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-orm</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 

    <!-- Hibernate --> 

    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>4.1.4.Final</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>5.2.3.Final</version> 
    </dependency> 
    <!-- jsr303 validation --> 
    <dependency> 
     <groupId>javax.validation</groupId> 
     <artifactId>validation-api</artifactId> 
     <version>1.1.0.Final</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-validator</artifactId> 
     <version>5.1.3.Final</version> 
    </dependency> 

    <!-- MySQL --> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>6.0.6</version> 
     <scope>runtime</scope> 
    </dependency> 
    <!-- Servlet+JSP+JSTL --> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>3.1.0</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet.jsp</groupId> 
     <artifactId>javax.servlet.jsp-api</artifactId> 
     <version>2.3.1</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 

    <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
     <version>1.2.14</version> 
    </dependency> 
</dependencies> 
<build> 
    <finalName>E-library</finalName> 
</build> 

我的配置文件如下:

@Configuration 
@EnableTransactionManagement 
public class PersistenceJPAConfig { 

@Autowired 
private Environment environment; 

@Bean 
public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); 
    em.setDataSource(dataSource()); 
    em.setPackagesToScan(new String[] { "com.cd.coe.model" }); 

    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
    em.setJpaVendorAdapter(vendorAdapter); 
    em.setJpaProperties(additionalProperties()); 

    return em; 
} 

@Bean(name = "dataSource") 
public DataSource dataSource() { 
    DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
    dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName")); 
    dataSource.setUrl(environment.getRequiredProperty("jdbc.url")); 
    dataSource.setUsername(environment.getRequiredProperty("jdbc.username")); 
    dataSource.setPassword(environment.getRequiredProperty("jdbc.password")); 
    return dataSource; 
} 

@Bean 
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { 
    JpaTransactionManager transactionManager = new JpaTransactionManager(); 
    transactionManager.setEntityManagerFactory(emf); 

    return transactionManager; 
} 

@Bean 
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { 
    return new PersistenceExceptionTranslationPostProcessor(); 
} 

我的application.properties文件是正確的。

在服務器上運行此代碼時出現問題,因此無法找到確切的錯誤。我已經嘗試過不同版本的hibernate和mysql連接器,但問題仍然存在。

+1

看起來像屬性jdbc.driverClassName未設置 – Jens

回答

0

嘗試使用 -

@PropertySource({ "classpath:application.properties" }) //Add this annotaion at your class 

而且,

environment.getProperty("jdbc.driverClassName"); //Use this to read a property 

希望這會爲你:)工作

0

如果你正確地分析堆棧跟蹤,它清楚地說,問題爲「需要鍵[jdbc.driverClassName]未找到'。請確保屬性文件具有提到的密鑰。您還需要在您的配置類中設置PropertySource。

@PropertySource("classpath:application.properties") 
相關問題