Spring Boot - org.springframework.beans.factory.BeanCreationException:使用名称创建 bean 时出错
Posted
技术标签:
【中文标题】Spring Boot - org.springframework.beans.factory.BeanCreationException:使用名称创建 bean 时出错【英文标题】:Spring Boot - org.springframework.beans.factory.BeanCreationException: Error creating bean with name 【发布时间】:2017-04-17 23:35:17 【问题描述】:我是 Spring Boot 的新手。我尝试将 mysql db 与 hibernate 连接,但是当我命令时出现此错误
mvn spring-boot:run
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/autkusoytas/configuration/DatabaseConfig.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
Caused by: java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:343)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
... 15 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 18 common frames omitted
我的 pom.xml 在这里:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.autkusoytas</groupId>
<artifactId>Demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Iyzico</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
DatabaseConfig 在这里:
package com.autkusoytas.configuration;
import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
public class DatabaseConfig
@Value("$db.driver")
private String DB_DRIVER;
@Value("$db.password")
private String DB_PASSWORD;
@Value("$db.url")
private String DB_URL;
@Value("$db.username")
private String DB_USERNAME;
@Value("$hibernate.dialect")
private String HIBERNATE_DIALECT;
@Value("$hibernate.show_sql")
private String HIBERNATE_SHOW_SQL;
@Value("$hibernate.hbm2ddl.auto")
private String HIBERNATE_HBM2DDL_AUTO;
@Value("$entitymanager.packagesToScan")
private String ENTITYMANAGER_PACKAGES_TO_SCAN;
@Bean
public DataSource dataSource()
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(DB_DRIVER);
dataSource.setUrl(DB_URL);
dataSource.setUsername(DB_USERNAME);
dataSource.setPassword(DB_PASSWORD);
return dataSource;
@Bean
public LocalSessionFactoryBean sessionFactory()
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource());
sessionFactoryBean.setPackagesToScan(ENTITYMANAGER_PACKAGES_TO_SCAN);
Properties hibernateProperties = new Properties();
hibernateProperties.put("hibernate.dialect", HIBERNATE_DIALECT);
hibernateProperties.put("hibernate.show_sql", HIBERNATE_SHOW_SQL);
hibernateProperties.put("hibernate.hbm2ddl.auto", HIBERNATE_HBM2DDL_AUTO);
sessionFactoryBean.setHibernateProperties(hibernateProperties);
return sessionFactoryBean;
@Bean
public HibernateTransactionManager transactionManager()
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
// class DatabaseConfig
而且我也在使用这样的 application.properties:
db.driver: com.mysql.jdbc.Driver db.url:jdbc:mysql://localhost:3306/test db.username:root 数据库密码:
hibernate.dialect:org.hibernate.dialect.MySQL5Dialect hibernate.show_sql: 真 hibernate.hbm2ddl.auto:更新 entitymanager.packagesToScan: com.autkusoytas
WEB-INF/库: 感谢您的帮助。
【问题讨论】:
要解决 ClassNotFoundException 和 NoClassDefFoundError 等问题,您应该首先检查异常堆栈。它清楚地表明您缺少一些库。 我已经尝试添加 dom4j 库,但没有成功。 @Utku 好的,但请确保它在类路径中。我在你的 pom.xml 文件中看不到 dom4j 依赖项 是的,我知道。我以前做过,但没有用。当我将 dom4j 依赖项添加到 pom.xml 时,我得到完全相同的错误。 @RohitGaikwad,我更新了问题 【参考方案1】:错误信息很清楚:Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
在 POM 依赖项中添加dom4j
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
【讨论】:
【参考方案2】:从http://www.java2s.com/Code/Jar/d/Downloaddom4j16jar.htm下载dom4j的jar文件
在这里,您可以在此站点上看到解决此问题所需的 DocumentException 类。
【讨论】:
是的。我尝试手动将 jar 添加到 BuildBath 并完成。谢谢。以上是关于Spring Boot - org.springframework.beans.factory.BeanCreationException:使用名称创建 bean 时出错的主要内容,如果未能解决你的问题,请参考以下文章
Spring Loaded is a JVM agent for reloading class file changes