当前位置: 首页 > 知识库问答 >
问题:

Spring Boot-org.springframework.beans.factory.BeanCreationException:创建bean时出错

慎俊艾
2023-03-14

我是春靴新来的。我试图用hibernate连接MySql db,但当我命令

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>
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

hibernate.dialog:org.hibernate.dialt.mysql5dialog
hibernate.show_sql:true
hibernate.hbm2ddl.auto:update
entitymanager.packageStoscan:com.autkusoytas

共有1个答案

霍修筠
2023-03-14

从http://www.java2s.com/code/jar/d/downloaddom4j16jar.htm下载dom4j的jar文件

在此站点,您可以看到解决此问题所需的DocumentException类。

 类似资料:
  • 我正在尝试运行以下代码 我的配置类是: AppConfig.class RepositoryConfig.class 我的模型课是:POC。班 我的存储库类是:POCRepository类 我的服务等级是: 我正在运行main类,出现以下错误。 我的网络。xml详细信息如下 请帮帮我

  • 我试图编译一个非常简单的程序,将包含3个用户的简单表保存到http://localhost/phpmyadmin,以清空名为,users ' '的数据库,但它仍然显示异常,您可以看到。 1个异常org.springframework.beans.factory。BeanCreationException:创建在类路径资源[org/springframework/boot/autoconfigure

  • 当我启动Weblogic时(使用jar:hibernate-core-4.3.6.final.jar和hibernate-jpa-2.1-api-1.0.0.final.jar),遇到以下错误信息: 无法自动连接字段:private org.hibernate.sessionFactory com.nscorp.lars.shopleveling.core.dao.impl.Dataloaddao

  • 我在SpringBoot应用程序中创建HighHendRestClient bean时遇到一个错误。我已经做了一个测试'app',在那里我检查了我可以实例化我想要的对象,然后进行我想要的调用,我现在正在做一个新的应用程序的婴儿步骤。 就我所能看到的(我还没有用它做太多...) 当我添加它时(最初我传入了RestClient bean,但现在我临时创建了一个本地对象,以便更清晰) 我得到这个java

  • 我使用的是Spring 3.1.4 服务实现 DAO实现 web.xml

  • 严重:上下文初始化失败org.springframework.beans.factory.BeanCreationException:创建ServletContext资源[/web-inf/mvc-dispatcher-servlet.xml]中定义的名为“org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerM