java - 无法使用 Java 配置 Autowiring SessionFactory

标签 java spring hibernate autowired

配置类名称以黄色标记,显示 Application context not configured for this file

My Config Class :

@Configuration
@EnableTransactionManagement
public class DatabaseConfig {

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan("com.tornikeshelia.model");
    sessionFactory.setHibernateProperties(getHibernateProperties());
    return sessionFactory;
}

private Properties getHibernateProperties(){
    Properties prop = new Properties();
    Properties properties = new Properties();
    properties.setProperty("hibernate.hbm2ddl.auto", "update");
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL57InnoDBDialect");
    return properties;
}

@Bean(name = "dataSource")
public BasicDataSource dataSource(){
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.cj.jdbc.Driver");

    ds.setUrl("jdbc:mysql://localhost:3306/test?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=utf-8");
    ds.setUsername("username");
    //ds.setPassword("");

    return ds;
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory){
    HibernateTransactionManager txManager = new HibernateTransactionManager();
    txManager.setSessionFactory(sessionFactory);
    return txManager;
}

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

我已经注释掉了ds.setPassword因为我的测试数据库没有任何密码保护。另外, hibernate 不会从我的 Entity 自动创建表class ,我认为那是因为 Spring 没有读取这个配置文件

当尝试autowire时我的 SessionFactor DaoImpl类通过:

@Resource(name = "sessionFactory")
private SessionFactory session;

错误显示 Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.tornikeshelia.dao.PersonDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

SOLUTION : The problem was 1)The location of config package and 2) in the warning message that was on the class name (yellowed out with message - Application context not configured for this file So if anybody has same problem here's the solution -

1) move your config package inside the parent package where the model package is

2) Step on the class name (click anywhere on the class name) the yellow light bulb will appear, click on it and then click on configure application context, a new tab will appear where you should choose this class and press okay.

最佳答案

尝试编辑您的配置类并更新 transationManager() bean:

    @Bean
    public HibernateTransactionManager transactionManager(){
        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(sessionFactory().getObject());
        return txManager;
    }

您可能会发生错误,因为您尝试注入(inject)尚未创建的 bean。

关于java - 无法使用 Java 配置 Autowiring SessionFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57108860/

相关文章:

java - 使用 owasp-java-html-sanitizer 嵌入 CSS

jquery - Thymeleaf,将对象发送到ajax

java - 在 hibernate 实体和数据传输对象之间转换的好模式是什么?

java - Hibernate 左连接返回对象

java - System.out.format 如何防止死锁?

java - 在 Apache Beam 中连接行

java - "E/RecyclerView: No adapter attached; skipping layout"

java - Spring MVC 自定义作用域 bean

java - Spring ModelAttribute正确解析

java - 在java中构建应用程序主机(例如wordpress.com或google group)的可能解决方案是什么?