我是 java 新来的!当我运行junit测试时,控制台向我显示异常,我不知道如何解决这个问题。我的hibernate配置有什么问题?这段代码非常适用于h2数据库,但是当我将数据库和配置参数更改为mysql 8.0时,eclipse返回异常。我希望有人能给我一些有用的建议,并解释如何解决这个问题。谢谢。
这是我的mysql 8.0的hibernate配置
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@ComponentScan(basePackages= {"com.savaleks.websiteback.dto"})
@EnableTransactionManagement
public class HibernateConfig {
private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/website";
private final static String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver";
private final static String DATABASE_DIALECT = "org.hibernate.dialect.MySQL8Dialect";
private final static String DATABASE_USERNAME = "root";
private final static String DATABASE_PASSWORD = "";
@Bean
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
// Database connection information
dataSource.setDriverClassName(DATABASE_DRIVER);
dataSource.setUrl(DATABASE_URL);
dataSource.setUsername(DATABASE_USERNAME);
dataSource.setPassword(DATABASE_PASSWORD);
return dataSource;
}
@Bean
public SessionFactory getSessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);
builder.addProperties(getHibernateProperties());
builder.scanPackages("com.savaleks.websiteback.dto");
return builder.buildSessionFactory();
}
// All Hibernate properties returned in this method
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", DATABASE_DIALECT);
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "true");
return null;
}
@Bean
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
return transactionManager;
}
}
这是控制台输出
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryDAO': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getSessionFactory' defined in com.savaleks.websiteback.config.HibernateConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'getSessionFactory' threw exception; nested exception is java.lang.NullPointerException
pom.xml文件
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.20.RELEASE</spring.version>
<mysql.version>8.0.13</mysql.version>
<hibernate.version>5.2.17.Final</hibernate.version>
</properties>
<dependencies>
<!-- JUNIT TEST -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- SPRING CONTEXT -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- MYSQL DATABASE -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- HIBERNATE -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- DATABASE CONNECTION POOLING -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
如果需要更多的信息,我可以加载更多的类和控制台输出。有什么问题吗?有什么想法吗?
1条答案
按热度按时间iq3niunx1#
更改为:
或者,
或者,
没有更多的方法了。
进一步阅读:
@javadoc的spring框架中@configuration classes段落中的bean方法
@Bean
.1.12.1. 基本概念:@bean和@configuration in spring framework reference(version 5.1.3.release)